【问题标题】:Symfony 3 : Configure cache component pools with RedisSymfony 3:使用 Redis 配置缓存组件池
【发布时间】:2017-09-15 22:45:45
【问题描述】:

我想使用新的Cache Component 在 Redis 中存储数据。

我想配置具有不同数据生命周期的池。

现在,我配置了:

framework:
    cache:
        app: cache.adapter.redis
        default_redis_provider: "redis://localhost:6379"
        pools:
            app.cache.codification:
                adapter: cache.app
                default_lifetime: 86400
            app.cache.another_pool:
                adapter: cache.app
                default_lifetime: 600

但我不知道如何在我的代码中使用app.cache.codification 池。 我声明了以下服务:

acme.cache.repository.code_list:
    class: Acme\Cache\Repository\CodeList
    public: false
    arguments:
        - "@cache.app"
        - "@acme.webservice.repository.code_list"

我是这样使用它的:

class CodeList
{
    private $webserviceCodeList;

    /**
     * @var AbstractAdapter
     */
    private $cacheAdapter;

    public static $CACHE_KEY = 'webservices.codification.search';

    private $lists;

    /**
     * @param AbstractAdapter $cacheAdapter
     * @param WebserviceCodeList $webserviceCodeList
     */
    public function __construct($cacheAdapter, $webserviceCodeList)
    {
        $this->cacheAdapter = $cacheAdapter;
        $this->webserviceCodeList = $webserviceCodeList;
    }

    /**
     * @param string $listName
     * @return array
     */
    public function getCodeList(string $listName)
    {
        if ($this->lists !== null) {
            return $this->lists;
        }

        // Cache get item
        $cacheItem = $this->cacheAdapter->getItem(self::$CACHE_KEY);

        // Cache HIT
        if ($cacheItem->isHit()) {
            $this->lists = $cacheItem->get();
            return $this->lists;
        }

        // Cache MISS
        $this->lists = $this->webserviceCodeList->getCodeList($listName);
        $cacheItem->set($this->lists);
        $this->cacheAdapter->save($cacheItem);

        return $this->lists;
    }
}

【问题讨论】:

  • 你用这种方式有什么错误吗?有什么不工作吗?你预计会发生什么?
  • 我没有错误,但我希望能够在 app.cache.codificationapp.cache.another_pool 两个池之间进行选择,每个池都有不同的生命周期。我不知道该怎么做。

标签: symfony redis symfony-3.2


【解决方案1】:

要将池公开为服务,您需要两个额外的选项:namepublic,如下所示:

framework:
    cache:
        app: cache.adapter.redis
        default_redis_provider: "redis://localhost:6379"
        pools:
            app.cache.codification:
                name: app.cache.codification # this will be the service's name
                public: true # this will expose the pool as a service
                adapter: cache.app
                default_lifetime: 86400
            app.cache.another_pool:
                name: app.cache.another_pool
                public: true
                adapter: cache.app
                default_lifetime: 600

现在您可以通过引用它们的名称将这两个池用作服务:

acme.cache.repository.code_list:
    class: Acme\Cache\Repository\CodeList
    public: false
    arguments:
        - "@app.cache.codification" # pool's name
        - "@acme.webservice.repository.code_list"

有关缓存选项的更多详细信息:http://symfony.com/doc/current/reference/configuration/framework.html#public

干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-24
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2015-09-21
    • 2015-06-18
    相关资源
    最近更新 更多