我也在使用带有文件缓存和 redis 缓存的 yii2。在我的主配置文件中定义了 redis 缓存设置。
'redis' => [
'class' => 'yii'redis'Connection',
'hostname' => 'MY_IP',
'port' => MY_PORT,
'database' => 0,
],
我还在文件缓存设置下添加了一个组件。
'cache' => [
'class' => 'yii'caching'FileCache',
],
因此,对于缓存,我使用的是$cache = Yii::$app->cache;
,并且设置缓存Yii::$app->cache->set($id, $value, $time);
并使用Yii::$app->cache->get($id);
,因此这是从文件缓存中设置值,还是覆盖文件缓存并使用 Redis 在其上。
如果这是使用文件缓存,那么我们如何用 redis 覆盖文件缓存 .我们可以将 redis 缓存与此Yii::$app->cache->get($id);
一起使用,或者我们可以将 redis 与 use yii'redis'Cache;
$redis->hmset('test_collection', 'key1', 'val1', 'key2', 'val2');
是的,您只需将 $cache
属性设置为以下内容即可:
'cache' => [
'class' => 'yii'redis'Cache',
'redis' => 'redis' // id of the connection component as it is already defined
];
在我的代码中,我以这种方式使用它:
$cache = Yii::$app->cache;
$cache->add($access_token, ['id' => Yii::$app->user->id], $expire);
$user = $cache->get($access_token);
我还注意到一些组件已经在使用它,例如 urlManager,它开始将生成的规则存储在 redis 数据库中。请参阅 yii''redis''Cache 文档,了解在 $cache 中使用时可用属性和方法的完整列表。