Using Redis Cache in Yii 1.x (Production Setup + Tips)

You are viewing revision #2 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#1)

Hey everyone,

I’m working on a Yii 1.x–based ERP/POS system and recently implemented Redis caching for performance optimization. Thought I’d share my setup and a few lessons learned in case it helps someone still maintaining Yii 1.x apps.

My Setup ¶
'components'=>array(
    'cache'=>array(
        'class'=>'CRedisCache',
        'hostname'=>'127.0.0.1',
        'port'=>6379,
        'database'=>1,
        'keyPrefix'=>'database',
    ),
),
Why Redis Instead of FileCache? ¶
  • Much faster read/write (RAM-based)
  • Great for high-traffic POS/ERP environments
  • Reduces DB load significantly
  • Works well for session + query caching
✅ Where I’m Using Cache ¶
  • Product listing queries (heavy joins)
  • Dashboard stats (sales, stock, reports)
  • API responses for branch sync
  • Session handling (planning to shift fully to Redis)
Things to Watch Out For ¶
  1. Key Prefix is Important If you’re running multiple apps on same Redis instance, always use keyPrefix to avoid conflicts.

  2. Cache Invalidation Yii 1.x doesn’t auto-handle this well. You need to manually clear cache when:

    • product updates
    • stock changes
    • price updates
  3. Persistence Redis is in-memory. Make sure:

    • RDB or AOF is enabled (depending on your setup)
    • Otherwise you risk data loss on restart
  4. Production Deployment Don’t keep Redis on default config:

    • bind to private IP
    • use password (requirepass)
    • firewall the port
Example Usage ¶
$key = 'product_list';

$data = Yii::app()->cache->get($key);

if ($data === false) {
    $data = Product::model()->findAll();
    Yii::app()->cache->set($key, $data, 300); // cache for 5 minutes
}
Question for Community ¶

For those still on Yii 1.x:

  • Are you using Redis for sessions as well?
  • Any best practices for automatic cache invalidation?

Would love to hear how others are optimizing legacy Yii apps in production.

Thanks!

0 0
1 follower
Viewed: 58 times
Version: 1.1
Category: Tips
Last updated by: AftabHussainSharSukkur AftabHussainSharSukkur
Created on: Apr 21, 2026
Last updated: 7 hours ago
Update Article

Revisions

View all history

Related Articles