Data Caching in PHP: First Aid for Your Server
\n\nImagine: you visit your favorite site, and it takes forever to load. Sound familiar? Most often, the reason is not a weak hosting or a "heavy" design, but that the server performs the same work every time. It queries the database again, rebuilds the HTML code, recalculates statistics.
\n\nThe solution to this problem is caching in PHP. It's like preparing meals for the week ahead: you spend time once, and then just reheat. In the world of web development, this is called "data caching." A properly configured PHP cache can speed up a site by 10–50 times and reduce server load to a minimum.
\n\nIn this article, we'll break down what types of caching exist, how to implement them using PHP, and when to choose which tool. Let's go!
\n\nWhy Caching is a Must-Have for Any PHP Project?
\n\nAny dynamic PHP site works on the scheme: request → processing → response. The most expensive operation is working with the database (MySQL, PostgreSQL) and generating complex HTML pages. If you have 1000 visitors per hour, and each request makes 10 SQL queries, the server will quickly "go down."
\n\nCache in PHP solves three main tasks:
\n\n- \n
- Speed — data is served from memory or a file in milliseconds; \n
- Resource savings — the CPU and database get a break; \n
- Scalability — the site handles traffic growth without hardware upgrades. \n
It doesn't matter whether you write in pure PHP or use a framework (Laravel, Symfony, Yii2) — the principles of data caching are the same everywhere.
\n\nMain Types of Caching in PHP
\n\nBefore writing code, let's figure out what exactly we can cache. There are three main levels:
\n\n1. Page Cache
\nThe simplest and most effective type. The finished HTML code of a page is saved to a file or memory. On a repeat request, the server serves the ready HTML, bypassing the PHP interpreter and the database. Ideal for static pages (homepage, blog, catalog).
\n\n2. Data/Object Cache
\nThe results of heavy computations, database queries, and API responses are saved. For example, a list of recent articles, currency exchange rates, user ratings. This is what is commonly called data caching.
\n\n3. Opcode Cache
\nPHP scripts are compiled into bytecode. Tools like OPcache save this bytecode in memory so that the same script is not compiled on every request. It is enabled at the server level and does not require writing code.
\n\nIn this article, we will focus on the first two types, as they can be controlled through PHP code.
\n\nImplementing Caching in PHP: From Simple to Complex
\n\nFile Cache from Scratch
\nThe simplest way of caching in PHP is to save data to files. This does not require installing additional extensions and works on any hosting.
\n\nHere is an example of a simple class for a file cache:
\n\n<?php\nclass FileCache\n{\n private $cacheDir;\n\n public function __construct($cacheDir = '/tmp/php-cache/')\n {\n $this->cacheDir = $cacheDir;\n if (!is_dir($this->cacheDir)) {\n mkdir($this->cacheDir, 0755, true);\n }\n }\n\n public function set($key, $data, $ttl = 3600)\n {\n $file = $this->getFilePath($key);\n $content = [\n 'expires' => time() + $ttl,\n 'data' => $data\n ];\n file_put_contents($file, serialize($content));\n }\n\n public function get($key)\n {\n $file = $this->getFilePath($key);\n if (!file_exists($file)) {\n return null;\n }\n $content = unserialize(file_get_contents($file));\n if ($content['expires'] < time(