|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\phpfastcache\Cache; |
| 4 | + |
| 5 | +use Drupal\Component\Utility\Crypt; |
| 6 | +use Drupal\Core\Cache\Cache; |
| 7 | +use Drupal\Core\Cache\CacheBackendInterface; |
| 8 | +use Drupal\Core\Cache\DatabaseBackend; |
| 9 | +use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface; |
| 10 | + |
| 11 | + |
| 12 | +/** |
| 13 | + * Class PhpFastCacheService |
| 14 | + * |
| 15 | + * @todo Uncamelize class name... |
| 16 | + */ |
| 17 | +class PhpfastcacheBackend implements CacheBackendInterface { |
| 18 | + |
| 19 | + /** |
| 20 | + * @var ExtendedCacheItemPoolInterface |
| 21 | + */ |
| 22 | + protected $cachePool; |
| 23 | + |
| 24 | + /** |
| 25 | + * The name of the cache bin to use. |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected $bin; |
| 30 | + |
| 31 | + /** |
| 32 | + * Prefix for all keys in this cache bin. |
| 33 | + * |
| 34 | + * Includes the site-specific prefix in $sitePrefix. |
| 35 | + * |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + protected $binPrefix; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var array |
| 42 | + */ |
| 43 | + protected $settings; |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructs a new PhpFastCacheBackend instance. |
| 47 | + * |
| 48 | + * @param $bin string |
| 49 | + * The name of the cache bin. |
| 50 | + * @param ExtendedCacheItemPoolInterface $cachePool |
| 51 | + * @param array $settings |
| 52 | + */ |
| 53 | + public function __construct(string $bin, ExtendedCacheItemPoolInterface $cachePool, array $settings) { |
| 54 | + $this->cachePool = $cachePool; |
| 55 | + $this->bin = $bin; |
| 56 | + $this->binPrefix = 'pfc.' . $this->bin . '.'; |
| 57 | + $this->settings = $settings; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + /** |
| 62 | + * @inheritDoc |
| 63 | + */ |
| 64 | + public function get($cid, $allow_invalid = FALSE) { |
| 65 | + $item = $this->cachePool->getItem($this->normalizeCid($cid)); |
| 66 | + |
| 67 | + if ($item->isHit()) { |
| 68 | + $data = $item->get(); |
| 69 | + if (($data && $data->valid) || $allow_invalid) { |
| 70 | + return $data; |
| 71 | + } |
| 72 | + return FALSE; |
| 73 | + } |
| 74 | + |
| 75 | + if ($allow_invalid) { |
| 76 | + return FALSE; |
| 77 | + } |
| 78 | + |
| 79 | + return FALSE; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @inheritDoc |
| 84 | + */ |
| 85 | + public function getMultiple(&$cids, $allow_invalid = FALSE) { |
| 86 | + $cacheObjects = []; |
| 87 | + foreach ($cids as $cid) { |
| 88 | + $item = $this->get($cid, $allow_invalid); |
| 89 | + if ($item !== FALSE) { |
| 90 | + $cacheObjects[ $cid ] = $item; |
| 91 | + unset($cids[ $cid ]); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return $cacheObjects; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @inheritDoc |
| 100 | + */ |
| 101 | + public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) { |
| 102 | + $cacheObject = $this->getDrupalCacheStdObject(); |
| 103 | + $cacheObject->cid = $cid; |
| 104 | + $cacheObject->data = $data; |
| 105 | + $cacheObject->expire = $expire; |
| 106 | + $cacheObject->tags = $tags; |
| 107 | + $cacheObject->serialized = FALSE; |
| 108 | + $cacheObject->valid = TRUE; |
| 109 | + |
| 110 | + |
| 111 | + $cacheItem = $this->cachePool->getItem($this->normalizeCid($cid)); |
| 112 | + $cacheItem->set($cacheObject); |
| 113 | + // Not sure if its used atm |
| 114 | + // $cacheItem->setTags(array_map([$this, 'normalizeCid'], $tags)); |
| 115 | + |
| 116 | + if ($expire > 1000000000) { |
| 117 | + $date = new \DateTime; |
| 118 | + $date->setTimestamp($expire); |
| 119 | + $cacheItem->expiresAt($date); |
| 120 | + } |
| 121 | + else { |
| 122 | + $cacheItem->expiresAfter(((int) $expire === Cache::PERMANENT ? 60 * 60 * 24 * 365 : $expire)); |
| 123 | + } |
| 124 | + $this->cachePool->save($cacheItem); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @inheritDoc |
| 129 | + */ |
| 130 | + public function setMultiple(array $items) { |
| 131 | + foreach ($items as $cid => $item) { |
| 132 | + /** |
| 133 | + * Do not Normalize cid here as it |
| 134 | + * will be done in set() method |
| 135 | + */ |
| 136 | + $this->set($cid, $item[ 'data' ], $item[ 'expire' ] ?? Cache::PERMANENT, $item[ 'tags' ] ?? []); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @inheritDoc |
| 142 | + */ |
| 143 | + public function delete($cid) { |
| 144 | + $this->cachePool->deleteItem($this->normalizeCid($cid)); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @inheritDoc |
| 149 | + */ |
| 150 | + public function deleteMultiple(array $cids) { |
| 151 | + $this->cachePool->deleteItems(\array_map([$this, 'normalizeCid'], $cids)); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @inheritDoc |
| 156 | + */ |
| 157 | + public function deleteAll() { |
| 158 | + $this->cachePool->clear(); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @inheritDoc |
| 163 | + */ |
| 164 | + public function invalidate($cid) { |
| 165 | + $cacheItem = $this->cachePool->getItem($this->normalizeCid($cid)); |
| 166 | + $cacheObject = $cacheItem->get(); |
| 167 | + |
| 168 | + if (\is_object($cacheObject)) { |
| 169 | + $cacheObject->valid = FALSE; |
| 170 | + } |
| 171 | + |
| 172 | + $this->cachePool->save($cacheItem); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @inheritDoc |
| 177 | + */ |
| 178 | + public function invalidateMultiple(array $cids) { |
| 179 | + foreach ($cids as $cid) { |
| 180 | + $this->invalidate($cid); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @inheritDoc |
| 186 | + */ |
| 187 | + public function invalidateAll() { |
| 188 | + $this->cachePool->clear(); |
| 189 | + //throw new UnsupportedMethodException('Method invalidateAll() is currently not supported by PhpFastCache as there no way to list items in cache'); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @inheritDoc |
| 194 | + */ |
| 195 | + public function garbageCollection() { |
| 196 | + /** |
| 197 | + * Does not concerns PhpFastCache |
| 198 | + */ |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * @inheritDoc |
| 203 | + */ |
| 204 | + public function removeBin() { |
| 205 | + // TODO: Implement removeBin() method. |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * @return PhpfastcacheStoredObject |
| 210 | + */ |
| 211 | + protected function getDrupalCacheStdObject(): PhpfastcacheStoredObject { |
| 212 | + return new PhpfastcacheStoredObject(); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Borrowed from DatabaseBackend cache backend |
| 217 | + * Normalizes a cache ID in order to comply with database limitations. |
| 218 | + * |
| 219 | + * @param string $cid |
| 220 | + * The passed in cache ID. |
| 221 | + * |
| 222 | + * @return string |
| 223 | + * An ASCII-encoded cache ID that is at most 255 characters long. |
| 224 | + * @see DatabaseBackend::normalizeCid() |
| 225 | + */ |
| 226 | + protected function normalizeCid($cid): string { |
| 227 | + static $maxKeyLength = 64; |
| 228 | + |
| 229 | + /** |
| 230 | + * Add PhpFastCache Prefix |
| 231 | + */ |
| 232 | + $cid = ($this->settings[ 'phpfastcache_prefix' ] ?: 'd8') . '-' . $cid; |
| 233 | + |
| 234 | + /** |
| 235 | + * Nothing to do if the ID is a US ASCII string of 64 characters or less. |
| 236 | + */ |
| 237 | + $cid_is_ascii = mb_check_encoding($cid, 'ASCII'); |
| 238 | + if (\strlen($cid) <= $maxKeyLength && $cid_is_ascii) { |
| 239 | + return $this->replaceUnsupportedPsr6Characters($cid); |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Return a string that uses as much as possible of the original cache ID |
| 244 | + * with the hash appended. |
| 245 | + */ |
| 246 | + $hash = Crypt::hashBase64($cid); |
| 247 | + |
| 248 | + if (!$cid_is_ascii) { |
| 249 | + return $this->replaceUnsupportedPsr6Characters($hash); |
| 250 | + } |
| 251 | + |
| 252 | + return $this->replaceUnsupportedPsr6Characters( |
| 253 | + $this->binPrefix . substr($cid, 0, $maxKeyLength - \strlen($hash)) . $hash |
| 254 | + ); |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * @param $str |
| 259 | + * |
| 260 | + * @return mixed |
| 261 | + */ |
| 262 | + protected function replaceUnsupportedPsr6Characters($str) { |
| 263 | + return str_replace( |
| 264 | + ['{', '}', '(', ')', '/', '\\', '@', ':'], |
| 265 | + '_', |
| 266 | + $str |
| 267 | + ); |
| 268 | + } |
| 269 | +} |
0 commit comments