Caching in PHP: Redis, Memcached, and Other Tools
Caching is one of the most effective ways to speed up PHP web applications. Instead of executing heavy database queries or computing complex data every time, you save the result in a fast storage. In this article, we will cover the main caching tools in PHP: Redis, Memcached, APCu, and simple file caching. You will learn when to choose which tool and how to use it correctly.
Why is caching needed?
Imagine your website displays a list of the latest news. Without caching, each user forces PHP to execute an SQL query, process the data, and generate HTML. If 1000 people visit the site per minute, the database experiences a huge load. A cache allows you to save the result for 5–10 minutes and serve it instantly.
Main use cases for caching in PHP:
- Results of heavy SQL queries
- HTML fragments of pages
- Data from API requests to external services
- User sessions
- Configurations and settings
1. Redis — The King of Caching
Redis is an in-memory data store operating on a key-value principle. It supports complex data structures: lists, sets, hash tables. For PHP, there is the popular phpredis extension and the more modern Predis library.
Installation and Connection
Install Redis on the server, then install the extension via Composer:
composer require predis/predisExample of working with Redis in PHP
<?phprequire 'vendor/autoload.php';
use Predis\\Client;
$client = new Client([ 'scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379,]);
// Save data for 10 minutes$client->setex('news_list', 600, json_encode($newsData));
// Retrieve data$cached = $client->get('news_list');if ($cached) { $newsData = json_decode($cached, true);} else { // Query the database and save to cache}?>Advantages of Redis: high speed (microseconds), automatic expiration of stale keys (TTL), support for Pub/Sub, transactions. Disadvantage — it consumes RAM.
2. Memcached — Simplicity and Speed
Memcached is another in-memory store, but simpler than Redis. It only stores strings and does not support complex data structures. However, it is ideal for basic caching.
Example with Memcached
<?php$memcached = new Memcached();$memcached->addServer('127.0.0.1', 11211);
$key = 'user_profile_123';$data = $memcached->get($key);
if ($data === false) { // Data not in cache — retrieve from database $data = getUserProfileFromDB(123); $memcached->set($key, $data, 300); // 5 minutes}?>Memcached is faster than Redis for simple get/set operations, but lacks in functionality. It is suitable for caching query results and sessions.
3. APCu — Cache at the PHP Level
APCu (Alternative PHP Cache) is an extension that stores data directly in the memory of the PHP process. It does not require a separate server, but only works within a single server.
Example with APCu
<?php// Save value for 10 minutesapcu_store('site_config', $config, 600);
// Retrieve value$config = apcu_fetch('site_config');if ($config === false) { // Load config from file $config = parse_ini_file('config.ini'); apcu_store('site_config', $config, 600);}?>APCu is ideal for caching configurations, templates, and small data that rarely changes. The downside — data is lost when PHP or the web server is restarted.