Exception
9 months ago
Logging
9 months ago
Persister
9 months ago
Region
9 months ago
AssociationCacheEntry.php
9 months ago
CacheConfiguration.php
9 months ago
CacheEntry.php
9 months ago
CacheException.php
9 months ago
CacheFactory.php
9 months ago
CacheKey.php
9 months ago
CollectionCacheEntry.php
9 months ago
CollectionCacheKey.php
9 months ago
CollectionHydrator.php
9 months ago
ConcurrentRegion.php
9 months ago
DefaultCache.php
9 months ago
DefaultCacheFactory.php
9 months ago
DefaultCollectionHydrator.php
9 months ago
DefaultEntityHydrator.php
9 months ago
DefaultQueryCache.php
9 months ago
EntityCacheEntry.php
9 months ago
EntityCacheKey.php
9 months ago
EntityHydrator.php
9 months ago
Lock.php
9 months ago
LockException.php
9 months ago
MultiGetRegion.php
9 months ago
QueryCache.php
9 months ago
QueryCacheEntry.php
9 months ago
QueryCacheKey.php
9 months ago
QueryCacheValidator.php
9 months ago
Region.php
9 months ago
RegionsConfiguration.php
9 months ago
TimestampCacheEntry.php
9 months ago
TimestampCacheKey.php
9 months ago
TimestampQueryCacheValidator.php
9 months ago
TimestampRegion.php
9 months ago
index.php
9 months ago
DefaultCache.php
185 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Cache; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\ORM\Cache; |
| 6 | use MailPoetVendor\Doctrine\ORM\Cache\Persister\CachedPersister; |
| 7 | use MailPoetVendor\Doctrine\ORM\EntityManagerInterface; |
| 8 | use MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadata; |
| 9 | use MailPoetVendor\Doctrine\ORM\ORMInvalidArgumentException; |
| 10 | use MailPoetVendor\Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; |
| 11 | use MailPoetVendor\Doctrine\ORM\UnitOfWork; |
| 12 | use function is_array; |
| 13 | use function is_object; |
| 14 | class DefaultCache implements Cache |
| 15 | { |
| 16 | private $em; |
| 17 | private $uow; |
| 18 | private $cacheFactory; |
| 19 | private $queryCaches = []; |
| 20 | private $defaultQueryCache; |
| 21 | public function __construct(EntityManagerInterface $em) |
| 22 | { |
| 23 | $this->em = $em; |
| 24 | $this->uow = $em->getUnitOfWork(); |
| 25 | $this->cacheFactory = $em->getConfiguration()->getSecondLevelCacheConfiguration()->getCacheFactory(); |
| 26 | } |
| 27 | public function getEntityCacheRegion($className) |
| 28 | { |
| 29 | $metadata = $this->em->getClassMetadata($className); |
| 30 | $persister = $this->uow->getEntityPersister($metadata->rootEntityName); |
| 31 | if (!$persister instanceof CachedPersister) { |
| 32 | return null; |
| 33 | } |
| 34 | return $persister->getCacheRegion(); |
| 35 | } |
| 36 | public function getCollectionCacheRegion($className, $association) |
| 37 | { |
| 38 | $metadata = $this->em->getClassMetadata($className); |
| 39 | $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); |
| 40 | if (!$persister instanceof CachedPersister) { |
| 41 | return null; |
| 42 | } |
| 43 | return $persister->getCacheRegion(); |
| 44 | } |
| 45 | public function containsEntity($className, $identifier) |
| 46 | { |
| 47 | $metadata = $this->em->getClassMetadata($className); |
| 48 | $persister = $this->uow->getEntityPersister($metadata->rootEntityName); |
| 49 | if (!$persister instanceof CachedPersister) { |
| 50 | return \false; |
| 51 | } |
| 52 | return $persister->getCacheRegion()->contains($this->buildEntityCacheKey($metadata, $identifier)); |
| 53 | } |
| 54 | public function evictEntity($className, $identifier) |
| 55 | { |
| 56 | $metadata = $this->em->getClassMetadata($className); |
| 57 | $persister = $this->uow->getEntityPersister($metadata->rootEntityName); |
| 58 | if (!$persister instanceof CachedPersister) { |
| 59 | return; |
| 60 | } |
| 61 | $persister->getCacheRegion()->evict($this->buildEntityCacheKey($metadata, $identifier)); |
| 62 | } |
| 63 | public function evictEntityRegion($className) |
| 64 | { |
| 65 | $metadata = $this->em->getClassMetadata($className); |
| 66 | $persister = $this->uow->getEntityPersister($metadata->rootEntityName); |
| 67 | if (!$persister instanceof CachedPersister) { |
| 68 | return; |
| 69 | } |
| 70 | $persister->getCacheRegion()->evictAll(); |
| 71 | } |
| 72 | public function evictEntityRegions() |
| 73 | { |
| 74 | $metadatas = $this->em->getMetadataFactory()->getAllMetadata(); |
| 75 | foreach ($metadatas as $metadata) { |
| 76 | $persister = $this->uow->getEntityPersister($metadata->rootEntityName); |
| 77 | if (!$persister instanceof CachedPersister) { |
| 78 | continue; |
| 79 | } |
| 80 | $persister->getCacheRegion()->evictAll(); |
| 81 | } |
| 82 | } |
| 83 | public function containsCollection($className, $association, $ownerIdentifier) |
| 84 | { |
| 85 | $metadata = $this->em->getClassMetadata($className); |
| 86 | $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); |
| 87 | if (!$persister instanceof CachedPersister) { |
| 88 | return \false; |
| 89 | } |
| 90 | return $persister->getCacheRegion()->contains($this->buildCollectionCacheKey($metadata, $association, $ownerIdentifier)); |
| 91 | } |
| 92 | public function evictCollection($className, $association, $ownerIdentifier) |
| 93 | { |
| 94 | $metadata = $this->em->getClassMetadata($className); |
| 95 | $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); |
| 96 | if (!$persister instanceof CachedPersister) { |
| 97 | return; |
| 98 | } |
| 99 | $persister->getCacheRegion()->evict($this->buildCollectionCacheKey($metadata, $association, $ownerIdentifier)); |
| 100 | } |
| 101 | public function evictCollectionRegion($className, $association) |
| 102 | { |
| 103 | $metadata = $this->em->getClassMetadata($className); |
| 104 | $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); |
| 105 | if (!$persister instanceof CachedPersister) { |
| 106 | return; |
| 107 | } |
| 108 | $persister->getCacheRegion()->evictAll(); |
| 109 | } |
| 110 | public function evictCollectionRegions() |
| 111 | { |
| 112 | $metadatas = $this->em->getMetadataFactory()->getAllMetadata(); |
| 113 | foreach ($metadatas as $metadata) { |
| 114 | foreach ($metadata->associationMappings as $association) { |
| 115 | if (!$association['type'] & ClassMetadata::TO_MANY) { |
| 116 | continue; |
| 117 | } |
| 118 | $persister = $this->uow->getCollectionPersister($association); |
| 119 | if (!$persister instanceof CachedPersister) { |
| 120 | continue; |
| 121 | } |
| 122 | $persister->getCacheRegion()->evictAll(); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | public function containsQuery($regionName) |
| 127 | { |
| 128 | return isset($this->queryCaches[$regionName]); |
| 129 | } |
| 130 | public function evictQueryRegion($regionName = null) |
| 131 | { |
| 132 | if ($regionName === null && $this->defaultQueryCache !== null) { |
| 133 | $this->defaultQueryCache->clear(); |
| 134 | return; |
| 135 | } |
| 136 | if (isset($this->queryCaches[$regionName])) { |
| 137 | $this->queryCaches[$regionName]->clear(); |
| 138 | } |
| 139 | } |
| 140 | public function evictQueryRegions() |
| 141 | { |
| 142 | $this->getQueryCache()->clear(); |
| 143 | foreach ($this->queryCaches as $queryCache) { |
| 144 | $queryCache->clear(); |
| 145 | } |
| 146 | } |
| 147 | public function getQueryCache($regionName = null) |
| 148 | { |
| 149 | if ($regionName === null) { |
| 150 | return $this->defaultQueryCache ?: ($this->defaultQueryCache = $this->cacheFactory->buildQueryCache($this->em)); |
| 151 | } |
| 152 | if (!isset($this->queryCaches[$regionName])) { |
| 153 | $this->queryCaches[$regionName] = $this->cacheFactory->buildQueryCache($this->em, $regionName); |
| 154 | } |
| 155 | return $this->queryCaches[$regionName]; |
| 156 | } |
| 157 | private function buildEntityCacheKey(ClassMetadata $metadata, $identifier) : EntityCacheKey |
| 158 | { |
| 159 | if (!is_array($identifier)) { |
| 160 | $identifier = $this->toIdentifierArray($metadata, $identifier); |
| 161 | } |
| 162 | return new EntityCacheKey($metadata->rootEntityName, $identifier); |
| 163 | } |
| 164 | private function buildCollectionCacheKey(ClassMetadata $metadata, string $association, $ownerIdentifier) : CollectionCacheKey |
| 165 | { |
| 166 | if (!is_array($ownerIdentifier)) { |
| 167 | $ownerIdentifier = $this->toIdentifierArray($metadata, $ownerIdentifier); |
| 168 | } |
| 169 | return new CollectionCacheKey($metadata->rootEntityName, $association, $ownerIdentifier); |
| 170 | } |
| 171 | private function toIdentifierArray(ClassMetadata $metadata, $identifier) : array |
| 172 | { |
| 173 | if (is_object($identifier)) { |
| 174 | $class = DefaultProxyClassNameResolver::getClass($identifier); |
| 175 | if ($this->em->getMetadataFactory()->hasMetadataFor($class)) { |
| 176 | $identifier = $this->uow->getSingleIdentifierValue($identifier); |
| 177 | if ($identifier === null) { |
| 178 | throw ORMInvalidArgumentException::invalidIdentifierBindingEntity($class); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | return [$metadata->identifier[0] => $identifier]; |
| 183 | } |
| 184 | } |
| 185 |