Yii时间缓存无法正常工作?

时间:2013-02-19 10:05:47

标签: caching yii

我的YII版本:1.1.12 ...刮擦,我升级到1.1.13版本,但仍无效。

我试过了:

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

我看到了我存储的数据!但是,如果我试试这个:

Yii::app()->cache->set('someKey', $auctions, 10);
$data = Yii::app()->cache->get('someKey');
print_r( $data );
我什么都没看到?为什么YII忽略了我的时间间隔?我错过了什么?

**编辑**

我的缓存在config中定义为:

'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),
    ),
),

我知道Memcache正在运行,因为我在YII框架之外用这个例子测试了它:

$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"));

这个工作,项目缓存5秒......

2 个答案:

答案 0 :(得分:3)

似乎这是CMemCache.php中的一个错误。有这个功能:

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不希望有时间添加,所以我的快速解决方法是:

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

答案 1 :(得分:1)

好吧,确保$auctions定义良好。

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

确保配置正常并且您没有使用CDummyCache。我看起来像这样:

'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',
       ),
       ...
   ),