Hello,
I have number of almost same websites, and for all them I use redis caching to cache queries.
But after some time even if all is cached, asking cache took some amount of time.
Keeping all that in mind, I tried to make some cuper-cache which will use file cache for caching redis cache.
So it first query file cache, if it is there then use it.
Otherwise query redis cache, and after value is found store it in file-cache also.
But somewhere there is an error, after redis cache is cached locally, it got returned as json string instead of array for example.
Does anybody has idea where is the error, or maybe advice how to improve this caching solutiuon. Code is below:
class Cache extends \yii\caching\Cache
{
/** @var \yii\redis\Cache */
public $redisCache;
/** @var FileCache */
public $fileCache;
public function init()
{
parent::init();
$this->redisCache = Yii::$app->redisCache;
$this->fileCache = Yii::$app->fileCache;
}
/**
* Retrieves a value from cache with a specified key.
* First try file cache and if it fails try redis cache and store to file cache
*
* @param string $key a unique key identifying the cached value
*
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
$value = $this->fileCache->getValue($key);
if ($value !== false) {
return $value;
}
$value = $this->redisCache->getValue($key);
$this->fileCache->set($key, $value);
return $value;
}
/**
* Stores a value identified by a key in cache.
* Save ion both caches
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
*
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $duration)
{
$this->fileCache->setValue($key, $value, $duration);
return $this->redisCache->setValue($key, $value, $duration);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
*Store in both caches
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
*
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $duration)
{
$this->fileCache->addValue($key, $value, $duration);
return $this->redisCache->addValue($key, $value, $duration);
}
/**
* Deletes a value with the specified key from cache
* Delete from both
*
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key)
{
$this->fileCache->deleteValue($key);
return $this->redisCache->deleteValue($key);
}
/**
* Deletes all values from cache.
* Flush both.
*
* @return boolean whether the flush operation was successful.
*/
protected function flushValues()
{
$this->fileCache->flushValues();
return $this->redisCache->flushValues();
}
}