【问题标题】:Setup of Symfony 3.1 cache component with Redis使用 Redis 设置 Symfony 3.1 缓存组件
【发布时间】:2016-10-17 13:09:20
【问题描述】:

我尝试使用 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 的内容。

你能告诉我我在哪里犯了错误,或者给我一个提示可能是什么问题吗?

【问题讨论】:

  • 缺少$this->cacheAdapter->save($cachedResources);
  • 我不敢相信它是如此微不足道。谢谢@malcolm,您可以发表您的评论作为答案吗?我会接受的。它有效。
  • 谢谢 :) 很高兴能帮上忙 :)

标签: php symfony redis symfony-3.1 symfony-cache


【解决方案1】:

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

$cachedResources->set($resources);

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

【讨论】:

    猜你喜欢
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 2020-03-13
    • 2018-10-25
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    相关资源
    最近更新 更多