rockstardev
rockstardev

Reputation: 13537

Yii time caching not working?

My version of YII: 1.1.12... scratch that, I upgraded to version 1.1.13, and still doesn't work.

I tried this:

Yii::app()->cache->set('someKey', $auctions);
$data = Yii::app()->cache->get('someKey');
print_r($data);

And I see the data that I stored! However, if I try this:

Yii::app()->cache->set('someKey', $auctions, 10);
$data = Yii::app()->cache->get('someKey');
print_r( $data );

I see nothing? Why is YII ignoring my time interval? What am I missing?

** EDIT **

My caching is defined in config as:

'cache'=>array(
  'class'=>'system.caching.CMemCache',
    'useMemcached'=>false,
    'servers'=>array(
      array( 'host'=>'127.0.0.1', 'port'=> 11211, 'weight'=>60 ),
      //array('host'=>'server2', 'port'=>11211, 'weight'=>40),
    ),
),

I know the Memcache is working, because I've tested it with this example outside of the YII framework:

$memcache = new Memcache;
$memcache->connect("localhost",11211);
$tmp_object = new stdClass;
$tmp_object->str_attr = "test";
$memcache->set("mysupertest",$tmp_object,false,5);
var_dump($memcache->get("mysupertest"));

This works and the item is cached for 5 seconds...

Upvotes: 1

Views: 4725

Answers (2)

rockstardev
rockstardev

Reputation: 13537

It seems like it's a bug in CMemCache.php. There is this function:

protected function setValue($key,$value,$expire)
{
  if($expire>0)
    $expire+=time();
  else
    $expire=0;

  return $this->useMemcached ? $this->_cache->set($key,$value,$expire) : $this->_cache->set($key,$value,0,$expire);
}

MemCache does not want the time to be added, so my quick fix is:

protected function setValue($key,$value,$expire)
{
  return $this->useMemcached ? $this->_cache->set($key,$value,$expire) : $this->_cache->set($key,$value,0,$expire);
}

Upvotes: 3

ddinchev
ddinchev

Reputation: 34673

Well, make sure $auctions are well defined.

Yii::app()->cache->set('someKey', array('someValue'), 120); // 120 means 2 minutes
print_r(Yii::app()->cache->get('someKey')); // you should see the array with the single value, I do see it when I try to run it

Make sure the config is OK and you are not using CDummyCache. Mine looks like this:

'components' => array(
       ...
       // Add a cache component to store data 
       // For demo, we are using the CFileCache, you can use any 
       // type your server is configured for. This is the simplest as it
       // requires no configuration or setup on the server.
       'cache' => array (
           'class' => 'system.caching.CFileCache',
       ),
       ...
   ),

Upvotes: 1

Related Questions