Introduction: Why Caching in PHP is Needed
Any modern web application in PHP faces performance issues. Every database query, configuration file read, or rendering of a complex HTML template takes server time and resources. Caching is a technique that allows you to save the results of expensive operations in fast storage, so that on subsequent requests, ready-made data can be delivered instantly.
PHP has several caching levels: from built-in bytecode caching (OPcache) to distributed systems like Redis and Memcached. In this article, we'll break down how and when to use each of these tools so your site runs faster, handles more load, and saves resources.
Built-in Solutions: OPcache and APCu
Before connecting external services, it's worth looking at what PHP offers "out of the box." Two key mechanisms — OPcache and APCu — can provide a significant performance boost without installing additional software.
OPcache: Bytecode Caching
Every time PHP executes a script, it compiles it into bytecode (opcode). OPcache stores this bytecode in shared memory, allowing the compilation stage to be skipped on subsequent requests. This is especially effective for frameworks (Laravel, Symfony) and CMS (WordPress, Drupal), where dozens of files are loaded on each request.
Configuration in php.ini:
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
It is recommended to enable OPcache on all production servers. This is arguably the simplest way to speed up a PHP application by 20–40%.
APCu: User Data Caching
APCu (Alternative PHP Cache) provides a simple interface for storing arbitrary data in shared memory. Unlike OPcache, which caches code, APCu stores computation results: database data, serialized objects, HTML fragments.
Example of caching an SQL query result:
// Save data for 300 seconds
$key = 'users_list';
$data = apcu_fetch($key, $success);
if (!$success) {
$data = $db->query("SELECT * FROM users")->fetchAll();
apcu_store($key, $data, 300);
}
return $data;
APCu is ideal for single-page applications or projects with a single server. However, its data is stored locally, and when using multiple servers (horizontal scaling), you will need a distributed solution.
Memcached: Simple and Fast Distributed Caching
Memcached is a high-performance caching system that operates in RAM. It was created to speed up dynamic web applications by reducing the load on databases. The main advantages are simplicity, speed, and support for distributed storage.
Installing the PHP extension:
pecl install memcached
Example of connection and operation:
$memcached = new Memcached();
$memcached->addServer('localhost', 11211);
$key = 'article_123';
$cached = $memcached->get($key);
if ($cached === false) {
$article = getArticleFromDB(123);
$memcached->set($key, $article, 3600); // cache for 1 hour
} else {
$article = $cached;
}
Memcached does not support complex data structures (only strings and binary objects), but it handles the task of a "classic" cache perfectly: put data by key, get data by key. It's important to remember that Memcached has no built-in persistence — all data is lost when the server is restarted.
Redis: A Powerful and Flexible Caching Tool
Redis is not just a cache, but a full-fledged in-memory data store with support for persistence, transactions, and complex structures (lists, sets, hashes, queues). For a PHP developer, Red