php - Symfony2 Doctrine 元数据缓存与 Redis 问题

标签 php symfony caching doctrine-orm redis

我正在尝试将 Redis 用作缓存原则元数据、查询和结果的驱动程序。以下是我的配置。

auto_generate_proxy_classes: "%kernel.debug%"
    naming_strategy: doctrine.orm.naming_strategy.underscore
    auto_mapping: true
    result_cache_driver:
        type: redis
        host: %redis_host%
        instance_class: Redis
    query_cache_driver: redis
    #metadata_cache_driver: redis

当我从 #metadata_cache_driver: redis 行删除注释时,我在运行测试时遇到错误,并出现以下错误。

TypeError: Argument 1 passed to Doctrine\ORM\Mapping\ClassMetadataFactory::wakeupReflection() must implement interface Doctrine\Common\Persistence\Mapping\ClassMetadata, string given, called in vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php on line 214

我的功能测试如下所示:

public function testX()
{
    //The data in prepared in setup..
    $param1 = 'test-id';
    $param2 = 'test-key';
    $result = $this->em->getRepository('MyBundle:Test')
            ->findOneByXX($param1, $param2);
    $this->assertTrue($result instanceof Test);
}

我的查询如下所示:

$qb->select('c')
       ->from('MyBundle:Test', 'c')
       ->where('c.id = :id')
       ->andWhere('c.key = :key')
       ->setParameter('id', $id)
       ->setParameter('key', $key);

    $query = $qb->getQuery()
            ->useResultCache(true);

    return $query->getOneOrNullResult();

Redis 是否需要额外配置?任何帮助将不胜感激??

最佳答案

我认为要解决此问题,您需要为 redis 设置序列化程序,默认序列化程序可能不知道 PHP 类,当对象从缓存中删除并反序列化时,它与原来的类型不同在序列化之前。

$redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);

对于您的情况,您可能需要将配置选项设置为驱动程序配置的一部分。

关于php - Symfony2 Doctrine 元数据缓存与 Redis 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34659836/

相关文章:

php - 如何使用php取消stripe api中的SubscriptionSchedule?

php - 如何在 RAD PHP XE2 中升级 PHP

php - php 中的 sql 查询

php - Symfony 自动加载命名空间问题

ios - 在 iOS 上,由于 dataWithContentsOfURL :options:error: has an option to not cache data, 这是否意味着 dataWithContentsOfURL 缓存数据?

php - 项目编码标准与单元测试代码覆盖率冲突怎么办?

php - 交响乐2 : Translate to default locale

Symfony2 bundle 未注册

caching - 什么是 TLB 击落?

caching - Guava Cache - maximumSize - 驱逐(LRU 或最常见)