diff --git a/core/core.services.yml b/core/core.services.yml index b342ef7878..aef6c11af6 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -539,8 +539,8 @@ services: # @todo Remove this tag in https://www.drupal.org/node/2549143. tags: - { name: plugin_manager_cache_clear } - entity.static_cache: - class: Drupal\Core\Cache\StaticCache\StaticCache + entity.memory_cache: + class: Drupal\Core\Cache\MemoryCache\MemoryCache entity_type.manager: class: Drupal\Core\Entity\EntityTypeManager arguments: ['@container.namespaces', '@module_handler', '@cache.discovery', '@string_translation', '@class_resolver'] diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php index 9d9eb10203..1cc067fc9b 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php @@ -3,7 +3,7 @@ namespace Drupal\Core\Config\Entity; use Drupal\Core\Cache\CacheableMetadata; -use Drupal\Core\Cache\StaticCache\StaticCacheInterface; +use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigImporterException; use Drupal\Core\Entity\EntityInterface; @@ -105,13 +105,11 @@ class ConfigEntityStorage extends EntityStorageBase implements ConfigEntityStora * The UUID service. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. - * @param \Drupal\Core\Cache\StaticCache\StaticCacheInterface $static_cache + * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface|null $memory_cache * The static cache backend. */ - public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, StaticCacheInterface $static_cache = NULL) { - // @todo Remove this line in Drupal 9.0.x and make $static_cache required. - $static_cache = isset($static_cache) ? $static_cache : \Drupal::service('entity.static_cache'); - parent::__construct($entity_type, $static_cache); + public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache = NULL) { + parent::__construct($entity_type, $memory_cache); $this->configFactory = $config_factory; $this->uuidService = $uuid_service; @@ -127,7 +125,7 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $container->get('config.factory'), $container->get('uuid'), $container->get('language_manager'), - $container->get('entity.static_cache') + $container->get('entity.memory_cache') ); } diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index f58b3eb30c..53629ba600 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -4,7 +4,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; -use Drupal\Core\Cache\StaticCache\StaticCacheInterface; +use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\TypedData\TranslationStatusInterface; @@ -45,13 +45,11 @@ * The entity manager. * @param \Drupal\Core\Cache\CacheBackendInterface $cache * The cache backend to be used. - * @param \Drupal\Core\Cache\StaticCache\StaticCacheInterface $static_cache + * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface|null $memory_cache * The static cache backend. */ - public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, StaticCacheInterface $static_cache = NULL) { - // @todo Remove this line in Drupal 9.0.x and make $static_cache required. - $static_cache = isset($static_cache) ? $static_cache : \Drupal::service('entity.static_cache'); - parent::__construct($entity_type, $static_cache); + public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, MemoryCacheInterface $memory_cache = NULL) { + parent::__construct($entity_type, $memory_cache); $this->bundleKey = $this->entityType->getKey('bundle'); $this->entityManager = $entity_manager; $this->cacheBackend = $cache; @@ -65,7 +63,7 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $entity_type, $container->get('entity.manager'), $container->get('cache.entity'), - $container->get('entity.static_cache') + $container->get('entity.memory_cache') ); } @@ -984,7 +982,7 @@ public function loadUnchanged($id) { // As we've removed the entity from the static cache already we have to // put the loaded unchanged entity there to simulate the behavior of the // parent. - $this->setStaticCache($entities); + $this->setMemoryCache($entities); } } diff --git a/core/lib/Drupal/Core/Entity/EntityStorageBase.php b/core/lib/Drupal/Core/Entity/EntityStorageBase.php index 8c790bdb44..b3888b744c 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageBase.php @@ -3,20 +3,13 @@ namespace Drupal\Core\Entity; use Drupal\Core\Entity\Query\QueryInterface; -use Drupal\Core\Cache\StaticCache\StaticCacheInterface; +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. * @@ -76,9 +69,9 @@ /** * The static cache. * - * @var \Drupal\Core\Cache\StaticCache\StaticCacheInterface + * @var \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface */ - protected $staticCache; + protected $memoryCache; /** * The static cache cache tag. @@ -92,19 +85,19 @@ * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type definition. - * @param \Drupal\Core\Cache\StaticCache\StaticCacheInterface + * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface|null * The static cache. */ - public function __construct(EntityTypeInterface $entity_type, StaticCacheInterface $static_cache = NULL) { + 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(); - // @todo Remove this line in Drupal 9.0.x and make $static_cache required. - $this->staticCache = isset($static_cache) ? $static_cache : \Drupal::service('entity.static_cache'); - $this->cacheTag = 'entity_static_cache:' . $this->entityTypeId; + // @todo Remove this line in Drupal 9.0.x and make $memory_cache required. + $this->memoryCache = isset($memory_cache) ? $memory_cache : \Drupal::service('entity.memory_cache'); + $this->cacheTag = 'entity.memory_cache:' . $this->entityTypeId; } /** @@ -142,12 +135,12 @@ public function loadUnchanged($id) { public function resetCache(array $ids = NULL) { if ($this->entityType->isStaticallyCacheable() && isset($ids)) { foreach ($ids as $id) { - $this->staticCache->delete($this->getCacheId($id)); + $this->memoryCache->delete($this->getCacheId($id)); } } else { // Call the backend method directly. - $this->staticCache->invalidateTags([$this->cacheTag]); + $this->memoryCache->invalidateTags([$this->cacheTag]); } } @@ -160,12 +153,12 @@ public function resetCache(array $ids = NULL) { * @return \Drupal\Core\Entity\EntityInterface[] * Array of entities from the entity cache. */ - protected function getFromStaticCache(array $ids) { + protected function getFromMemoryCache(array $ids) { $entities = []; // Load any available entities from the internal cache. if ($this->entityType->isStaticallyCacheable()) { foreach ($ids as $id) { - if ($cached = $this->staticCache->get($this->getCacheId($id))) { + if ($cached = $this->memoryCache->get($this->getCacheId($id))) { $entities[$id] = $cached->data; } } @@ -179,10 +172,10 @@ protected function getFromStaticCache(array $ids) { * @param \Drupal\Core\Entity\EntityInterface[] $entities * Entities to store in the cache. */ - protected function setStaticCache(array $entities) { + protected function setMemoryCache(array $entities) { if ($this->entityType->isStaticallyCacheable()) { foreach ($entities as $id => $entity) { - $this->staticCache->set($this->getCacheId($entity->id()), $entity, StaticCacheInterface::CACHE_PERMANENT, [$this->cacheTag]); + $this->memoryCache->set($this->getCacheId($entity->id()), $entity, MemoryCacheInterface::CACHE_PERMANENT, [$this->cacheTag]); } } } @@ -262,7 +255,7 @@ public function loadMultiple(array $ids = NULL) { // Try to load entities from the static cache, if the entity type supports // static caching. if ($this->entityType->isStaticallyCacheable() && $ids) { - $entities += $this->getFromStaticCache($ids); + $entities += $this->getFromMemoryCache($ids); // If any entities were loaded, remove them from the ids still to load. if ($passed_ids) { $ids = array_keys(array_diff_key($passed_ids, $entities)); @@ -287,7 +280,7 @@ public function loadMultiple(array $ids = NULL) { if ($this->entityType->isStaticallyCacheable()) { // Add entities to the cache. if (!empty($queried_entities)) { - $this->setStaticCache($queried_entities); + $this->setMemoryCache($queried_entities); } } @@ -575,16 +568,4 @@ public function getAggregateQuery($conjunction = 'AND') { */ abstract protected function getQueryServiceName(); - /** - * {@inheritdoc} - */ - public function __sleep() { - // In case the storage is being serialized then we prevent from serializing - // the static cache of entities together with it, as this could lead to a - // memory leak. - $vars = parent::__sleep(); - unset($vars['entities']); - return $vars; - } - } diff --git a/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php b/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php index 7c8d30163b..3898a9e74c 100644 --- a/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php @@ -83,7 +83,7 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $container->get('keyvalue')->get('entity_storage__' . $entity_type->id()), $container->get('uuid'), $container->get('language_manager'), - $container->get('entity.static_cache') + $container->get('entity.memory_cache') ); } diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php index 7768e01e10..2bbd089ce0 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php @@ -133,7 +133,7 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $container->get('entity.manager'), $container->get('cache.entity'), $container->get('language_manager'), - $container->get('entity.static_cache') + $container->get('entity.memory_cache') ); } @@ -161,13 +161,11 @@ public function getFieldStorageDefinitions() { * The cache backend to be used. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. - * @param \Drupal\Core\Cache\CacheBackendInterface $static_cache - * The static cache backend to be used. + * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface|null $memory_cache + * The memory cache backend to be used. */ - public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, CacheBackendInterface $static_cache = NULL) { - // @todo Remove this line in Drupal 9.0.x and make $static_cache required. - $static_cache = isset($static_cache) ? $static_cache : \Drupal::service('entity.static_cache'); - parent::__construct($entity_type, $entity_manager, $cache, $static_cache); + public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, CacheBackendInterface $memory_cache = NULL) { + parent::__construct($entity_type, $entity_manager, $cache, $memory_cache); $this->database = $database; $this->languageManager = $language_manager; $this->initTableLayout(); diff --git a/core/lib/Drupal/Core/MemoryCache/MemoryCache.php b/core/lib/Drupal/Core/MemoryCache/MemoryCache.php index fa61227f15..b0d2ff502a 100644 --- a/core/lib/Drupal/Core/MemoryCache/MemoryCache.php +++ b/core/lib/Drupal/Core/MemoryCache/MemoryCache.php @@ -1,18 +1,18 @@ cache[$cid] = (object) [ 'cid' => $cid, 'data' => $data, diff --git a/core/modules/config/tests/config_entity_static_cache_test/src/ConfigOverrider.php b/core/modules/config/tests/config_entity_static_cache_test/src/ConfigOverrider.php index 9d48e0b1b9..4e6a490f84 100644 --- a/core/modules/config/tests/config_entity_static_cache_test/src/ConfigOverrider.php +++ b/core/modules/config/tests/config_entity_static_cache_test/src/ConfigOverrider.php @@ -1,6 +1,6 @@ assertNotIdentical($permissions, $authenticated->getPermissions()); $this->runUpdates(); - \Drupal::service('entity.static_cache')->reset(); + \Drupal::service('entity.memory_cache')->reset(); $authenticated = Role::load('authenticated'); $this->assertIdentical($permissions, $authenticated->getPermissions()); } diff --git a/core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStaticCacheTest.php b/core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStaticCacheTest.php index 1cd9cb1753..c8475342ff 100644 --- a/core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStaticCacheTest.php +++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStaticCacheTest.php @@ -2,7 +2,7 @@ namespace Drupal\KernelTests\Core\Config; -use Drupal\config_entity_static_cache_test\ConfigOverrider; +use Drupal\config_entity.memory_cache_test\ConfigOverrider; use Drupal\KernelTests\KernelTestBase; /** @@ -17,7 +17,7 @@ class ConfigEntityStaticCacheTest extends KernelTestBase { * * @var array */ - public static $modules = ['config_test', 'config_entity_static_cache_test']; + public static $modules = ['config_test', 'config_entity.memory_cache_test']; /** * The type ID of the entity under test. @@ -54,7 +54,7 @@ public function testCacheHit() { ->getStorage($this->entityTypeId); $entity_1 = $storage->load($this->entityId); $entity_2 = $storage->load($this->entityId); - // config_entity_static_cache_test_config_test_load() sets _loadStamp to a + // config_entity.memory_cache_test_config_test_load() sets _loadStamp to a // random string. If they match, it means $entity_2 was retrieved from the // static cache rather than going through a separate load sequence. $this->assertSame($entity_1->_loadStamp, $entity_2->_loadStamp);