public function testQueryCacheChain()
 {
     $name = 'my_query_region';
     $key = new QueryCacheKey('my_query_hash');
     $this->logger->setLogger('mock', $this->mock);
     $this->mock->expects($this->once())->method('queryCacheHit')->with($this->equalTo($name), $this->equalTo($key));
     $this->mock->expects($this->once())->method('queryCachePut')->with($this->equalTo($name), $this->equalTo($key));
     $this->mock->expects($this->once())->method('queryCacheMiss')->with($this->equalTo($name), $this->equalTo($key));
     $this->logger->queryCacheHit($name, $key);
     $this->logger->queryCachePut($name, $key);
     $this->logger->queryCacheMiss($name, $key);
 }
 /**
  * {@inheritdoc}
  */
 public function storeCollectionCache(CollectionCacheKey $key, $elements)
 {
     $targetPersister = $this->uow->getEntityPersister($this->targetEntity->rootEntityName);
     $targetRegion = $targetPersister->getCacheRegion();
     $targetHydrator = $targetPersister->getEntityHydrator();
     $entry = $this->hydrator->buildCacheEntry($this->targetEntity, $key, $elements);
     foreach ($entry->identifiers as $index => $identifier) {
         $entityKey = new EntityCacheKey($this->targetEntity->rootEntityName, $identifier);
         if ($targetRegion->contains($entityKey)) {
             continue;
         }
         $class = $this->targetEntity;
         $className = ClassUtils::getClass($elements[$index]);
         if ($className !== $this->targetEntity->name) {
             $class = $this->metadataFactory->getMetadataFor($className);
         }
         $entity = $elements[$index];
         $entityEntry = $targetHydrator->buildCacheEntry($class, $entityKey, $entity);
         $targetRegion->put($entityKey, $entityEntry);
     }
     $cached = $this->region->put($key, $entry);
     if ($this->cacheLogger && $cached) {
         $this->cacheLogger->collectionCachePut($this->regionName, $key);
     }
 }
 /**
  * @param string $targetEntity
  * @param object $element
  */
 protected function evictElementCache($targetEntity, $element)
 {
     /* @var $targetPersister CachedEntityPersister */
     $targetPersister = $this->uow->getEntityPersister($targetEntity);
     $targetRegion = $targetPersister->getCacheRegion();
     $key = new EntityCacheKey($targetEntity, $this->uow->getEntityIdentifier($element));
     $targetRegion->evict($key);
     if ($this->cacheLogger) {
         $this->cacheLogger->entityCachePut($targetRegion->getName(), $key);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function loadOneToManyCollection(array $assoc, $sourceEntity, PersistentCollection $coll)
 {
     $persister = $this->uow->getCollectionPersister($assoc);
     $hasCache = $persister instanceof CachedPersister;
     if ($hasCache) {
         $ownerId = $this->uow->getEntityIdentifier($coll->getOwner());
         $key = new CollectionCacheKey($assoc['sourceEntity'], $assoc['fieldName'], $ownerId);
         $list = $persister->loadCollectionCache($coll, $key);
         if ($list !== null) {
             if ($this->cacheLogger) {
                 $this->cacheLogger->collectionCacheHit($persister->getCacheRegion()->getName(), $key);
             }
             return $list;
         }
     }
     $list = $this->persister->loadOneToManyCollection($assoc, $sourceEntity, $coll);
     if ($hasCache) {
         $persister->storeCollectionCache($key, $list);
         if ($this->cacheLogger) {
             $this->cacheLogger->collectionCacheMiss($persister->getCacheRegion()->getName(), $key);
         }
     }
     return $list;
 }
 /**
  * {@inheritdoc}
  */
 public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = array())
 {
     if (!($key->cacheMode & Cache::MODE_GET)) {
         return null;
     }
     $entry = $this->region->get($key);
     if (!$entry instanceof QueryCacheEntry) {
         return null;
     }
     if (!$this->validator->isValid($key, $entry)) {
         $this->region->evict($key);
         return null;
     }
     $result = array();
     $entityName = reset($rsm->aliasMap);
     $hasRelation = !empty($rsm->relationMap);
     $persister = $this->uow->getEntityPersister($entityName);
     $region = $persister->getCacheRegion();
     $regionName = $region->getName();
     // @TODO - move to cache hydration component
     foreach ($entry->result as $index => $entry) {
         if (($entityEntry = $region->get($entityKey = new EntityCacheKey($entityName, $entry['identifier']))) === null) {
             if ($this->cacheLogger !== null) {
                 $this->cacheLogger->entityCacheMiss($regionName, $entityKey);
             }
             return null;
         }
         if ($this->cacheLogger !== null) {
             $this->cacheLogger->entityCacheHit($regionName, $entityKey);
         }
         if (!$hasRelation) {
             $result[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->resolveAssociationEntries($this->em), self::$hints);
             continue;
         }
         $data = $entityEntry->data;
         foreach ($entry['associations'] as $name => $assoc) {
             $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
             $assocRegion = $assocPersister->getCacheRegion();
             if ($assoc['type'] & ClassMetadata::TO_ONE) {
                 if (($assocEntry = $assocRegion->get($assocKey = new EntityCacheKey($assoc['targetEntity'], $assoc['identifier']))) === null) {
                     if ($this->cacheLogger !== null) {
                         $this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey);
                     }
                     $this->uow->hydrationComplete();
                     return null;
                 }
                 $data[$name] = $this->uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), self::$hints);
                 if ($this->cacheLogger !== null) {
                     $this->cacheLogger->entityCacheHit($assocRegion->getName(), $assocKey);
                 }
                 continue;
             }
             if (!isset($assoc['list']) || empty($assoc['list'])) {
                 continue;
             }
             $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
             $collection = new PersistentCollection($this->em, $targetClass, new ArrayCollection());
             foreach ($assoc['list'] as $assocIndex => $assocId) {
                 if (($assocEntry = $assocRegion->get($assocKey = new EntityCacheKey($assoc['targetEntity'], $assocId))) === null) {
                     if ($this->cacheLogger !== null) {
                         $this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey);
                     }
                     $this->uow->hydrationComplete();
                     return null;
                 }
                 $element = $this->uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), self::$hints);
                 $collection->hydrateSet($assocIndex, $element);
                 if ($this->cacheLogger !== null) {
                     $this->cacheLogger->entityCacheHit($assocRegion->getName(), $assocKey);
                 }
             }
             $data[$name] = $collection;
             $collection->setInitialized(true);
         }
         $result[$index] = $this->uow->createEntity($entityEntry->class, $data, self::$hints);
     }
     $this->uow->hydrationComplete();
     return $result;
 }
 /**
  * Load from second level cache or executes the query and put into cache.
  *
  * @param ArrayCollection|array|null $parameters
  * @param integer|null               $hydrationMode
  *
  * @return mixed
  */
 private function executeUsingQueryCache($parameters = null, $hydrationMode = null)
 {
     $rsm = $this->getResultSetMapping();
     $querykey = new QueryCacheKey($this->getHash(), $this->lifetime, $this->cacheMode ?: Cache::MODE_NORMAL);
     $queryCache = $this->_em->getCache()->getQueryCache($this->cacheRegion);
     $result = $queryCache->get($querykey, $rsm, $this->_hints);
     if ($result !== null) {
         if ($this->cacheLogger) {
             $this->cacheLogger->queryCacheHit($queryCache->getRegion()->getName(), $querykey);
         }
         return $result;
     }
     $result = $this->executeIgnoreQueryCache($parameters, $hydrationMode);
     $cached = $queryCache->put($querykey, $rsm, $result, $this->_hints);
     if ($this->cacheLogger) {
         $this->cacheLogger->queryCacheMiss($queryCache->getRegion()->getName(), $querykey);
         if ($cached) {
             $this->cacheLogger->queryCachePut($queryCache->getRegion()->getName(), $querykey);
         }
     }
     return $result;
 }