annotate core/modules/block_content/src/BlockContentUuidLookup.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\block_content;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\CacheBackendInterface;
Chris@0 6 use Drupal\Core\Cache\CacheCollector;
Chris@0 7 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@0 8 use Drupal\Core\Lock\LockBackendInterface;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * A cache collector that caches IDs for block_content UUIDs.
Chris@0 12 *
Chris@0 13 * As block_content entities are used as block plugin derivatives, it is a
Chris@0 14 * fairly safe limitation that there are not hundreds of them, a site will
Chris@0 15 * likely run into problems with too many block content entities in other places
Chris@0 16 * than a cache that only stores UUID's and IDs. The same assumption is not true
Chris@0 17 * for other content entities.
Chris@0 18 *
Chris@0 19 * @internal
Chris@0 20 */
Chris@0 21 class BlockContentUuidLookup extends CacheCollector {
Chris@0 22
Chris@0 23 /**
Chris@0 24 * The entity type manager.
Chris@0 25 *
Chris@0 26 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 27 */
Chris@0 28 protected $entityTypeManager;
Chris@0 29
Chris@0 30 /**
Chris@0 31 * Constructs a BlockContentUuidLookup instance.
Chris@0 32 *
Chris@0 33 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
Chris@0 34 * The cache backend.
Chris@0 35 * @param \Drupal\Core\Lock\LockBackendInterface $lock
Chris@0 36 * The lock backend.
Chris@0 37 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@0 38 * The entity type manager.
Chris@0 39 */
Chris@0 40 public function __construct(CacheBackendInterface $cache, LockBackendInterface $lock, EntityTypeManagerInterface $entity_type_manager) {
Chris@0 41 parent::__construct('block_content_uuid', $cache, $lock);
Chris@0 42 $this->entityTypeManager = $entity_type_manager;
Chris@0 43 }
Chris@0 44
Chris@0 45 /**
Chris@0 46 * {@inheritdoc}
Chris@0 47 */
Chris@0 48 protected function resolveCacheMiss($key) {
Chris@0 49 $ids = $this->entityTypeManager->getStorage('block_content')->getQuery()
Chris@0 50 ->condition('uuid', $key)
Chris@0 51 ->execute();
Chris@0 52
Chris@0 53 // Only cache if there is a match, otherwise creating new entities would
Chris@0 54 // require to invalidate the cache.
Chris@0 55 $id = reset($ids);
Chris@0 56 if ($id) {
Chris@0 57 $this->storage[$key] = $id;
Chris@0 58 $this->persist($key);
Chris@0 59 }
Chris@0 60 return $id;
Chris@0 61 }
Chris@0 62
Chris@0 63 }