3 namespace Drupal\block_content;
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Cache\CacheCollector;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Lock\LockBackendInterface;
11 * A cache collector that caches IDs for block_content UUIDs.
13 * As block_content entities are used as block plugin derivatives, it is a
14 * fairly safe limitation that there are not hundreds of them, a site will
15 * likely run into problems with too many block content entities in other places
16 * than a cache that only stores UUID's and IDs. The same assumption is not true
17 * for other content entities.
21 class BlockContentUuidLookup extends CacheCollector {
24 * The entity type manager.
26 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
28 protected $entityTypeManager;
31 * Constructs a BlockContentUuidLookup instance.
33 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
35 * @param \Drupal\Core\Lock\LockBackendInterface $lock
37 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
38 * The entity type manager.
40 public function __construct(CacheBackendInterface $cache, LockBackendInterface $lock, EntityTypeManagerInterface $entity_type_manager) {
41 parent::__construct('block_content_uuid', $cache, $lock);
42 $this->entityTypeManager = $entity_type_manager;
48 protected function resolveCacheMiss($key) {
49 $ids = $this->entityTypeManager->getStorage('block_content')->getQuery()
50 ->condition('uuid', $key)
53 // Only cache if there is a match, otherwise creating new entities would
54 // require to invalidate the cache.
57 $this->storage[$key] = $id;