class MemoryBackend
Same name in this branch
- 11.x core/lib/Drupal/Core/Flood/MemoryBackend.php \Drupal\Core\Flood\MemoryBackend
Same name in other branches
- 9 core/lib/Drupal/Core/Flood/MemoryBackend.php \Drupal\Core\Flood\MemoryBackend
- 9 core/lib/Drupal/Core/Cache/MemoryBackend.php \Drupal\Core\Cache\MemoryBackend
- 8.9.x core/lib/Drupal/Core/Flood/MemoryBackend.php \Drupal\Core\Flood\MemoryBackend
- 8.9.x core/lib/Drupal/Core/Cache/MemoryBackend.php \Drupal\Core\Cache\MemoryBackend
- 10 core/lib/Drupal/Core/Flood/MemoryBackend.php \Drupal\Core\Flood\MemoryBackend
- 10 core/lib/Drupal/Core/Cache/MemoryBackend.php \Drupal\Core\Cache\MemoryBackend
Defines a memory cache implementation.
Stores cache items in memory using a PHP array.
Should be used for unit tests and specialist use-cases only, does not store cached items between requests.
The functions ::prepareItem()/::set() use unserialize()/serialize(). It behaves as an external cache backend to avoid changing the cached data by reference. In ::prepareItem(), the object is not modified by the call to unserialize() because we make a clone of it.
Hierarchy
- class \Drupal\Core\Cache\MemoryBackend implements \Drupal\Core\Cache\CacheBackendInterface, \Drupal\Core\Cache\CacheTagsInvalidatorInterface
Expanded class hierarchy of MemoryBackend
Related topics
14 files declare their use of MemoryBackend
- AssetResolverTest.php in core/
tests/ Drupal/ Tests/ Core/ Asset/ AssetResolverTest.php - BackendChainImplementationUnitTest.php in core/
tests/ Drupal/ Tests/ Core/ Cache/ BackendChainImplementationUnitTest.php - BackendChainTest.php in core/
tests/ Drupal/ KernelTests/ Core/ Cache/ BackendChainTest.php - CacheFactoryDecorator.php in core/
modules/ system/ tests/ modules/ performance_test/ src/ Cache/ CacheFactoryDecorator.php - ChainedFastBackendTest.php in core/
tests/ Drupal/ Tests/ Core/ Cache/ ChainedFastBackendTest.php
File
-
core/
lib/ Drupal/ Core/ Cache/ MemoryBackend.php, line 23
Namespace
Drupal\Core\CacheView source
class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterface {
/**
* Array to store cache objects.
*
* @var object[]
*/
protected $cache = [];
/**
* Constructs a MemoryBackend object.
*
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(TimeInterface $time) {
}
/**
* {@inheritdoc}
*/
public function get($cid, $allow_invalid = FALSE) {
if (isset($this->cache[$cid])) {
return $this->prepareItem($this->cache[$cid], $allow_invalid);
}
else {
return FALSE;
}
}
/**
* {@inheritdoc}
*/
public function getMultiple(&$cids, $allow_invalid = FALSE) {
$ret = [];
$items = array_intersect_key($this->cache, array_flip($cids));
foreach ($items as $item) {
$item = $this->prepareItem($item, $allow_invalid);
if ($item) {
$ret[$item->cid] = $item;
}
}
$cids = array_diff($cids, array_keys($ret));
return $ret;
}
/**
* 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.
*
* @return mixed
* The item with data as appropriate or FALSE if there is no
* valid item to load.
*/
protected function prepareItem($cache, $allow_invalid) {
if (!isset($cache->data)) {
return FALSE;
}
// The object passed into this function is the one stored in $this->cache.
// We must clone it as part of the preparation step so that the actual
// cache object is not affected by the unserialize() call or other
// manipulations of the returned object.
$prepared = clone $cache;
$prepared->data = unserialize($prepared->data);
// Check expire time.
$prepared->valid = $prepared->expire == Cache::PERMANENT || $prepared->expire >= $this->time
->getRequestTime();
if (!$allow_invalid && !$prepared->valid) {
return FALSE;
}
return $prepared;
}
/**
* {@inheritdoc}
*/
public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
assert(Inspector::assertAllStrings($tags), 'Cache Tags must be strings.');
$tags = array_unique($tags);
// Sort the cache tags so that they are stored consistently in the database.
sort($tags);
$this->cache[$cid] = (object) [
'cid' => $cid,
'data' => serialize($data),
'created' => $this->time
->getRequestTime(),
'expire' => $expire,
'tags' => $tags,
];
}
/**
* {@inheritdoc}
*/
public function setMultiple(array $items = []) {
foreach ($items as $cid => $item) {
$this->set($cid, $item['data'], $item['expire'] ?? CacheBackendInterface::CACHE_PERMANENT, $item['tags'] ?? []);
}
}
/**
* {@inheritdoc}
*/
public function delete($cid) {
unset($this->cache[$cid]);
}
/**
* {@inheritdoc}
*/
public function deleteMultiple(array $cids) {
$this->cache = array_diff_key($this->cache, array_flip($cids));
}
/**
* {@inheritdoc}
*/
public function deleteAll() {
$this->cache = [];
}
/**
* {@inheritdoc}
*/
public function invalidate($cid) {
if (isset($this->cache[$cid])) {
$this->cache[$cid]->expire = $this->time
->getRequestTime() - 1;
}
}
/**
* {@inheritdoc}
*/
public function invalidateMultiple(array $cids) {
$items = array_intersect_key($this->cache, array_flip($cids));
foreach ($items as $cid => $item) {
$this->cache[$cid]->expire = $this->time
->getRequestTime() - 1;
}
}
/**
* {@inheritdoc}
*/
public function invalidateTags(array $tags) {
foreach ($this->cache as $cid => $item) {
if (array_intersect($tags, $item->tags)) {
$this->cache[$cid]->expire = $this->time
->getRequestTime() - 1;
}
}
}
/**
* {@inheritdoc}
*/
public function invalidateAll() {
foreach ($this->cache as $cid => $item) {
$this->cache[$cid]->expire = $this->time
->getRequestTime() - 1;
}
}
/**
* {@inheritdoc}
*/
public function garbageCollection() {
}
/**
* {@inheritdoc}
*/
public function removeBin() {
$this->cache = [];
}
/**
* Prevents data stored in memory backends from being serialized.
*/
public function __sleep() : array {
return [
'time',
];
}
/**
* Reset statically cached variables.
*
* This is only used by tests.
*/
public function reset() {
$this->cache = [];
}
}
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::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::prepareItem | protected | function | Prepares a cached item. | 1 | |
MemoryBackend::removeBin | public | function | Remove a cache bin. | Overrides CacheBackendInterface::removeBin | |
MemoryBackend::reset | public | function | Reset statically cached variables. | ||
MemoryBackend::set | public | function | Stores data in the persistent cache. | Overrides CacheBackendInterface::set | 2 |
MemoryBackend::setMultiple | public | function | Store multiple items in the persistent cache. | Overrides CacheBackendInterface::setMultiple | |
MemoryBackend::__construct | public | function | Constructs a MemoryBackend object. | ||
MemoryBackend::__sleep | public | function | Prevents data stored in memory backends from being serialized. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.