Yii2 -不能使用缓存组件将过期时间的对象保存到memcached中


Yii2 - can't use cache component to save objects with expiration duration to memcached

我有一个使用php和Yii2作为REST API后端的应用程序。
在运行Windows 8 64bit and WAMP 2.5 with a local instance of memcached的机器上编写代码,在Amazon EC2 instance running ubuntu that uses Amazon ElastiCache and running apache2上进行测试。

我发现,使用内置的Yii2缓存组件不允许我设置或添加对象缓存与有效期的安装,但如果我使用PHP扩展直接像一个魅力。

代码示例:

$cacheObject = Yii::$app->cache;
$cacheObject->add('testKey','testValue',10);
if($cacheObject->add('testKey','testValue',10))
{
    return 'Something is wrong with Yii2 cache component!';
}
else
{
    return 'Timed caching with Yii2 works!';
}
// Above code never enters else clause.

$memcachedObject = new Memcached();
$memcachedObject->addServer(CACHE_ENDPOINT,CACHE_PORT);
$memcachedObject->add('testKey2','testValue2',10);
if($memcachedObject->add('testKey2','testValue2',10))
{
    return 'Something is wrong with memcached extension';
}
else
{
    return 'Timed caching with Memcached extension works!';
}
// This code always enters else clause.

我怀疑我必须做些什么来启用Yii2中的定时缓存。

作为参考,我使用了这个问题的答案中的说明,以便安装和运行我的本地memcached实例-如何在WAMP中启用memcache,但看到它也发生在我的EC2实例上,我怀疑我的安装有问题。

你知道为什么这不起作用吗?

正如您所知,PHP实现了memcached(服务器)与两个不同库的集成:

    memcache (PHP memcache)memcached (PHP memcached)

Yii2使用MemCache类中的useMemcached属性只暴露一个接口。

参见MemCache类中的代码:
protected function addValue($key, $value, $duration)
{
    $expire = $duration > 0 ? $duration + time() : 0;
    return $this->useMemcached ? $this->_cache->add($key, $value, $expire) : $this->_cache->add($key, $value, 0, $expire);
}

我认为有一个不匹配之间的你的php-lib的memcache/memcached(见phpinfo())你的Yii2配置(见useMemcached属性-默认值是false)。