Symfony 3: setting up cache component pools using Redis

I would like to use the new cache component for storing data in Redis.

I would like to configure pools with different data life.

Currently, I have 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 do not 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 as follows:

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;
}

}

+4
source share
1 answer

To open the pool as a service, you will need two additional parameters: nameand public, as shown below:

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 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 specifying their name:

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

: http://symfony.com/doc/current/reference/configuration/framework.html#public

!

0

Source: https://habr.com/ru/post/1675187/


All Articles