class MemoryCache

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Cache/MemoryCache/MemoryCache.php \Drupal\Core\Cache\MemoryCache\MemoryCache
  2. 10 core/lib/Drupal/Core/Cache/MemoryCache/MemoryCache.php \Drupal\Core\Cache\MemoryCache\MemoryCache
  3. 11.x core/lib/Drupal/Core/Cache/MemoryCache/MemoryCache.php \Drupal\Core\Cache\MemoryCache\MemoryCache

Defines a memory cache implementation.

Stores cache items in memory using a PHP array.

Hierarchy

Expanded class hierarchy of MemoryCache

Related topics

5 files declare their use of MemoryCache
ConfigEntityStorageTest.php in core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php
EntityStorageDeprecationTest.php in core/tests/Drupal/Tests/Core/Entity/EntityStorageDeprecationTest.php
KeyValueEntityStorageTest.php in core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php
SqlContentEntityStorageTest.php in core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php
Contains \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageTest.
UserSessionTest.php in core/tests/Drupal/Tests/Core/Session/UserSessionTest.php
3 string references to 'MemoryCache'
book.services.yml in core/modules/book/book.services.yml
core/modules/book/book.services.yml
core.services.yml in core/core.services.yml
core/core.services.yml
jsonapi.services.yml in core/modules/jsonapi/jsonapi.services.yml
core/modules/jsonapi/jsonapi.services.yml
3 services use MemoryCache
book.memory_cache in core/modules/book/book.services.yml
Drupal\Core\Cache\MemoryCache\MemoryCache
cache.jsonapi_memory in core/modules/jsonapi/jsonapi.services.yml
Drupal\Core\Cache\MemoryCache\MemoryCache
entity.memory_cache in core/core.services.yml
Drupal\Core\Cache\MemoryCache\MemoryCache

File

core/lib/Drupal/Core/Cache/MemoryCache/MemoryCache.php, line 15

Namespace

Drupal\Core\Cache\MemoryCache
View source
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.
     */
    protected function prepareItem($cache, $allow_invalid = FALSE) {
        if (!isset($cache->data)) {
            return FALSE;
        }
        // Check expire time.
        $cache->valid = $cache->expire == static::CACHE_PERMANENT || $cache->expire >= $this->getRequestTime();
        if (!$allow_invalid && !$cache->valid) {
            return FALSE;
        }
        return $cache;
    }
    
    /**
     * {@inheritdoc}
     */
    public function set($cid, $data, $expire = MemoryCacheInterface::CACHE_PERMANENT, array $tags = []) {
        assert(Inspector::assertAllStrings($tags), 'Cache tags must be strings.');
        $tags = array_unique($tags);
        $this->cache[$cid] = (object) [
            'cid' => $cid,
            'data' => $data,
            'created' => $this->getRequestTime(),
            'expire' => $expire,
            'tags' => $tags,
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
CacheBackendInterface::CACHE_PERMANENT constant Indicates that the item should never be removed unless explicitly deleted.
MemoryBackend::$cache protected property Array to store cache objects.
MemoryBackend::delete public function Deletes an item from the cache. Overrides CacheBackendInterface::delete 1
MemoryBackend::deleteAll public function Deletes all cache items in a bin. Overrides CacheBackendInterface::deleteAll
MemoryBackend::deleteMultiple public function Deletes multiple items from the cache. Overrides CacheBackendInterface::deleteMultiple
MemoryBackend::garbageCollection public function Performs garbage collection on a cache bin. Overrides CacheBackendInterface::garbageCollection
MemoryBackend::get public function Returns data from the persistent cache. Overrides CacheBackendInterface::get 1
MemoryBackend::getMultiple public function Returns data from the persistent cache when given an array of cache IDs. Overrides CacheBackendInterface::getMultiple
MemoryBackend::getRequestTime protected function Wrapper method for REQUEST_TIME constant.
MemoryBackend::invalidate public function Marks a cache item as invalid. Overrides CacheBackendInterface::invalidate
MemoryBackend::invalidateAll public function Marks all cache items as invalid. Overrides CacheBackendInterface::invalidateAll
MemoryBackend::invalidateMultiple public function Marks cache items as invalid. Overrides CacheBackendInterface::invalidateMultiple
MemoryBackend::invalidateTags public function Marks cache items with any of the specified tags as invalid. Overrides CacheTagsInvalidatorInterface::invalidateTags
MemoryBackend::removeBin public function Remove a cache bin. Overrides CacheBackendInterface::removeBin
MemoryBackend::reset public function Reset statically cached variables.
MemoryBackend::setMultiple public function Store multiple items in the persistent cache. Overrides CacheBackendInterface::setMultiple
MemoryBackend::__sleep public function Prevents data stored in memory backends from being serialized.
MemoryCache::prepareItem protected function Prepares a cached item. Overrides MemoryBackend::prepareItem
MemoryCache::set public function Stores data in the persistent cache. Overrides MemoryBackend::set

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.