DefaultCache.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Cache;
  20. use Doctrine\Common\Util\ClassUtils;
  21. use Doctrine\ORM\Cache;
  22. use Doctrine\ORM\Cache\Persister\CachedPersister;
  23. use Doctrine\ORM\EntityManagerInterface;
  24. use Doctrine\ORM\Mapping\ClassMetadata;
  25. use Doctrine\ORM\ORMInvalidArgumentException;
  26. use Doctrine\ORM\UnitOfWork;
  27. use function is_array;
  28. use function is_object;
  29. /**
  30. * Provides an API for querying/managing the second level cache regions.
  31. */
  32. class DefaultCache implements Cache
  33. {
  34. /** @var EntityManagerInterface */
  35. private $em;
  36. /** @var UnitOfWork */
  37. private $uow;
  38. /** @var CacheFactory */
  39. private $cacheFactory;
  40. /** @var QueryCache[] */
  41. private $queryCaches = [];
  42. /** @var QueryCache */
  43. private $defaultQueryCache;
  44. public function __construct(EntityManagerInterface $em)
  45. {
  46. $this->em = $em;
  47. $this->uow = $em->getUnitOfWork();
  48. $this->cacheFactory = $em->getConfiguration()
  49. ->getSecondLevelCacheConfiguration()
  50. ->getCacheFactory();
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getEntityCacheRegion($className)
  56. {
  57. $metadata = $this->em->getClassMetadata($className);
  58. $persister = $this->uow->getEntityPersister($metadata->rootEntityName);
  59. if (! ($persister instanceof CachedPersister)) {
  60. return null;
  61. }
  62. return $persister->getCacheRegion();
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getCollectionCacheRegion($className, $association)
  68. {
  69. $metadata = $this->em->getClassMetadata($className);
  70. $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association));
  71. if (! ($persister instanceof CachedPersister)) {
  72. return null;
  73. }
  74. return $persister->getCacheRegion();
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function containsEntity($className, $identifier)
  80. {
  81. $metadata = $this->em->getClassMetadata($className);
  82. $persister = $this->uow->getEntityPersister($metadata->rootEntityName);
  83. if (! ($persister instanceof CachedPersister)) {
  84. return false;
  85. }
  86. return $persister->getCacheRegion()->contains($this->buildEntityCacheKey($metadata, $identifier));
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function evictEntity($className, $identifier)
  92. {
  93. $metadata = $this->em->getClassMetadata($className);
  94. $persister = $this->uow->getEntityPersister($metadata->rootEntityName);
  95. if (! ($persister instanceof CachedPersister)) {
  96. return;
  97. }
  98. $persister->getCacheRegion()->evict($this->buildEntityCacheKey($metadata, $identifier));
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function evictEntityRegion($className)
  104. {
  105. $metadata = $this->em->getClassMetadata($className);
  106. $persister = $this->uow->getEntityPersister($metadata->rootEntityName);
  107. if (! ($persister instanceof CachedPersister)) {
  108. return;
  109. }
  110. $persister->getCacheRegion()->evictAll();
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function evictEntityRegions()
  116. {
  117. $metadatas = $this->em->getMetadataFactory()->getAllMetadata();
  118. foreach ($metadatas as $metadata) {
  119. $persister = $this->uow->getEntityPersister($metadata->rootEntityName);
  120. if (! ($persister instanceof CachedPersister)) {
  121. continue;
  122. }
  123. $persister->getCacheRegion()->evictAll();
  124. }
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function containsCollection($className, $association, $ownerIdentifier)
  130. {
  131. $metadata = $this->em->getClassMetadata($className);
  132. $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association));
  133. if (! ($persister instanceof CachedPersister)) {
  134. return false;
  135. }
  136. return $persister->getCacheRegion()->contains($this->buildCollectionCacheKey($metadata, $association, $ownerIdentifier));
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function evictCollection($className, $association, $ownerIdentifier)
  142. {
  143. $metadata = $this->em->getClassMetadata($className);
  144. $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association));
  145. if (! ($persister instanceof CachedPersister)) {
  146. return;
  147. }
  148. $persister->getCacheRegion()->evict($this->buildCollectionCacheKey($metadata, $association, $ownerIdentifier));
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function evictCollectionRegion($className, $association)
  154. {
  155. $metadata = $this->em->getClassMetadata($className);
  156. $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association));
  157. if (! ($persister instanceof CachedPersister)) {
  158. return;
  159. }
  160. $persister->getCacheRegion()->evictAll();
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function evictCollectionRegions()
  166. {
  167. $metadatas = $this->em->getMetadataFactory()->getAllMetadata();
  168. foreach ($metadatas as $metadata) {
  169. foreach ($metadata->associationMappings as $association) {
  170. if (! $association['type'] & ClassMetadata::TO_MANY) {
  171. continue;
  172. }
  173. $persister = $this->uow->getCollectionPersister($association);
  174. if (! ($persister instanceof CachedPersister)) {
  175. continue;
  176. }
  177. $persister->getCacheRegion()->evictAll();
  178. }
  179. }
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function containsQuery($regionName)
  185. {
  186. return isset($this->queryCaches[$regionName]);
  187. }
  188. /**
  189. * {@inheritdoc}
  190. */
  191. public function evictQueryRegion($regionName = null)
  192. {
  193. if ($regionName === null && $this->defaultQueryCache !== null) {
  194. $this->defaultQueryCache->clear();
  195. return;
  196. }
  197. if (isset($this->queryCaches[$regionName])) {
  198. $this->queryCaches[$regionName]->clear();
  199. }
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function evictQueryRegions()
  205. {
  206. $this->getQueryCache()->clear();
  207. foreach ($this->queryCaches as $queryCache) {
  208. $queryCache->clear();
  209. }
  210. }
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function getQueryCache($regionName = null)
  215. {
  216. if ($regionName === null) {
  217. return $this->defaultQueryCache ?:
  218. $this->defaultQueryCache = $this->cacheFactory->buildQueryCache($this->em);
  219. }
  220. if (! isset($this->queryCaches[$regionName])) {
  221. $this->queryCaches[$regionName] = $this->cacheFactory->buildQueryCache($this->em, $regionName);
  222. }
  223. return $this->queryCaches[$regionName];
  224. }
  225. /**
  226. * @param ClassMetadata $metadata The entity metadata.
  227. * @param mixed $identifier The entity identifier.
  228. *
  229. * @return EntityCacheKey
  230. */
  231. private function buildEntityCacheKey(ClassMetadata $metadata, $identifier)
  232. {
  233. if (! is_array($identifier)) {
  234. $identifier = $this->toIdentifierArray($metadata, $identifier);
  235. }
  236. return new EntityCacheKey($metadata->rootEntityName, $identifier);
  237. }
  238. /**
  239. * @param ClassMetadata $metadata The entity metadata.
  240. * @param string $association The field name that represents the association.
  241. * @param mixed $ownerIdentifier The identifier of the owning entity.
  242. *
  243. * @return CollectionCacheKey
  244. */
  245. private function buildCollectionCacheKey(ClassMetadata $metadata, $association, $ownerIdentifier)
  246. {
  247. if (! is_array($ownerIdentifier)) {
  248. $ownerIdentifier = $this->toIdentifierArray($metadata, $ownerIdentifier);
  249. }
  250. return new CollectionCacheKey($metadata->rootEntityName, $association, $ownerIdentifier);
  251. }
  252. /**
  253. * @param ClassMetadata $metadata The entity metadata.
  254. * @param mixed $identifier The entity identifier.
  255. *
  256. * @return array<string, mixed>
  257. */
  258. private function toIdentifierArray(ClassMetadata $metadata, $identifier)
  259. {
  260. if (is_object($identifier) && $this->em->getMetadataFactory()->hasMetadataFor(ClassUtils::getClass($identifier))) {
  261. $identifier = $this->uow->getSingleIdentifierValue($identifier);
  262. if ($identifier === null) {
  263. throw ORMInvalidArgumentException::invalidIdentifierBindingEntity();
  264. }
  265. }
  266. return [$metadata->identifier[0] => $identifier];
  267. }
  268. }