Caching in PHP: Redis, Memcached, and Built-in Solutions — Accelerating Web Applications
Every PHP developer faces performance issues: the database responds slowly, pages load for a long time, and the server struggles under load. The solution to these problems lies in proper caching. In this article, we will analyze three main approaches to caching in PHP: built-in mechanisms (APCu, OpCache), external servers Memcached and Redis. You will learn when to choose which tool and get ready-made code examples for implementation.
1. Built-in Solutions: OpCache and APCu
Let's start with the simplest — caching at the PHP engine level. Two key components are distinguished here.
1.1. OpCache (PHP opcode cache)
OpCache is built into PHP since version 5.5. It caches the compiled bytecode (opcode) of PHP scripts. Without it, each request re-parses and compiles the source code — this is a waste of resources. Enabling OpCache gives a speed increase of up to 30-50% without a single line of code.
Configuration in php.ini:
opcache.enable=1opcache.memory_consumption=128opcache.max_accelerated_files=4000opcache.revalidate_freq=601.2. APCu (Alternative PHP Cache user)
APCu is a cache for user data (variables, computation results). It works in RAM and is ideal for storing small, frequently requested data: configurations, translations, results of simple SQL queries.
Example of working with APCu:
<?php// Save data to cache for 300 secondsapcu_store('site_config', $config, 300);
// Get data from cache$cached = apcu_fetch('site_config');if ($cached === false) { // Data not found — load and save $cached = loadConfigFromDB(); apcu_store('site_config', $cached, 300);}
// Delete keyapcu_delete('site_config');?>Pros: does not require external services, easy to configure, minimal latency. Cons: works only on one server, data is lost when PHP-FPM restarts.
2. Memcached — Distributed Cache for Simple Data
Memcached is a high-performance distributed in-memory caching system. It is ideal for storing simple key-value pairs and sessions. Unlike APCu, Memcached runs as a separate server and can be shared across multiple web servers.
Installing the extension: pecl install memcached
Example of connection and operation:
<?php$memcached = new Memcached();$memcached->addServer('localhost', 11211);
// Set with TTL (time to live) of 600 seconds$memcached->set('user_123_profile', $profileData, 600);
// Get$profile = $memcached->get('user_123_profile');if ($memcached->getResultCode() === Memcached::RES_NOTFOUND) { // Cache miss — load from DB $profile = getUserProfileFromDB(123); $memcached->set('user_123_profile', $profile, 600);}
// Session caching (configuration in php.ini)// session.save_handler = memcached// session.save_path = "localhost:11211"?>When to use Memcached:
- Caching SQL query results (product lists, categories)
- Storing user sessions in a distributed environment
- Caching HTML blocks (e.g., menu or footer)
Limitations: no built-in data structures (only strings), maximum value size is 1 MB (can be increased), no persistence — cache is cleared when the server restarts.
3. Redis — Advanced Cache with Data Structures
Redis is not just a cache, but a full-fledged in-memory data store with support for complex types: lists, sets, hashes, sorted sets. It supports persistence (saving to disk),