php - Yii2 redis 缓存优于文件缓存

标签 php caching redis yii2

我正在使用带有文件缓存和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 覆盖文件缓存。我们可以通过此 Yii::$app->cache->get($id); 使用 redis 缓存吗?我们可以使用 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/

相关文章:

lua - 唯一的随 secret 钥 redis lua 脚本

caching - 如果正在使用,如何将 key 保留在redis中,如果不使用,如何过期?

php - 将数组值显示为数组中的数字

serialization - Redis 重组序列化字符串失败 - UTF-8

php - 下拉数据不发布 "The forum you are trying to create a topic on, does not exist!"

session - 在 Laravel 中使用 Redis 持久化闪存数据

java - Spring Cache 抽象 JDK 可选

caching - 缓存大小的容量规划

php - 使用个人账户的 Paypal 付款列表

php - 使用 PHP 数据库类作为单例有哪些缺点?