annotate core/lib/Drupal/Core/Cache/ApcuBackend.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Cache;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * Stores cache items in the Alternative PHP Cache User Cache (APCu).
Chris@0 7 */
Chris@0 8 class ApcuBackend implements CacheBackendInterface {
Chris@0 9
Chris@0 10 /**
Chris@0 11 * The name of the cache bin to use.
Chris@0 12 *
Chris@0 13 * @var string
Chris@0 14 */
Chris@0 15 protected $bin;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Prefix for all keys in the storage that belong to this site.
Chris@0 19 *
Chris@0 20 * @var string
Chris@0 21 */
Chris@0 22 protected $sitePrefix;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Prefix for all keys in this cache bin.
Chris@0 26 *
Chris@0 27 * Includes the site-specific prefix in $sitePrefix.
Chris@0 28 *
Chris@0 29 * @var string
Chris@0 30 */
Chris@0 31 protected $binPrefix;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * The cache tags checksum provider.
Chris@0 35 *
Chris@0 36 * @var \Drupal\Core\Cache\CacheTagsChecksumInterface
Chris@0 37 */
Chris@0 38 protected $checksumProvider;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Constructs a new ApcuBackend instance.
Chris@0 42 *
Chris@0 43 * @param string $bin
Chris@0 44 * The name of the cache bin.
Chris@0 45 * @param string $site_prefix
Chris@0 46 * The prefix to use for all keys in the storage that belong to this site.
Chris@0 47 * @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
Chris@0 48 * The cache tags checksum provider.
Chris@0 49 */
Chris@0 50 public function __construct($bin, $site_prefix, CacheTagsChecksumInterface $checksum_provider) {
Chris@0 51 $this->bin = $bin;
Chris@0 52 $this->sitePrefix = $site_prefix;
Chris@0 53 $this->checksumProvider = $checksum_provider;
Chris@0 54 $this->binPrefix = $this->sitePrefix . '::' . $this->bin . '::';
Chris@0 55 }
Chris@0 56
Chris@0 57 /**
Chris@0 58 * Prepends the APCu user variable prefix for this bin to a cache item ID.
Chris@0 59 *
Chris@0 60 * @param string $cid
Chris@0 61 * The cache item ID to prefix.
Chris@0 62 *
Chris@0 63 * @return string
Chris@0 64 * The APCu key for the cache item ID.
Chris@0 65 */
Chris@0 66 public function getApcuKey($cid) {
Chris@0 67 return $this->binPrefix . $cid;
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * {@inheritdoc}
Chris@0 72 */
Chris@0 73 public function get($cid, $allow_invalid = FALSE) {
Chris@0 74 $cache = apcu_fetch($this->getApcuKey($cid));
Chris@0 75 return $this->prepareItem($cache, $allow_invalid);
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * {@inheritdoc}
Chris@0 80 */
Chris@0 81 public function getMultiple(&$cids, $allow_invalid = FALSE) {
Chris@0 82 // Translate the requested cache item IDs to APCu keys.
Chris@0 83 $map = [];
Chris@0 84 foreach ($cids as $cid) {
Chris@0 85 $map[$this->getApcuKey($cid)] = $cid;
Chris@0 86 }
Chris@0 87
Chris@0 88 $result = apcu_fetch(array_keys($map));
Chris@0 89 $cache = [];
Chris@0 90 if ($result) {
Chris@0 91 foreach ($result as $key => $item) {
Chris@0 92 $item = $this->prepareItem($item, $allow_invalid);
Chris@0 93 if ($item) {
Chris@0 94 $cache[$map[$key]] = $item;
Chris@0 95 }
Chris@0 96 }
Chris@0 97 }
Chris@0 98 unset($result);
Chris@0 99
Chris@0 100 $cids = array_diff($cids, array_keys($cache));
Chris@0 101 return $cache;
Chris@0 102 }
Chris@0 103
Chris@0 104 /**
Chris@0 105 * Returns all cached items, optionally limited by a cache ID prefix.
Chris@0 106 *
Chris@0 107 * APCu is a memory cache, shared across all server processes. To prevent
Chris@0 108 * cache item clashes with other applications/installations, every cache item
Chris@0 109 * is prefixed with a unique string for this site. Therefore, functions like
Chris@0 110 * apcu_clear_cache() cannot be used, and instead, a list of all cache items
Chris@0 111 * belonging to this application need to be retrieved through this method
Chris@0 112 * instead.
Chris@0 113 *
Chris@0 114 * @param string $prefix
Chris@0 115 * (optional) A cache ID prefix to limit the result to.
Chris@0 116 *
Chris@0 117 * @return \APCUIterator
Chris@0 118 * An APCUIterator containing matched items.
Chris@0 119 */
Chris@0 120 protected function getAll($prefix = '') {
Chris@0 121 return $this->getIterator('/^' . preg_quote($this->getApcuKey($prefix), '/') . '/');
Chris@0 122 }
Chris@0 123
Chris@0 124 /**
Chris@0 125 * Prepares a cached item.
Chris@0 126 *
Chris@0 127 * Checks that the item is either permanent or did not expire.
Chris@0 128 *
Chris@0 129 * @param \stdClass $cache
Chris@0 130 * An item loaded from cache_get() or cache_get_multiple().
Chris@0 131 * @param bool $allow_invalid
Chris@0 132 * If TRUE, a cache item may be returned even if it is expired or has been
Chris@0 133 * invalidated. See ::get().
Chris@0 134 *
Chris@0 135 * @return mixed
Chris@0 136 * The cache item or FALSE if the item expired.
Chris@0 137 */
Chris@0 138 protected function prepareItem($cache, $allow_invalid) {
Chris@0 139 if (!isset($cache->data)) {
Chris@0 140 return FALSE;
Chris@0 141 }
Chris@0 142
Chris@0 143 $cache->tags = $cache->tags ? explode(' ', $cache->tags) : [];
Chris@0 144
Chris@0 145 // Check expire time.
Chris@0 146 $cache->valid = $cache->expire == Cache::PERMANENT || $cache->expire >= REQUEST_TIME;
Chris@0 147
Chris@0 148 // Check if invalidateTags() has been called with any of the entry's tags.
Chris@0 149 if (!$this->checksumProvider->isValid($cache->checksum, $cache->tags)) {
Chris@0 150 $cache->valid = FALSE;
Chris@0 151 }
Chris@0 152
Chris@0 153 if (!$allow_invalid && !$cache->valid) {
Chris@0 154 return FALSE;
Chris@0 155 }
Chris@0 156
Chris@0 157 return $cache;
Chris@0 158 }
Chris@0 159
Chris@0 160 /**
Chris@0 161 * {@inheritdoc}
Chris@0 162 */
Chris@0 163 public function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = []) {
Chris@0 164 assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache tags must be strings.');
Chris@0 165 $tags = array_unique($tags);
Chris@0 166 $cache = new \stdClass();
Chris@0 167 $cache->cid = $cid;
Chris@0 168 $cache->created = round(microtime(TRUE), 3);
Chris@0 169 $cache->expire = $expire;
Chris@0 170 $cache->tags = implode(' ', $tags);
Chris@0 171 $cache->checksum = $this->checksumProvider->getCurrentChecksum($tags);
Chris@0 172 // APCu serializes/unserializes any structure itself.
Chris@0 173 $cache->serialized = 0;
Chris@0 174 $cache->data = $data;
Chris@0 175
Chris@0 176 // Expiration is handled by our own prepareItem(), not APCu.
Chris@0 177 apcu_store($this->getApcuKey($cid), $cache);
Chris@0 178 }
Chris@0 179
Chris@0 180 /**
Chris@0 181 * {@inheritdoc}
Chris@0 182 */
Chris@0 183 public function setMultiple(array $items = []) {
Chris@0 184 foreach ($items as $cid => $item) {
Chris@0 185 $this->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : []);
Chris@0 186 }
Chris@0 187 }
Chris@0 188
Chris@0 189 /**
Chris@0 190 * {@inheritdoc}
Chris@0 191 */
Chris@0 192 public function delete($cid) {
Chris@0 193 apcu_delete($this->getApcuKey($cid));
Chris@0 194 }
Chris@0 195
Chris@0 196 /**
Chris@0 197 * {@inheritdoc}
Chris@0 198 */
Chris@0 199 public function deleteMultiple(array $cids) {
Chris@0 200 apcu_delete(array_map([$this, 'getApcuKey'], $cids));
Chris@0 201 }
Chris@0 202
Chris@0 203 /**
Chris@0 204 * {@inheritdoc}
Chris@0 205 */
Chris@0 206 public function deleteAll() {
Chris@0 207 apcu_delete($this->getIterator('/^' . preg_quote($this->binPrefix, '/') . '/'));
Chris@0 208 }
Chris@0 209
Chris@0 210 /**
Chris@0 211 * {@inheritdoc}
Chris@0 212 */
Chris@0 213 public function garbageCollection() {
Chris@0 214 // APCu performs garbage collection automatically.
Chris@0 215 }
Chris@0 216
Chris@0 217 /**
Chris@0 218 * {@inheritdoc}
Chris@0 219 */
Chris@0 220 public function removeBin() {
Chris@0 221 apcu_delete($this->getIterator('/^' . preg_quote($this->binPrefix, '/') . '/'));
Chris@0 222 }
Chris@0 223
Chris@0 224 /**
Chris@0 225 * {@inheritdoc}
Chris@0 226 */
Chris@0 227 public function invalidate($cid) {
Chris@0 228 $this->invalidateMultiple([$cid]);
Chris@0 229 }
Chris@0 230
Chris@0 231 /**
Chris@0 232 * {@inheritdoc}
Chris@0 233 */
Chris@0 234 public function invalidateMultiple(array $cids) {
Chris@0 235 foreach ($this->getMultiple($cids) as $cache) {
Chris@0 236 $this->set($cache->cid, $cache, REQUEST_TIME - 1);
Chris@0 237 }
Chris@0 238 }
Chris@0 239
Chris@0 240 /**
Chris@0 241 * {@inheritdoc}
Chris@0 242 */
Chris@0 243 public function invalidateAll() {
Chris@0 244 foreach ($this->getAll() as $data) {
Chris@0 245 $cid = str_replace($this->binPrefix, '', $data['key']);
Chris@0 246 $this->set($cid, $data['value'], REQUEST_TIME - 1);
Chris@0 247 }
Chris@0 248 }
Chris@0 249
Chris@0 250 /**
Chris@0 251 * Instantiates and returns the APCUIterator class.
Chris@0 252 *
Chris@0 253 * @param mixed $search
Chris@0 254 * A PCRE regular expression that matches against APC key names, either as a
Chris@0 255 * string for a single regular expression, or as an array of regular
Chris@0 256 * expressions. Or, optionally pass in NULL to skip the search.
Chris@0 257 * @param int $format
Chris@0 258 * The desired format, as configured with one or more of the APC_ITER_*
Chris@0 259 * constants.
Chris@0 260 * @param int $chunk_size
Chris@0 261 * The chunk size. Must be a value greater than 0. The default value is 100.
Chris@0 262 * @param int $list
Chris@0 263 * The type to list. Either pass in APC_LIST_ACTIVE or APC_LIST_DELETED.
Chris@0 264 *
Chris@0 265 * @return \APCUIterator
Chris@0 266 */
Chris@0 267 protected function getIterator($search = NULL, $format = APC_ITER_ALL, $chunk_size = 100, $list = APC_LIST_ACTIVE) {
Chris@0 268 return new \APCUIterator($search, $format, $chunk_size, $list);
Chris@0 269 }
Chris@0 270
Chris@0 271 }