3 namespace Drupal\Core\Cache;
5 use Drupal\Core\PhpStorage\PhpStorageFactory;
6 use Drupal\Component\Utility\Crypt;
9 * Defines a PHP cache implementation.
11 * Stores cache items in a PHP file using a storage that implements
12 * Drupal\Component\PhpStorage\PhpStorageInterface.
14 * This is fast because of PHP's opcode caching mechanism. Once a file's
15 * content is stored in PHP's opcode cache, including it doesn't require
16 * reading the contents from a filesystem. Instead, PHP will use the already
17 * compiled opcodes stored in memory.
21 class PhpBackend implements CacheBackendInterface {
29 * Array to store cache objects.
31 protected $cache = [];
34 * The cache tags checksum provider.
36 * @var \Drupal\Core\Cache\CacheTagsChecksumInterface
38 protected $checksumProvider;
41 * Constructs a PhpBackend object.
44 * The cache bin for which the object is created.
45 * @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
46 * The cache tags checksum provider.
48 public function __construct($bin, CacheTagsChecksumInterface $checksum_provider) {
49 $this->bin = 'cache_' . $bin;
50 $this->checksumProvider = $checksum_provider;
56 public function get($cid, $allow_invalid = FALSE) {
57 return $this->getByHash($this->normalizeCid($cid), $allow_invalid);
61 * Fetch a cache item using a hashed cache ID.
63 * @param string $cidhash
64 * The hashed version of the original cache ID after being normalized.
65 * @param bool $allow_invalid
66 * (optional) If TRUE, a cache item may be returned even if it is expired or
67 * has been invalidated.
71 protected function getByHash($cidhash, $allow_invalid = FALSE) {
72 if ($file = $this->storage()->getFullPath($cidhash)) {
73 $cache = @include $file;
76 return $this->prepareItem($cache, $allow_invalid);
84 public function setMultiple(array $items) {
85 foreach ($items as $cid => $item) {
86 $this->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : []);
93 public function getMultiple(&$cids, $allow_invalid = FALSE) {
96 foreach ($cids as $cid) {
97 if ($item = $this->get($cid, $allow_invalid)) {
98 $ret[$item->cid] = $item;
102 $cids = array_diff($cids, array_keys($ret));
108 * Prepares a cached item.
110 * Checks that items are either permanent or did not expire, and returns data
113 * @param object $cache
114 * An item loaded from cache_get() or cache_get_multiple().
115 * @param bool $allow_invalid
116 * If FALSE, the method returns FALSE if the cache item is not valid.
119 * The item with data as appropriate or FALSE if there is no
120 * valid item to load.
122 protected function prepareItem($cache, $allow_invalid) {
123 if (!isset($cache->data)) {
127 // Check expire time.
128 $cache->valid = $cache->expire == Cache::PERMANENT || $cache->expire >= REQUEST_TIME;
130 // Check if invalidateTags() has been called with any of the item's tags.
131 if (!$this->checksumProvider->isValid($cache->checksum, $cache->tags)) {
132 $cache->valid = FALSE;
135 if (!$allow_invalid && !$cache->valid) {
145 public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
146 assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache Tags must be strings.');
150 'created' => round(microtime(TRUE), 3),
152 'tags' => array_unique($tags),
153 'checksum' => $this->checksumProvider->getCurrentChecksum($tags),
155 $this->writeItem($this->normalizeCid($cid), $item);
161 public function delete($cid) {
162 $this->storage()->delete($this->normalizeCid($cid));
168 public function deleteMultiple(array $cids) {
169 foreach ($cids as $cid) {
177 public function deleteAll() {
178 $this->storage()->deleteAll();
184 public function invalidate($cid) {
185 $this->invalidatebyHash($this->normalizeCid($cid));
189 * Invalidate one cache item.
191 * @param string $cidhash
192 * The hashed version of the original cache ID after being normalized.
194 protected function invalidatebyHash($cidhash) {
195 if ($item = $this->getByHash($cidhash)) {
196 $item->expire = REQUEST_TIME - 1;
197 $this->writeItem($cidhash, $item);
204 public function invalidateMultiple(array $cids) {
205 foreach ($cids as $cid) {
206 $this->invalidate($cid);
213 public function invalidateAll() {
214 foreach ($this->storage()->listAll() as $cidhash) {
215 $this->invalidatebyHash($cidhash);
222 public function garbageCollection() {
228 public function removeBin() {
230 $this->storage()->deleteAll();
234 * Writes a cache item to PhpStorage.
236 * @param string $cidhash
237 * The hashed version of the original cache ID after being normalized.
238 * @param \stdClass $item
239 * The cache item to store.
241 protected function writeItem($cidhash, \stdClass $item) {
242 $content = '<?php return unserialize(' . var_export(serialize($item), TRUE) . ');';
243 $this->storage()->save($cidhash, $content);
247 * Gets the PHP code storage object to use.
249 * @return \Drupal\Component\PhpStorage\PhpStorageInterface
251 protected function storage() {
252 if (!isset($this->storage)) {
253 $this->storage = PhpStorageFactory::get($this->bin);
255 return $this->storage;
259 * Ensures a normalized cache ID.
262 * The passed in cache ID.
265 * A normalized cache ID.
267 protected function normalizeCid($cid) {
268 return Crypt::hashBase64($cid);