protected function setUp() { if (!extension_loaded('redis')) { $this->markTestSkipped('Redis extension is not loaded'); } parent::setUp(); static::$kernel = static::createKernel(); static::$kernel->boot(); $container = static::$kernel->getContainer(); /** @var ManagerRegistry $doctrine */ $doctrine = $container->get('doctrine'); /** @var EntityManager $em */ $em = $doctrine->getManager(); // create database and load schema $schemaTool = new SchemaTool($em); $schemaTool->dropDatabase(); $schemaTool->createSchema($em->getMetadataFactory()->getAllMetadata()); /* @var EntityCache $entityCache */ $this->cache = $container->get('tree_house_cache.orm.entity_cache'); $this->cache->clear(); $this->ormCache = $em->getConfiguration()->getResultCacheImpl(); $this->entity = new EntityMock(1234); $this->entityCacheKey = $this->cache->getEntityKey($this->entity); $this->entityCacheClass = $this->cache->getEntityClassKey($this->entity); $em->persist($this->entity); $em->flush($this->entity); }
/** * @param string $entity * @param int $id * @param int|null $ttl * * @return object|null */ public function find($entity, $id, $ttl = null) { $query = $this->createQueryBuilder($entity, 'x')->select('x')->where('x.id = :id')->setParameter('id', $id)->getQuery(); if ($ttl !== false) { $key = sprintf($this->cache->getEntityKeyFormat(), $this->cache->getEntityClassKey($entity), $id); $query->useResultCache(true, $ttl, $key); } return $query->getOneOrNullResult(); }