public function testEvictAll()
 {
     $key1 = new CacheKeyMock('key.1');
     $key2 = new CacheKeyMock('key.2');
     $this->assertFalse($this->region->contains($key1));
     $this->assertFalse($this->region->contains($key2));
     $this->region->put($key1, new CacheEntryMock(array('value' => 'foo')));
     $this->region->put($key2, new CacheEntryMock(array('value' => 'bar')));
     $this->assertTrue($this->region->contains($key1));
     $this->assertTrue($this->region->contains($key2));
     $this->region->evictAll();
     $this->assertFalse($this->region->contains($key1));
     $this->assertFalse($this->region->contains($key2));
 }
 /**
  * {@inheritdoc}
  */
 public function storeCollectionCache(CollectionCacheKey $key, $elements)
 {
     /* @var $targetPersister CachedEntityPersister */
     $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 => $entityKey) {
         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);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function storeCollectionCache(CollectionCacheKey $key, $elements)
 {
     /* @var $targetPersister CachedEntityPersister */
     $associationMapping = $this->sourceEntity->associationMappings[$key->association];
     $targetPersister = $this->uow->getEntityPersister($this->targetEntity->rootEntityName);
     $targetRegion = $targetPersister->getCacheRegion();
     $targetHydrator = $targetPersister->getEntityHydrator();
     // Only preserve ordering if association configured it
     if (!(isset($associationMapping['indexBy']) && $associationMapping['indexBy'])) {
         // Elements may be an array or a Collection
         $elements = array_values(is_array($elements) ? $elements : $elements->getValues());
     }
     $entry = $this->hydrator->buildCacheEntry($this->targetEntity, $key, $elements);
     foreach ($entry->identifiers as $index => $entityKey) {
         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);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function put(CacheKey $key, CacheEntry $entry, Lock $lock = null)
 {
     $this->calls[__FUNCTION__][] = array('key' => $key, 'entry' => $entry);
     $this->throwException(__FUNCTION__);
     if (isset($this->locks[$key->hash])) {
         if ($lock !== null && $this->locks[$key->hash]->value === $lock->value) {
             return $this->region->put($key, $entry);
         }
         return false;
     }
     return $this->region->put($key, $entry);
 }
 /**
  * {@inheritdoc}
  */
 public function loadById(array $identifier, $entity = null)
 {
     $cacheKey = new EntityCacheKey($this->class->rootEntityName, $identifier);
     $cacheEntry = $this->region->get($cacheKey);
     $class = $this->class;
     if ($cacheEntry !== null) {
         if ($cacheEntry->class !== $this->class->name) {
             $class = $this->metadataFactory->getMetadataFor($cacheEntry->class);
         }
         if (($entity = $this->hydrator->loadCacheEntry($class, $cacheKey, $cacheEntry, $entity)) !== null) {
             if ($this->cacheLogger) {
                 $this->cacheLogger->entityCacheHit($this->regionName, $cacheKey);
             }
             return $entity;
         }
     }
     $entity = $this->persister->loadById($identifier, $entity);
     if ($entity === null) {
         return null;
     }
     $class = $this->class;
     $className = ClassUtils::getClass($entity);
     if ($className !== $this->class->name) {
         $class = $this->metadataFactory->getMetadataFor($className);
     }
     $cacheEntry = $this->hydrator->buildCacheEntry($class, $cacheKey, $entity);
     $cached = $this->region->put($cacheKey, $cacheEntry);
     if ($cached && ($this->joinedAssociations === null || count($this->joinedAssociations) > 0)) {
         $this->storeJoinedAssociations($entity);
     }
     if ($this->cacheLogger) {
         if ($cached) {
             $this->cacheLogger->entityCachePut($this->regionName, $cacheKey);
         }
         $this->cacheLogger->entityCacheMiss($this->regionName, $cacheKey);
     }
     return $entity;
 }
 /**
  * {@inheritdoc}
  */
 public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $hints = array())
 {
     if ($rsm->scalarMappings) {
         throw new CacheException("Second level cache does not support scalar results.");
     }
     if (count($rsm->entityMappings) > 1) {
         throw new CacheException("Second level cache does not support multiple root entities.");
     }
     if (!$rsm->isSelect) {
         throw new CacheException("Second-level cache query supports only select statements.");
     }
     if (isset($hints[Query::HINT_FORCE_PARTIAL_LOAD]) && $hints[Query::HINT_FORCE_PARTIAL_LOAD]) {
         throw new CacheException("Second level cache does not support partial entities.");
     }
     if (!($key->cacheMode & Cache::MODE_PUT)) {
         return false;
     }
     $data = array();
     $entityName = reset($rsm->aliasMap);
     $hasRelation = !empty($rsm->relationMap);
     $metadata = $this->em->getClassMetadata($entityName);
     $persister = $this->uow->getEntityPersister($entityName);
     if (!$persister instanceof CachedPersister) {
         throw CacheException::nonCacheableEntity($entityName);
     }
     $region = $persister->getCacheRegion();
     foreach ($result as $index => $entity) {
         $identifier = $this->uow->getEntityIdentifier($entity);
         $data[$index]['identifier'] = $identifier;
         $data[$index]['associations'] = array();
         if ($key->cacheMode & Cache::MODE_REFRESH || !$region->contains($entityKey = new EntityCacheKey($entityName, $identifier))) {
             // Cancel put result if entity put fail
             if (!$persister->storeEntityCache($entity, $entityKey)) {
                 return false;
             }
         }
         if (!$hasRelation) {
             continue;
         }
         // @TODO - move to cache hydration components
         foreach ($rsm->relationMap as $name) {
             $assoc = $metadata->associationMappings[$name];
             if (($assocValue = $metadata->getFieldValue($entity, $name)) === null || $assocValue instanceof Proxy) {
                 continue;
             }
             if (!isset($assoc['cache'])) {
                 throw CacheException::nonCacheableEntityAssociation($entityName, $name);
             }
             $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
             $assocRegion = $assocPersister->getCacheRegion();
             $assocMetadata = $assocPersister->getClassMetadata();
             // Handle *-to-one associations
             if ($assoc['type'] & ClassMetadata::TO_ONE) {
                 $assocIdentifier = $this->uow->getEntityIdentifier($assocValue);
                 if ($key->cacheMode & Cache::MODE_REFRESH || !$assocRegion->contains($entityKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocIdentifier))) {
                     // Cancel put result if association entity put fail
                     if (!$assocPersister->storeEntityCache($assocValue, $entityKey)) {
                         return false;
                     }
                 }
                 $data[$index]['associations'][$name] = array('targetEntity' => $assocMetadata->rootEntityName, 'identifier' => $assocIdentifier, 'type' => $assoc['type']);
                 continue;
             }
             // Handle *-to-many associations
             $list = array();
             foreach ($assocValue as $assocItemIndex => $assocItem) {
                 $assocIdentifier = $this->uow->getEntityIdentifier($assocItem);
                 if ($key->cacheMode & Cache::MODE_REFRESH || !$assocRegion->contains($entityKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocIdentifier))) {
                     // Cancel put result if entity put fail
                     if (!$assocPersister->storeEntityCache($assocItem, $entityKey)) {
                         return false;
                     }
                 }
                 $list[$assocItemIndex] = $assocIdentifier;
             }
             $data[$index]['associations'][$name] = array('targetEntity' => $assocMetadata->rootEntityName, 'type' => $assoc['type'], 'list' => $list);
         }
     }
     return $this->region->put($key, new QueryCacheEntry($data));
 }
 /**
  * {@inheritdoc}
  */
 public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $hints = [])
 {
     if ($rsm->scalarMappings) {
         throw new CacheException("Second level cache does not support scalar results.");
     }
     if (count($rsm->entityMappings) > 1) {
         throw new CacheException("Second level cache does not support multiple root entities.");
     }
     if (!$rsm->isSelect) {
         throw new CacheException("Second-level cache query supports only select statements.");
     }
     if (isset($hints[Query::HINT_FORCE_PARTIAL_LOAD]) && $hints[Query::HINT_FORCE_PARTIAL_LOAD]) {
         throw new CacheException("Second level cache does not support partial entities.");
     }
     if (!($key->cacheMode & Cache::MODE_PUT)) {
         return false;
     }
     $data = [];
     $entityName = reset($rsm->aliasMap);
     $rootAlias = key($rsm->aliasMap);
     $hasRelation = !empty($rsm->relationMap);
     $persister = $this->uow->getEntityPersister($entityName);
     if (!$persister instanceof CachedPersister) {
         throw CacheException::nonCacheableEntity($entityName);
     }
     $region = $persister->getCacheRegion();
     foreach ($result as $index => $entity) {
         $identifier = $this->uow->getEntityIdentifier($entity);
         $entityKey = new EntityCacheKey($entityName, $identifier);
         $data[$index]['identifier'] = $identifier;
         $data[$index]['associations'] = [];
         if ($key->cacheMode & Cache::MODE_REFRESH || !$region->contains($entityKey)) {
             // Cancel put result if entity put fail
             if (!$persister->storeEntityCache($entity, $entityKey)) {
                 return false;
             }
         }
         if (!$hasRelation) {
             continue;
         }
         // @TODO - move to cache hydration components
         foreach ($rsm->relationMap as $alias => $name) {
             $parentAlias = $rsm->parentAliasMap[$alias];
             $parentClass = $rsm->aliasMap[$parentAlias];
             $metadata = $this->em->getClassMetadata($parentClass);
             $assoc = $metadata->associationMappings[$name];
             $assocValue = $this->getAssociationValue($rsm, $alias, $entity);
             if ($assocValue === null) {
                 continue;
             }
             // root entity association
             if ($rootAlias === $parentAlias) {
                 // Cancel put result if association put fail
                 if (($assocInfo = $this->storeAssociationCache($key, $assoc, $assocValue)) === null) {
                     return false;
                 }
                 $data[$index]['associations'][$name] = $assocInfo;
                 continue;
             }
             // store single nested association
             if (!is_array($assocValue)) {
                 // Cancel put result if association put fail
                 if ($this->storeAssociationCache($key, $assoc, $assocValue) === null) {
                     return false;
                 }
                 continue;
             }
             // store array of nested association
             foreach ($assocValue as $aVal) {
                 // Cancel put result if association put fail
                 if ($this->storeAssociationCache($key, $assoc, $aVal) === null) {
                     return false;
                 }
             }
         }
     }
     return $this->region->put($key, new QueryCacheEntry($data));
 }