How to cache data in PHP (APCu, Redis, Filesystem, Managed caching services)

Caching is one of the simplest and most effective techniques to improve the performance of a PHP application. Instead of recalculating or fetching the same data on every request, the idea is to store it temporarily so it can be reused quickly.
Depending on your project’s needs, several caching solutions exist. The most common are APCu, Redis, filesystem caching, and, in recent years, managed cache services from our beloved hyperscalers. Each comes with its own advantages and limitations.
APCu: the fastest
APCu is a PHP extension that stores data directly in memory. Access is extremely fast since it requires neither disk I/O nor communication with an external server.
This solution is ideal for caching frequently used data, such as configuration parameters, country lists, or expensive computation results.
However, APCu has an important limitation: its cache is server-local. If your application runs on multiple machines behind a load balancer, each server will have its own cache. The data is therefore not shared, which limits functionality.
$key = "user_123";
$data = apcu_fetch($key);
if ($data === false) {
$data = (object)["name" => "John", "age" => 30];
apcu_store($key, $data, 3600);
}
var_dump($data);
Redis: the distributed cache
Redis is an in-memory data store capable of sharing data between multiple applications and multiple servers.
It is particularly well suited for high-traffic applications or distributed architectures. In addition to caching, Redis can also be used to store sessions, implement message queues, or handle rate limiting systems.
Access to Redis is slightly slower than APCu since it requires network communication, but performance remains excellent and more than sufficient for most projects (On Windows, I’ve already experienced unexplained 10ms latencies...).
On the downside, Redis requires additional setup and maintenance.
$redis = new Redis();
$redis->connect("127.0.0.1", 6379);
$key = "user_123";
$data = $redis->get($key);
if ($data === false) {
$data = json_encode((object)["name" => "John", "age" => 30]);
$redis->setex($key, 3600, $data);
}
var_dump(json_decode($data, true));
Filesystem caching
The simplest solution is to store data in files.
This approach requires no external service and no PHP extension. It works on almost all shared hosting environments and is perfectly suitable for small projects.
However, performance is lower than in-memory solutions. Disk read and write operations are slower, and managing a large number of files can quickly become complex.
That said, file-based caching remains an acceptable solution when APCu or Redis are not available, especially if you have relatively fast disks. You need to evaluate whether you actually gain something from it or not.
$key = "user_123";
$file = sys_get_temp_dir() . "/" . md5($key) . ".cache";
if (file_exists($file) && (time() - filemtime($file)) < 3600) {
$data = json_decode(file_get_contents($file), true);
} else {
$data = (object)["name" => "John", "age" => 30];
file_put_contents($file, json_encode($data));
}
var_dump($data);
Managed caching services
If your application is hosted on a public cloud such as AWS, Azure, Google Cloud, or OVHcloud, it is usually not necessary to install and manage your own Redis server. Major providers offer fully managed caching services such as Amazon ElastiCache, Azure Cache for Redis, or Google Cloud Memorystore.
These services handle high availability, backups, monitoring, and updates, allowing developers to focus on their application rather than infrastructure management.
Their main drawback is cost, which can be significant for small projects, but they are often the preferred choice for production-grade applications.
$cache = new CacheManager(
"your-azure-cache.redis.cache.windows.net",
6380,
"your-access-key"
);
$user = $cache->remember("user_123", function () {
return (object)[
"name" => "John",
"age" => 30
];
}, 3600);
var_dump($user);
Which solution should you choose?
There is no universal answer. The choice mainly depends on your project’s infrastructure.
For a single-server application, APCu is often the most performant solution for temporary data.
If your application is distributed across multiple servers or you need to share cache between services, Redis is usually the best choice.
If your application is deployed on a public cloud such as AWS, Azure, or OVHcloud, it is often better to use a managed caching service instead of running your own Redis server. These services handle high availability, backups, monitoring, and updates, which greatly simplifies production operations.
Finally, for a small website or limited hosting environment, filesystem caching remains a simple, reliable, and easy-to-implement solution.
Conclusion
Caching is often one of the simplest ways to improve the performance of a PHP application. A few lines of code are sometimes enough to significantly reduce response time and database load.
In practice, these solutions are often complementary. It is not uncommon to use APCu for frequently accessed local data and Redis to share cache across multiple servers.