annotate core/lib/Drupal/Core/Cache/ApcuBackend.php @ 19:fa3358dc1485 tip

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