2014-08-28 39 views
1

我想在開發環境中禁用結果緩存。開發環境禁用結果緩存(Redis) - Symfony2

我不想評論緩存代碼或在開發環境中刪除它們。

有什麼辦法可以在dev env上禁用緩存?

我正在使用SNCRedisBundle & Predis for Symfony2 with Redis。

樣品單結果代碼:

$em = $this->container->get('doctrine')->getManager(); 
$predis = new \Snc\RedisBundle\Doctrine\Cache\RedisCache(); 
$predis->setRedis(new \Predis\Client()); 

$qb = $em->createQueryBuilder(); 
$qb 
    ->select('s') 
    ->from('CSSliderBundle:Slider', 's') 
    ->where($qb->expr()->eq('s.title', ':title')) 
    ->setParameter('title', $title); 

$slider = $qb 
    ->getQuery() 
    ->useResultCache(true, 3600 * 1.5) // added this line 
    ->setResultCacheDriver($predis) 
    ->setResultCacheLifetime(86400) 
    ->getOneOrNullResult(); 

而第二個問題:

有什麼辦法來清除插入/更新後的緩存教義內置的?我知道我可以使用lifecycleevents但我不知道如果有任何其他選項...

全部配置:

snc_redis: 
    clients: 
     default: 
      type: predis 
      alias: default 
      dsn: redis://localhost 
      logging: %kernel.debug% 
      options: 
       prefix: "%redis_prefix%" 
     cache: 
      type: predis 
      alias: cache 
      dsn: redis://localhost/1 
      logging: true 
      options: 
       prefix: "%redis_prefix%" 
     cluster: 
      type: predis 
      alias: cluster 
      dsn: 
       - redis://127.0.0.1/2 
       - redis://127.0.0.2/3 
       - redis://[email protected]/var/run/redis/redis-1.sock/4 
       - redis://127.0.0.1:6379/5 
      options: 
       profile: 2.4 
       connection_timeout: 10 
       connection_persistent: true 
       read_write_timeout: 30 
       iterable_multibulk: false 
       throw_errors: true 
       cluster: Snc\RedisBundle\Client\Predis\Connection\PredisCluster 
     monolog: 
      type: predis 
      alias: monolog 
      dsn: redis://localhost/6 
      logging: false 
     options: 
      connection_persistent: true 
    session: 
     client: default 
     use_as_default: true 
    doctrine: 
     metadata_cache: 
      client: cache 
      entity_manager: default 
      document_manager: default 
     result_cache: 
      client: cache 
      entity_manager: default 
      namespace: "doctrine_result_cache_%kernel.environment%_" 
     query_cache: 
      client: cache 
      entity_manager: default 
    monolog: 
     client: monolog 
     key: monolog 
    swiftmailer: 
     client: default 
     key: swiftmailer 

回答

0

你不需要每次注入的結果緩存IMPL教義。只須配置您snc_redis捆綁這樣的:

snc_redis: 
    # other options here.. 

    doctrine: 
     result_cache: 
      client: cache # your redis cahce_id connection 
      entity_manager: default # you may specify multiple entity_managers 
      namespace: "doctrine_result_cache_%kernel.environment%_" 

之後,您可以禁用它config_dev.yml

啓用$查詢緩存:

// public function useResultCache($bool, $lifetime = null, $resultCacheId = null) 
    $query->useResultCache(true, 3600 * 1.5); 

更多文檔:https://github.com/snc/SncRedisBundle/blob/master/Resources/doc/index.md#doctrine-caching

+0

沒有工作...在我添加了一條記錄後,仍然有相同的結果... – 2014-08-28 12:32:27

+0

@ R.CanserYanbakan,使用'$ query-> useResultCache(true,3600 * 1.5);'爲您的查詢。 – 2014-08-28 12:38:35

+0

兄弟,我會更新我的問題與您的代碼,但仍然不工作... – 2014-08-28 12:43:21