diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php index 02b9978..77bc7a5 100644 --- a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php +++ b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php @@ -608,4 +608,103 @@ public function delete(array $entities) { throw new EntityStorageException($e->getMessage(), $e->getCode(), $e); } } + + /** + * {@inheritdoc} + */ + protected function cacheGet($ids) { + $entities = parent::cacheGet($ids); + if ($this->cache) { + // Build the list of cache entries to retrieve. + $cids = array(); + foreach ($ids as $id) { + if (!isset($entities[$id])) { + $cids[] = "entity:{$this->entityType}:$id"; + } + } + if ($cids && $cache = cache('entity')->getMultiple($cids)) { + // Put the cached field values back into the entities and remove them from + // the list of entities to query. + foreach ($ids as $id) { + $cid = "entity:{$this->entityType}:$id"; + if (isset($cache[$cid])) { + $values = $cache[$cid]->data['values']; + $translations = $cache[$cid]->data['translations']; + $bundle = $this->bundleKey ? $cache[$cid]->data['bundle'] : FALSE; + $entities[$id] = new $this->entityClass($values, $this->entityType, $bundle, $translations); + // Already put the loaded entity into the cache. + $this->entityCache[$id] = $entities[$id]; + } + } + } + return $entities; + } + } + + /** + * {@inheritdoc} + */ + protected function cacheSet($entities) { + if ($this->cache) { + foreach ($entities as $id => $entity) { + // Skip entities that are already in the static cache. + if (isset($this->entityCache[$id])) { + continue; + } + + $data = array( + 'id' => $entity->id(), + 'bundle' => $entity->bundle(), + 'translations' => array_keys($entity->getTranslationLanguages()), + 'values' => array(), + ); + foreach ($entity->getTranslationLanguages() as $langcode => $language) { + $translation = $entity->getTranslation($langcode); + + // Make sure the default language is valid. + if ($entity->getUntranslated()->language()->id == $langcode) { + $langcode = Language::LANGCODE_DEFAULT; + } + + foreach ($translation as $field_name => $items) { + if (!$items->isEmpty()) { + foreach ($items as $delta => $item) { + // If the field item needs to be prepare the cache data, call + // the corresponding method, otherwise use the values as cache + // data. + if ($item instanceof PrepareCacheInterface) { + $data['values'][$field_name][$langcode][$delta] = $item->getCacheData(); + } + else { + $data['values'][$field_name][$langcode][$delta] = $item->getValue(); + } + } + } + } + } + $cid = "entity:{$this->entityType}:$id"; + cache('entity')->set($cid, $data); + } + } + parent::cacheSet($entities); + } + + /** + * {@inheritdoc} + */ + public function resetCache(array $ids = NULL) { + parent::resetCache($ids); + if ($ids) { + $cids = array(); + foreach ($ids as $id) { + $cids[] = "entity:{$this->entityType}:$id"; + } + cache('entity')->deleteMultiple($cids); + } + else { + cache('entity')->deleteAll(); + } + } + + }