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;
private $cacheAdapter;
public static $CACHE_KEY = 'webservices.codification.search';
private $lists;
public function __construct($cacheAdapter, $webserviceCodeList)
{
$this->cacheAdapter = $cacheAdapter;
$this->webserviceCodeList = $webserviceCodeList;
}
public function getCodeList(string $listName)
{
if ($this->lists !== null) {
return $this->lists;
}
$cacheItem = $this->cacheAdapter->getItem(self::$CACHE_KEY);
if ($cacheItem->isHit()) {
$this->lists = $cacheItem->get();
return $this->lists;
}
$this->lists = $this->webserviceCodeList->getCodeList($listName);
$cacheItem->set($this->lists);
$this->cacheAdapter->save($cacheItem);
return $this->lists;
}
}
source
share