1

我的 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 忽略了我的时间间隔?我错过了什么?

** 编辑 **

我的缓存在配置中定义为:

'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 秒......

4

2 回答 2

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);
}
于 2013-02-19T11:25:23.503 回答
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',
       ),
       ...
   ),
于 2013-02-19T10:23:41.700 回答