Introduction: Why Caching is the Foundation of a Fast PHP Application
Every PHP developer sooner or later faces performance issues. When the database starts to slow down and pages take 3-5 seconds to load, the first thing that comes to mind is caching. This is not just "saving data somewhere," it's a whole philosophy of optimization.
In this article, we will cover all popular approaches to caching in PHP: from simple file cache to industrial solutions on Redis and Memcached. You will learn how to choose the right tool and avoid common mistakes. Let's go!
1. Built-in Caching Mechanisms in PHP
1.1. Opcode Caching: OPcache
Before talking about external storage, it's worth remembering that PHP itself can cache its code. The OPcache mechanism stores compiled scripts in RAM, saving PHP from having to parse and compile files each time.
Enabling OPcache in php.ini:
opcache.enable=1opcache.memory_consumption=128opcache.max_accelerated_files=10000opcache.revalidate_freq=2This is the first line of defense. Without OPcache, any other caching will be less effective.
1.2. APCu — User Cache in Memory
APCu (Alternative PHP Cache) is an extension for storing arbitrary data in the shared memory of the PHP process. It is ideal for small projects where distributed caching is not required.
Example of working with APCu:
<?php// Save data for 5 minutesapcu_store('my_key', ['data' => 'value'], 300);
// Read data$data = apcu_fetch('my_key');if ($data === false) { echo "Cache not found, generating again";}
// Delete keyapcu_delete('my_key');?>Pros: lightning speed, simplicity. Cons: data lives only within a single server.
2. Memcached: Simplicity and Speed
2.1. What is Memcached and Why is it Needed
Memcached is a distributed in-memory caching system. It was created to speed up dynamic web applications by reducing the load on databases. Unlike APCu, Memcached runs as a separate server that can be connected to from different PHP instances.
2.2. Installing and Connecting Memcached
First, install the Memcached server:
sudo apt install memcachedsudo systemctl start memcachedThen install the PHP extension:
sudo pecl install memcachedNow you can work with the cache:
<?php$memcached = new Memcached();$memcached->addServer('localhost', 11211);
// Save for 10 minutes$memcached->set('user_123', ['name' => 'Ivan', 'email' => 'ivan@example.com'], 600);
// Read$user = $memcached->get('user_123');if ($user === false) { // Load from DB}?>2.3. When to Choose Memcached
- When you need a simple key-value store
- When data has a short lifespan (sessions, temporary data)
- When persistence is not required (Memcached does not save data to disk)
3. Redis: A Powerful Tool for Complex Scenarios
3.1. Why is Redis Better than Memcached?
Redis is not just a cache, it is a full-fledged in-memory database with support for complex data structures: lists, sets, hashes, bitmaps. Unlike Memcached, Redis supports:
- Persistence — data does not disappear on restart
- Transactions and Lua scripts
- Pub/Sub for message exchange
- Replication and clustering
3.2. Examples of Working with Redis in PHP
To work with Redis, use