php - 使用 Redis 设置 Symfony 3.1 缓存组件

标签 php symfony redis symfony-3.1 symfony-cache

我尝试使用 Redis 设置 Symfony 3.1 缓存。我遵循本教程:

https://symfony.com/blog/new-in-symfony-3-1-cache-component

我已经安装了 predis/predis 和 SncRedisBundle。

在我的 config.yml 中我放了

framework:
    cache:
        app: cache.adapter.redis
        default_redis_provider: redis://192.168.34.10
    snc_redis:
     clients:
        default:
            type: predis
            alias: default
            dsn: redis://192.168.34.10
            logging: %kernel.debug%
        cache:
            type: predis
            alias: cache
            dsn: redis://192.168.34.10
            logging: %kernel.debug%
            options:
                connection_timeout: 10
                read_write_timeout: 30

现在,当我尝试通过 snc_redis 访问 redis 时,它工作正常。但是当我尝试使用缓存组件时:

public function getLoggedUserAcl($userId)
{
    $cachedResources = $this->cacheAdapter->getItem('acl.rules');
    $cachedResources->expiresAfter(100);

    if ($cachedResources->isHit()) {
        dump('hit');
        $resources = $cachedResources->get();
    } else {
        dump('not-hit');
        $resources = $this->api->getCollection(Resource::class, null, null, [
            'userId' => $userId
        ]);

        $cachedResources->set($resources);
    }

    return $resources;
}

CacheAdapter 是 @cache.app 服务。

它总是转储 NOT_HIT。在日志中没有关于 REDIS 的内容。

你能告诉我我在哪里犯了错误或者给我一个提示可能是错的吗?

最佳答案

你在这里错过的最重要的事情是将结果保存到缓存:

$cachedResources->set($resources);

$this->cacheAdapter->save($cachedResources);

关于php - 使用 Redis 设置 Symfony 3.1 缓存组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40087277/

相关文章:

javascript - Laravel PUT 响应错误 419。 token 被添加到 header 中。我仍然没有得到结果

php - Mysql 公共(public)用户聊天与好友聊天

forms - 嵌入表单的持久性

php - Symfony Doctrine 冲洗实体

php - jQuery 将每个 PHP 响应视为状态为 0

php - symfony 中不同方法的空路由路径

php - 带有消息 'Unknown "的“Twig_Error_Syntax”渲染“过滤器

python - redis-py 有 NUMSUB 命令吗?

python - redis-py 管道 hset 不保存

scala - 使用 Scala 在 Redis 数据库中存储 Set 对象