Symfony 3 : Configure cache component pools with Redis
Asked Answered
V

1

7

I'd like to use the new Cache Component to store datas in Redis.

I'd like to configure pools with different lifetime of data.

Right now, I configured :

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

But I don't know how to use the app.cache.codification pool in my code. I declared the following service :

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

And I use it like this :

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;
    }
}
Valerle answered 19/4, 2017 at 15:36 Comment(2)
Do you get any errors using it this way? Is something not working? What do you expect to be happening?Ratchford
I have no errors, but I'd like to be able to choose between the 2 pools app.cache.codification and app.cache.another_pool, each having different lifetime. I don't know how to do that.Valerle
T
4

To expose a pool as a service you need two extra options: name and public, as follows:

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

Now you can use both pools as services by referencing their name:

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

More details about the cache options: http://symfony.com/doc/current/reference/configuration/framework.html#public

Cheers!

Thayer answered 2/5, 2017 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.