2

I was trying to configure memcached in DoctrineORM module of my Zendframework 2 project to perform caching queries, results and metadata follow this tutorial https://github.com/doctrine/DoctrineORMModule/blob/master/docs/cache.md In 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'
                ),
            ),
        ),
    ),
);

and after I create a method as the following:

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();
}

and after, I get a Doctrine\ORM\Cache\CacheException with message:

Entity "Application\Entity\User" not configured as part of the second-level cache.

Anyone who can suggest for me to solved this problem?

1 Answers1

0

Add this to the USER class:

 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
Simon Berton
  • 468
  • 5
  • 13