Reputation: 69
I am using a yii2 with file cache and redis cache also. In my main config file redis cache settings are defined.
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'MY_IP',
'port' => MY_PORT,
'database' => 0,
],
I also added a component under file cache setting.
'cache' => [
'class' => 'yii\caching\FileCache',
],
so for a caching i'm using a $cache = Yii::$app->cache;
and for setting a cache Yii::$app->cache->set($id, $value, $time);
and get using Yii::$app->cache->get($id);
so is this is set the value from file cache or it is override the filecache and using the redis over it.
If this is using the filecache so how we override the filecache with redis .Can we use the redis cache with this Yii::$app->cache->get($id);
or we can use the redis with the use yii\redis\Cache;
and set using the
$redis->hmset('test_collection', 'key1', 'val1', 'key2', 'val2');
Upvotes: 2
Views: 2727
Reputation: 7886
Yes you can by simply setting the $cache
property to this:
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => 'redis' // id of the connection component as it is already defined
];
In my code I'm using it this way:
$cache = Yii::$app->cache;
$cache->add($access_token, ['id' => Yii::$app->user->id], $expire);
$user = $cache->get($access_token);
I've also noticed that some components are already using it like the urlManager which started storing the generated rules in the redis DB. See yii\redis\Cache docs for full list of available properties and methods when using within $cache.
Upvotes: 3