MemoryBackend.php in Drupal 8
File
core/lib/Drupal/Core/Cache/MemoryBackend.php
View source
<?php
namespace Drupal\Core\Cache;
use Drupal\Component\Assertion\Inspector;
class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterface {
protected $cache = [];
public function get($cid, $allow_invalid = FALSE) {
if (isset($this->cache[$cid])) {
return $this
->prepareItem($this->cache[$cid], $allow_invalid);
}
else {
return FALSE;
}
}
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;
}
protected function prepareItem($cache, $allow_invalid) {
if (!isset($cache->data)) {
return FALSE;
}
$prepared = clone $cache;
$prepared->data = unserialize($prepared->data);
$prepared->valid = $prepared->expire == Cache::PERMANENT || $prepared->expire >= $this
->getRequestTime();
if (!$allow_invalid && !$prepared->valid) {
return FALSE;
}
return $prepared;
}
public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
assert(Inspector::assertAllStrings($tags), 'Cache Tags must be strings.');
$tags = array_unique($tags);
sort($tags);
$this->cache[$cid] = (object) [
'cid' => $cid,
'data' => serialize($data),
'created' => $this
->getRequestTime(),
'expire' => $expire,
'tags' => $tags,
];
}
public function setMultiple(array $items = []) {
foreach ($items as $cid => $item) {
$this
->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : []);
}
}
public function delete($cid) {
unset($this->cache[$cid]);
}
public function deleteMultiple(array $cids) {
$this->cache = array_diff_key($this->cache, array_flip($cids));
}
public function deleteAll() {
$this->cache = [];
}
public function invalidate($cid) {
if (isset($this->cache[$cid])) {
$this->cache[$cid]->expire = $this
->getRequestTime() - 1;
}
}
public function invalidateMultiple(array $cids) {
$items = array_intersect_key($this->cache, array_flip($cids));
foreach ($items as $cid => $item) {
$this->cache[$cid]->expire = $this
->getRequestTime() - 1;
}
}
public function invalidateTags(array $tags) {
foreach ($this->cache as $cid => $item) {
if (array_intersect($tags, $item->tags)) {
$this->cache[$cid]->expire = $this
->getRequestTime() - 1;
}
}
}
public function invalidateAll() {
foreach ($this->cache as $cid => $item) {
$this->cache[$cid]->expire = $this
->getRequestTime() - 1;
}
}
public function garbageCollection() {
}
public function removeBin() {
$this->cache = [];
}
protected function getRequestTime() {
return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME'];
}
public function __sleep() {
return [];
}
public function reset() {
$this->cache = [];
}
}