Asim Zaidi
Asim Zaidi

Reputation: 28324

cakephp redis storage besides sessions

I am trying to use redis as my caching engine and I have so far was able to add the add redis in cakephp and it writes my sessions to my redis just fine. This is what I did in the core

$engine = 'Redis';

$prefix = 'myapp_';

Cache::config('session', array(
    'engine' => $engine,
    'prefix' => $prefix . 'cake_session_',
    'duration' => $duration
));

Configure::write('Session', array(
    'defaults' => 'cache',
    'timeout' => 100,
    'start' => true,
    'checkAgent' => false,
    'handler' => array(
        'config' => 'session'
    )
));

Now I want to be able to write to redis from my controller some cached queries but I am not able to save that in redis. Instead it saves it to some default (I have no idea where) cache.

This is what I did in my controller

 public function index()
    {
       Cache::write("TestKey","myquery","default"); 
       var_dump(Cache::read("TestKey")); die;      
    }

The above gives me the value myquery on the browser but when I go to my redis-cli and look for keys * I do not see that key there. So obviously its saving somewhere else. I am not sure how to make it to write to redis. I have tried this Cache::write("TestKey","myquery","Redis"); but that also did not work. Thanks for help in advance

**EDIT 1 **

I just did some debugging and it looks like its trying to add to file

object(FileEngine)[3]
  protected '_File' => null
  public 'settings' => 
    array (size=10)
      'engine' => string 'File' (length=4)
      'path' => string '/Library/WebServer/Documents/phoneapp_backend/app/tmp/cache/' (length=60)
      'prefix' => string 'cake_' (length=5)
      'lock' => boolean true
      'serialize' => boolean true
      'isWindows' => boolean false
      'mask' => int 436
      'duration' => int 3600
      'probability' => int 100
      'groups' => 
        array (size=0)
          empty
  protected '_init' => boolean true
  protected '_groupPrefix' => null

How do I change it to write to redis?

Thanks

Upvotes: 0

Views: 1395

Answers (1)

You need to configure the default cache configuration to use Redis insteado of File. In app/Config/bootstrap.php Change

Cache::config('default', array('engine' => 'File'));

So it reads:

Cache::config('default', array('engine' => 'Redis'));

You may need to add more options to the configuration for specifying the server and port where Redis is running.

Upvotes: 2

Related Questions