gpt4 book ai didi

php - Yii2 redis 缓存超过文件缓存

转载 作者:IT王子 更新时间:2023-10-29 06:05:20 25 4
gpt4 key购买 nike

我正在使用带有文件缓存和 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); 一起使用吗?我们可以通过 use yii\redis\Cache; 使用 redis 并使用

设置
$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 中使用时可用属性和方法的完整列表的文档。

关于php - Yii2 redis 缓存超过文件缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36787133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com