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 Query $query * @param int|null $ttl * @param string|null $key * * @return array */ public function query(Query $query, $ttl = null, $key = null) { if (is_null($key)) { $key = $this->getQueryCacheKey($query); } if ($ttl !== false) { $query->useResultCache(true, $ttl, $key); } $hasKey = $this->cache->has($key); // load via entitymanager $res = $query->getResult(); // remember which queries contain these entities if (!empty($res) && $hasKey === false && $ttl !== false) { $this->cache->registerQueryForEntity($res[0], $key); foreach ($res as $entity) { $this->cache->registerQueryResult($entity, $key); } } return $res; }
/** * @param LifecycleEventArgs $args */ public function preRemove(LifecycleEventArgs $args) { $entity = $args->getEntity(); $this->entityCache->invalidateEntity($entity); $this->entityCache->invalidateEntityQueries($entity); }
public function testInvalidateAllOnRemove() { $this->cache->expects($this->once())->method('invalidateEntity')->with($this->entity); $this->cache->expects($this->once())->method('invalidateEntityQueries')->with($this->entity); $this->listener->preRemove($this->getLifecycleEventArgsMock()); }