I have an application that uses php with Yii2 as its REST API backend.
I code on a machine running Windows 8 64bit and WAMP 2.5 with a local instance of memcached
and test on an Amazon EC2 instance running ubuntu that uses Amazon ElastiCache and running apache2
.
I found that using the built-in Yii2 cache component does not allow me to set or add objects to cache with an expiration duration on either installation BUT that if I use the PHP extension directly it works like a charm.
Code example:
$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.
I doubt there's something I have to do to enable timed caching in Yii2.
For reference, I used the instructions from the answer to this question in order to install and run my local memcached instance - How to enable memcache in WAMP but seeing as it also happens on my EC2 instance I doubt there's a problem with my installation.
Any ideas on why this does not work?