2017-04-19 11 views
4

新しいを使用して、データを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プールを使用する方法がわかりません。 、namepublic: は、私は、次のサービスに宣言:

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

をそして私はこのようにそれを使用する:あなたは2つの追加オプションが必要なサービスとしてプールを公開するには

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

}

+0

この方法でエラーが発生しますか?何か動作していないのですか?何が起こっていると思いますか? – Chausser

+0

私はエラーはありませんが、それぞれが異なる生涯を持つ 'app.cache.codification'と' app.cache.another_pool'の2つのプールの中から選択したいと考えています。私はそれをする方法を知らない。 – melicerte

答えて

0

次のようになります。

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 yoキャッシュオプションについて

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

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

乾杯uが自分の名前を参照することにより、サービスとして両方のプールを使用することができます!

関連する問題