如何在DoctrineORMModule中将实体配置为二级缓存的一部分



我试图在Zendframework 2项目的DoctrineORM模块中配置memcached,以执行缓存查询、结果和元数据。请遵循本教程https://github.com/doctrine/DoctrineORMModule/blob/master/docs/cache.md在module.config.php 中

<?php
return array(
    'service_manager' => array(
        'invokables' => array(
//            ...
        ),
        'factories' => array(
            'doctrine.cache.my_memcached' => function ($sm) {
                $cache = new DoctrineCommonCacheMemcachedCache();
                $memcache = new Memcached();
                $memcache->addServer('localhost', 11211);
                $cache->setMemcached($memcache);
                return $cache;
            },
            'MemcachedFactory' => ApplicationServiceCacheMemcachedFactory::class,
        )
    ),
    'doctrine' => array(
        'cache' => array(
            'memcached' => array(
                'namespace' => '_Doctrine',
                'instance' => 'MemcachedFactory',
            ),
        ),
        'configuration' => array(
            'orm_default' => array(
                'query_cache' => 'memcached',
                'result_cache' => 'memcached',
                'metadata_cache' => 'memcached',
                'hydration_cache' => 'memcached',
                'second_level_cache' => [
                    'enabled' => true,
                    'default_lifetime' => 200,
                    'default_lock_lifetime' => 500,
                    'file_lock_region_directory' => './data/cache',
                    'regions' => [
                        'MyFirstRegionName' => [
                            'lifetime' => 800,
                            'lock_lifetime' => 1000
                        ],
                        'MySecondRegionName' => [
                            'lifetime' => 10,
                            'lock_lifetime' => 20
                        ],
                    ],
                ],
            ),
        ),
        'driver' => array(
            '_driver' => array(
                'class' => 'DoctrineORMMappingDriverAnnotationDriver',
                'cache' => 'my_memcached',
                'paths' => array(__DIR__ . '/../src/Application/Entity')
            ),
            'orm_default' => array(
                'drivers' => array(
                    'ApplicationEntity' => '_driver'
                ),
            ),
        ),
    ),
);

然后我创建了一个方法如下:

public function findById($id) {
    $qb = $this->entityManager->createQueryBuilder();
    $qb->select("u")
            ->from(User::class, "u")
            ->where($qb->expr()->eq("u.id", ":id"))
            ->setParameter("id", (int) $id);
    return $qb->getQuery()->setCacheable(true)->getOneOrNullResult();
}

然后,我得到一个DoctrineORMCacheCacheException带有消息:

实体"Application\Entity\User"未配置为二级缓存的一部分。

有谁能建议我解决这个问题吗?

将其添加到USER类:

 * @ORMCache(usage="NONSTRICT_READ_WRITE")

相关内容

  • 没有找到相关文章

最新更新