4
我想使用新的Cache Component在Redis中存儲數據。Symfony 3:使用Redis配置緩存組件池
我想配置具有不同數據生存期的池。
現在,我配置:
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
但我不知道如何使用app.cache.codification池在我的代碼。 我聲明瞭以下服務:
acme.cache.repository.code_list:
class: Acme\Cache\Repository\CodeList
public: false
arguments:
- "@cache.app"
- "@acme.webservice.repository.code_list"
我用它是這樣的:
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;
}
}
你有這種方式使用它的任何錯誤?有什麼不工作?你期望發生什麼? – Chausser
我沒有錯誤,但我希望能夠在2個「app.cache.codification」和「app.cache.another_pool」之間進行選擇,每個池有不同的生命週期。我不知道該怎麼做。 – melicerte