3 namespace Drupal\Core\Cache;
5 use Drupal\Component\Assertion\Inspector;
6 use Drupal\Core\PhpStorage\PhpStorageFactory;
7 use Drupal\Component\Utility\Crypt;
10 * Defines a PHP cache implementation.
12 * Stores cache items in a PHP file using a storage that implements
13 * Drupal\Component\PhpStorage\PhpStorageInterface.
15 * This is fast because of PHP's opcode caching mechanism. Once a file's
16 * content is stored in PHP's opcode cache, including it doesn't require
17 * reading the contents from a filesystem. Instead, PHP will use the already
18 * compiled opcodes stored in memory.
22 class PhpBackend implements CacheBackendInterface {
30 * Array to store cache objects.
32 protected $cache = [];
35 * The cache tags checksum provider.
37 * @var \Drupal\Core\Cache\CacheTagsChecksumInterface
39 protected $checksumProvider;
42 * Constructs a PhpBackend object.
45 * The cache bin for which the object is created.
46 * @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
47 * The cache tags checksum provider.
49 public function __construct($bin, CacheTagsChecksumInterface $checksum_provider) {
50 $this->bin = 'cache_' . $bin;
51 $this->checksumProvider = $checksum_provider;
57 public function get($cid, $allow_invalid = FALSE) {
58 return $this->getByHash($this->normalizeCid($cid), $allow_invalid);
62 * Fetch a cache item using a hashed cache ID.
64 * @param string $cidhash
65 * The hashed version of the original cache ID after being normalized.
66 * @param bool $allow_invalid
67 * (optional) If TRUE, a cache item may be returned even if it is expired or
68 * has been invalidated.
72 protected function getByHash($cidhash, $allow_invalid = FALSE) {
73 if ($file = $this->storage()->getFullPath($cidhash)) {
74 $cache = @include $file;
77 return $this->prepareItem($cache, $allow_invalid);
85 public function setMultiple(array $items) {
86 foreach ($items as $cid => $item) {
87 $this->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : []);
94 public function getMultiple(&$cids, $allow_invalid = FALSE) {
97 foreach ($cids as $cid) {
98 if ($item = $this->get($cid, $allow_invalid)) {
99 $ret[$item->cid] = $item;
103 $cids = array_diff($cids, array_keys($ret));
109 * Prepares a cached item.
111 * Checks that items are either permanent or did not expire, and returns data
114 * @param object $cache
115 * An item loaded from cache_get() or cache_get_multiple().
116 * @param bool $allow_invalid
117 * If FALSE, the method returns FALSE if the cache item is not valid.
120 * The item with data as appropriate or FALSE if there is no
121 * valid item to load.
123 protected function prepareItem($cache, $allow_invalid) {
124 if (!isset($cache->data)) {
128 // Check expire time.
129 $cache->valid = $cache->expire == Cache::PERMANENT || $cache->expire >= REQUEST_TIME;
131 // Check if invalidateTags() has been called with any of the item's tags.
132 if (!$this->checksumProvider->isValid($cache->checksum, $cache->tags)) {
133 $cache->valid = FALSE;
136 if (!$allow_invalid && !$cache->valid) {
146 public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
147 assert(Inspector::assertAllStrings($tags), 'Cache Tags must be strings.');
152 'created' => round(microtime(TRUE), 3),
154 'tags' => array_unique($tags),
155 'checksum' => $this->checksumProvider->getCurrentChecksum($tags),
157 $this->writeItem($this->normalizeCid($cid), $item);
163 public function delete($cid) {
164 $this->storage()->delete($this->normalizeCid($cid));
170 public function deleteMultiple(array $cids) {
171 foreach ($cids as $cid) {
179 public function deleteAll() {
180 $this->storage()->deleteAll();
186 public function invalidate($cid) {
187 $this->invalidatebyHash($this->normalizeCid($cid));
191 * Invalidate one cache item.
193 * @param string $cidhash
194 * The hashed version of the original cache ID after being normalized.
196 protected function invalidatebyHash($cidhash) {
197 if ($item = $this->getByHash($cidhash)) {
198 $item->expire = REQUEST_TIME - 1;
199 $this->writeItem($cidhash, $item);
206 public function invalidateMultiple(array $cids) {
207 foreach ($cids as $cid) {
208 $this->invalidate($cid);
215 public function invalidateAll() {
216 foreach ($this->storage()->listAll() as $cidhash) {
217 $this->invalidatebyHash($cidhash);
224 public function garbageCollection() {
230 public function removeBin() {
232 $this->storage()->deleteAll();
236 * Writes a cache item to PhpStorage.
238 * @param string $cidhash
239 * The hashed version of the original cache ID after being normalized.
240 * @param \stdClass $item
241 * The cache item to store.
243 protected function writeItem($cidhash, \stdClass $item) {
244 $content = '<?php return unserialize(' . var_export(serialize($item), TRUE) . ');';
245 $this->storage()->save($cidhash, $content);
249 * Gets the PHP code storage object to use.
251 * @return \Drupal\Component\PhpStorage\PhpStorageInterface
253 protected function storage() {
254 if (!isset($this->storage)) {
255 $this->storage = PhpStorageFactory::get($this->bin);
257 return $this->storage;
261 * Ensures a normalized cache ID.
264 * The passed in cache ID.
267 * A normalized cache ID.
269 protected function normalizeCid($cid) {
270 return Crypt::hashBase64($cid);