在Dev Env上禁用结果缓存(Redis) - Symfony2

时间:2014-08-28 11:09:16

标签: symfony caching doctrine-orm redis predis

我想在开发环境中禁用结果缓存。

我不想在开发环境中评论缓存代码或删除它们。

在dev env上有没有办法禁用缓存?

我正在使用SNCRedisBundle&使用Redis对Symfony2进行预测。

单个结果代码示例:

$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();

第二个问题:

使用doctrine内置插入/更新后有没有办法清除缓存?我知道我可以使用生命周期事件,但我想知道是否还有其他选择......

完整配置:

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://pw@/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

1 个答案:

答案 0 :(得分:0)

您不需要每次都将结果缓存impl注入doctrine。只需像这样配置你的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

禁用它

为$ query启用缓存:

    // 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