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;
}
}
app.cache.codification
andapp.cache.another_pool
, each having different lifetime. I don't know how to do that. – Valerle