首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在设备环境- Symfony2上禁用结果缓存(Redis)

在设备环境- Symfony2上禁用结果缓存(Redis)
EN

Stack Overflow用户
提问于 2014-08-28 19:09:16
回答 1查看 2.8K关注 0票数 1

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

我不想注释缓存代码,也不想在dev环境中删除它们。

有没有办法在dev环境下禁用缓存?

我在Symfony2和Redis上使用SNCRedisBundle & Predis。

单个结果代码示例:

代码语言:javascript
复制
$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();

第二个问题:

在insert / update之后,有没有办法使用doctrine内置来清除缓存?我知道我可以使用lifecycleevents,但我想知道是否有其他可用的选择……

完全配置:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2014-08-28 20:23:48

您不需要每次都将结果缓存impl注入到规则中。只需像这样配置您的snc_redis包:

代码语言:javascript
复制
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启用缓存:

代码语言:javascript
复制
    // 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25547363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档