Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3 : Configure cache component pools with Redis

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;
    }
}
like image 673
melicerte Avatar asked Apr 19 '17 15:04

melicerte


1 Answers

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!

like image 108
HELOstore Avatar answered Sep 30 '22 07:09

HELOstore