diff --git a/core/lib/Drupal/Core/Cache/MemoryBackend.php b/core/lib/Drupal/Core/Cache/MemoryBackend.php index 438dcdad9b..975493c683 100644 --- a/core/lib/Drupal/Core/Cache/MemoryBackend.php +++ b/core/lib/Drupal/Core/Cache/MemoryBackend.php @@ -64,17 +64,17 @@ public function getMultiple(&$cids, $allow_invalid = FALSE) { * Checks that items are either permanent or did not expire, and returns data * as appropriate. * - * @param object $cache + * @param array $cache * An item loaded from self::get() or self::getMultiple(). * @param bool $allow_invalid * (optional) If TRUE, cache items may be returned even if they have expired * or been invalidated. * - * @return mixed + * @return object|false * The item with data as appropriate or FALSE if there is no * valid item to load. */ - protected function prepareItem($cache, $allow_invalid) { + protected function prepareItem(array $cache, $allow_invalid) { if (!isset($cache['data'])) { return FALSE; } @@ -99,6 +99,9 @@ public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) { $tags = array_unique($tags); // Sort the cache tags so that they are stored consistently in the database. sort($tags); + + // Do not create an object at this point to minimise the number of objects + // garbage collection has to keep a track off. $this->cache[$cid] = [ 'cid' => $cid, 'data' => serialize($data), diff --git a/core/lib/Drupal/Core/Cache/MemoryCache/MemoryCache.php b/core/lib/Drupal/Core/Cache/MemoryCache/MemoryCache.php index e546eba04f..c6fa20ba98 100644 --- a/core/lib/Drupal/Core/Cache/MemoryCache/MemoryCache.php +++ b/core/lib/Drupal/Core/Cache/MemoryCache/MemoryCache.php @@ -8,33 +8,21 @@ /** * Defines a memory cache implementation. * - * Stores cache items in memory using a PHP array. + * Stores cache items in memory using a PHP array. Cache data is not serialised + * thereby returning the same object as was cached. * * @ingroup cache */ class MemoryCache extends MemoryBackend implements MemoryCacheInterface { /** - * Prepares a cached item. - * - * Checks that items are either permanent or did not expire, and returns data - * as appropriate. - * - * @param object $cache - * An item loaded from self::get() or self::getMultiple(). - * @param bool $allow_invalid - * (optional) If TRUE, cache items may be returned even if they have expired - * or been invalidated. Defaults to FALSE. - * - * @return mixed - * The item with data as appropriate or FALSE if there is no - * valid item to load. + * {@inheritdoc} */ - protected function prepareItem($cache, $allow_invalid = FALSE) { - $prepared = (object) $cache; - if (!isset($prepared->data)) { + protected function prepareItem(array $cache, $allow_invalid = FALSE) { + if (!isset($cache['data'])) { return FALSE; } + $prepared = (object) $cache; // Check expire time. $prepared->valid = $prepared->expire == static::CACHE_PERMANENT || $prepared->expire >= $this->getRequestTime(); @@ -52,8 +40,11 @@ public function set($cid, $data, $expire = MemoryCacheInterface::CACHE_PERMANENT assert(Inspector::assertAllStrings($tags), 'Cache tags must be strings.'); $tags = array_unique($tags); + // Do not create an object at this point to minimise the number of objects + // garbage collection has to keep a track off. $this->cache[$cid] = [ 'cid' => $cid, + // Note that $data is not serialized. 'data' => $data, 'created' => $this->getRequestTime(), 'expire' => $expire,