Choosing a Yii 2 Cache Component: FileCache, APCu, or Redis
Guidance on selecting the right caching strategy for Yii 2 apps based on hosting, performance, and persistence needs.
29 Apr 2026, 08:19 UTC

Decision: Select a Yii 2 Cache Component
When building a Yii 2 application you must decide which cache storage to use for Yii::$app->cache. The choice affects performance, persistence, and deployment complexity.
Constraints to consider
- Hosting environment: shared hosting vs dedicated/VPS.
- Need for cache sharing across multiple PHP‑FPM workers or servers.
- Availability of PHP extensions (APCu, php‑redis) and willingness to manage a Redis server.
- Persistence requirements: survive PHP process restarts or server reboots.
Supported options
| Component | Persistence | Speed | Setup Complexity | Typical Use‑Case |
|---|---|---|---|---|
| FileCache | Disk (single server) | Moderate | Low | Simple dev or single‑server production |
| APCuCache | Shared memory (single server) | Very high | Medium (requires APCu extension) | High‑traffic single‑server apps |
| RedisCache | Remote Redis server | High (network latency) | Medium‑High (requires Redis server & php‑redis) | Distributed or multi‑server environments |
Trade-offs
FileCache needs no extra extension but is limited by disk I/O and cannot be shared between PHP‑FPM pools or multiple servers.
APCuCache provides the fastest in‑memory access, yet each PHP‑FPM pool maintains its own memory segment, so caches are not pooled across pools or servers.
RedisCache adds network round‑trip latency but enables a central cache that all workers can read/write, supports fine‑grained TTL, and survives server restarts as long as Redis persists.
Concrete implementation
Edit the application configuration, typically config/web.php (or config/main.php for advanced templates). You need write permission on the file.
return [
'components' => [
'cache' => [
// Choose one of the following blocks
// FileCache (default, no extra config needed)
// 'class' => 'yii\\caching\\FileCache',
// APCuCache – requires APCu extension enabled
// 'class' => 'yii\\apcu\\Cache',
// RedisCache – requires yiisoft/yii2-redis extension and a Redis server
'class' => 'yii\\redis\\Cache',
'redis' => [
'hostname' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
],
];
After saving the file, restart PHP‑FPM or Apache if you changed extensions.
Validation steps
- Enable the Yii debug toolbar (
YII_DEBUGtrue) and load any page. - In the debugger panel select Cache and verify that hit/miss counters change after executing a cached operation, e.g.
Yii::$app->cache->set('test', 'value', 10); echo Yii::$app->cache->get('test'); - To test TTL, wait longer than the expiry and confirm the get returns
null. - For a quick performance check, run a benchmark tool such as
ab -n 100 http://your-app/test-cachedbefore and after enabling the cache; a noticeable drop in average response time indicates the cache layer is active.
Limitations and practical checks
- APCu may clash with OPcache; ensure
apc.enabled=1andapc.enable_cli=0for web requests. - Missing the yiisoft/yii2-redis extension triggers a class not found error; install via Composer and verify
php -m | grep redis. - Network partitions can cause Redis read failures; the cache component will return
falseon get/set, so your application should handle cache miss gracefully.
Rollback is not required because changing the cache component only alters runtime behavior; to revert, replace the component block with the previous setting and reload the application.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.