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

标签 symfony caching doctrine-orm redis predis

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

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

有什么方法可以在开发环境中禁用缓存吗?

我正在使用 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();

第二个问题:

有什么方法可以在插入/更新后使用 doctrine 内置清除缓存?我知道我可以使用 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://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

最佳答案

您不需要每次都向 doctrine 注入(inject)结果缓存实现。只需像这样配置您的 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

关于symfony - 在 Dev Env 上禁用结果缓存 (Redis) - Symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25547363/

相关文章:

php - 用 Twig 将两个数组相交

mysql - 如何改进 Doctriny 查询以检索大量数据?

postgresql - 主键上带有 PostgreSQL 序列的 Doctrine 更新模式

php - 缓存 php 生成的 javascript 页面

Symfony、学说 : how to disable Entity Listeners?

php - Silex是否支持自定义用户角色

c# - VssConnection 到 VSTS 总是提示输入凭据

rest - HTTP 缓存的授权检查

mysql - 更新架构时 Doctrine 添加现有外键(mysql)

php - 禁用 Doctrine 外键约束