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

时间:2016-05-12 15:04:29

标签: caching doctrine-orm zend-framework2 memcached

我试图在我的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 \Doctrine\Common\Cache\MemcachedCache();
                $memcache = new Memcached();
                $memcache->addServer('localhost', 11211);
                $cache->setMemcached($memcache);
                return $cache;
            },
            'MemcachedFactory' => \Application\Service\Cache\MemcachedFactory::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' => [
                        'My\FirstRegion\Name' => [
                            'lifetime' => 800,
                            'lock_lifetime' => 1000
                        ],
                        'My\SecondRegion\Name' => [
                            'lifetime' => 10,
                            'lock_lifetime' => 20
                        ],
                    ],
                ],
            ),
        ),
        'driver' => array(
            '_driver' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'my_memcached',
                'paths' => array(__DIR__ . '/../src/Application/Entity')
            ),
            'orm_default' => array(
                'drivers' => array(
                    'Application\Entity' => '_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();
}

之后,我得到Doctrine\ORM\Cache\CacheException 消息:

  

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

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

1 个答案:

答案 0 :(得分:0)

将此添加到USER类:

<html>
<head>
<style>
p {
   border-radius:15px; 
   border:5px dotted blue;
}
</style>
<body bgcolor="tomato">
<h1> Hi! </h1>
<hr>
<p> Hey! This has a border! </p>
</body>
</html>