Caching in PHP: Redis, Memcached, APCu, and File Caching

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Caching in PHP: Redis and Other Tools



Caching is one of the key mechanisms for optimizing the performance of PHP web applications. Properly configured caching can reduce server response time by 10–50 times, lower the load on the database, and decrease resource consumption. In this article, we will cover four main approaches to caching in PHP: Redis, Memcached, APCu, and classic file caching.



Why is caching needed in PHP?



Without caching, each PHP request can execute dozens of SQL queries, process templates, and load external data. Caching saves the result of an expensive operation (e.g., a database query or XML parsing) and serves it directly, bypassing repeated computations. Main use cases:



  • Caching SQL query results — save the result of a SELECT query for 5–60 seconds.
  • Caching HTML fragments — ready-made menu blocks, widgets, breadcrumbs.
  • Caching sessions — storing session data in Redis instead of files.
  • Caching API data — responses from external services (weather, exchange rates).
  • Opcode caching — speeding up the execution of PHP code itself (OPcache).


1. Redis — The King of In-Memory Caching



Redis is a high-performance in-memory key-value data store with support for complex data structures (lists, hashes, sets). For PHP, there are several clients: predis/predis (pure PHP) and ext-redis (C extension).



Installation example via Composer:

composer require predis/predis


Basic caching example with Redis:

<?phprequire 'vendor/autoload.php';

use Predis\\Client;

$redis = new Client([ 'scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379,]);

$cacheKey = 'user_list_page_1';$ttl = 300; // 5 minutes

// Try to get data from cacheif ($redis->exists($cacheKey)) { $data = unserialize($redis->get($cacheKey)); echo "Data from cache";} else { // Expensive operation (e.g., SQL query) $data = getUsersFromDatabase(1, 20); $redis->setex($cacheKey, $ttl, serialize($data)); echo "Data from DB";}

print_r($data);


Advantages of Redis: speed (milliseconds), TTL support, atomic operations, Pub/Sub, disk persistence. Disadvantages: requires a separate server, consumes RAM.



2. Memcached — Simplicity and Speed



Memcached is another popular in-memory store, but without complex data structures. In PHP, the memcached extension is used (note the letter d — this is the newer version).



Installing the extension:

sudo apt install php-memcached


Example of working with Memcached:

<?php$memcached = new Memcached();$memcached->addServer('127.0.0.1', 11211);

$key = 'article_42';$data = $memcached->get($key);

if ($memcached->getResultCode() === Memcached::RES_NOTFOUND) { $data = fetchArticleFromDB(42); $memcached->set($key, $data, 3600); // 1 hour echo "Query to DB";} else { echo "From Memcached cache";}


When to choose Memcached: if you need simple string caching without complex structures and don't want to configure persistence. Memcached is faster than Redis for simple get/set operations, but lags in functionality.



3. APCu — Caching Directly in PHP



APCu (Alternative PHP Cache) is an extension for caching data in the shared memory of the current PHP process. It does not require a separate

Recommendations