namespace Drupal\Core\Entity;
use Drupal\Core\Entity\Query\QueryInterface;
+use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
/**
* A base entity storage class.
*/
abstract class EntityStorageBase extends EntityHandlerBase implements EntityStorageInterface, EntityHandlerInterface {
- /**
- * Static cache of entities, keyed by entity ID.
- *
- * @var array
- */
- protected $entities = [];
-
/**
* Entity type ID for this storage.
*
*/
protected $entityClass;
+ /**
+ * The memory cache.
+ *
+ * @var \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface
+ */
+ protected $memoryCache;
+
+ /**
+ * The memory cache cache tag.
+ *
+ * @var string
+ */
+ protected $memoryCacheTag;
+
/**
* Constructs an EntityStorageBase instance.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
+ * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface|null $memory_cache
+ * The memory cache.
*/
- public function __construct(EntityTypeInterface $entity_type) {
+ public function __construct(EntityTypeInterface $entity_type, MemoryCacheInterface $memory_cache = NULL) {
$this->entityTypeId = $entity_type->id();
$this->entityType = $entity_type;
$this->idKey = $this->entityType->getKey('id');
$this->uuidKey = $this->entityType->getKey('uuid');
$this->langcodeKey = $this->entityType->getKey('langcode');
$this->entityClass = $this->entityType->getClass();
+
+ if (!isset($memory_cache)) {
+ @trigger_error('The $memory_cache parameter was added in Drupal 8.6.x and will be required in 9.0.0. See https://www.drupal.org/node/2973262', E_USER_DEPRECATED);
+ $memory_cache = \Drupal::service('entity.memory_cache');
+ }
+ $this->memoryCache = $memory_cache;
+ $this->memoryCacheTag = 'entity.memory_cache:' . $this->entityTypeId;
}
/**
return $this->entityType;
}
+ /**
+ * Builds the cache ID for the passed in entity ID.
+ *
+ * @param int $id
+ * Entity ID for which the cache ID should be built.
+ *
+ * @return string
+ * Cache ID that can be passed to the cache backend.
+ */
+ protected function buildCacheId($id) {
+ return "values:{$this->entityTypeId}:$id";
+ }
+
/**
* {@inheritdoc}
*/
public function resetCache(array $ids = NULL) {
if ($this->entityType->isStaticallyCacheable() && isset($ids)) {
foreach ($ids as $id) {
- unset($this->entities[$id]);
+ $this->memoryCache->delete($this->buildCacheId($id));
}
}
else {
- $this->entities = [];
+ // Call the backend method directly.
+ $this->memoryCache->invalidateTags([$this->memoryCacheTag]);
}
}
protected function getFromStaticCache(array $ids) {
$entities = [];
// Load any available entities from the internal cache.
- if ($this->entityType->isStaticallyCacheable() && !empty($this->entities)) {
- $entities += array_intersect_key($this->entities, array_flip($ids));
+ if ($this->entityType->isStaticallyCacheable()) {
+ foreach ($ids as $id) {
+ if ($cached = $this->memoryCache->get($this->buildCacheId($id))) {
+ $entities[$id] = $cached->data;
+ }
+ }
}
return $entities;
}
*/
protected function setStaticCache(array $entities) {
if ($this->entityType->isStaticallyCacheable()) {
- $this->entities += $entities;
+ foreach ($entities as $id => $entity) {
+ $this->memoryCache->set($this->buildCacheId($entity->id()), $entity, MemoryCacheInterface::CACHE_PERMANENT, [$this->memoryCacheTag]);
+ }
}
}
* Invokes a hook on behalf of the entity.
*
* @param string $hook
- * One of 'presave', 'insert', 'update', 'predelete', 'delete', or
+ * One of 'create', 'presave', 'insert', 'update', 'predelete', 'delete', or
* 'revision_delete'.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.