Chris@0: bin = 'cache_' . $bin; Chris@0: $this->checksumProvider = $checksum_provider; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function get($cid, $allow_invalid = FALSE) { Chris@0: return $this->getByHash($this->normalizeCid($cid), $allow_invalid); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Fetch a cache item using a hashed cache ID. Chris@0: * Chris@0: * @param string $cidhash Chris@0: * The hashed version of the original cache ID after being normalized. Chris@0: * @param bool $allow_invalid Chris@0: * (optional) If TRUE, a cache item may be returned even if it is expired or Chris@0: * has been invalidated. Chris@0: * Chris@0: * @return bool|mixed Chris@0: */ Chris@0: protected function getByHash($cidhash, $allow_invalid = FALSE) { Chris@0: if ($file = $this->storage()->getFullPath($cidhash)) { Chris@0: $cache = @include $file; Chris@0: } Chris@0: if (isset($cache)) { Chris@0: return $this->prepareItem($cache, $allow_invalid); Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setMultiple(array $items) { Chris@0: foreach ($items as $cid => $item) { Chris@0: $this->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : []); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getMultiple(&$cids, $allow_invalid = FALSE) { Chris@0: $ret = []; Chris@0: Chris@0: foreach ($cids as $cid) { Chris@0: if ($item = $this->get($cid, $allow_invalid)) { Chris@0: $ret[$item->cid] = $item; Chris@0: } Chris@0: } Chris@0: Chris@0: $cids = array_diff($cids, array_keys($ret)); Chris@0: Chris@0: return $ret; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares a cached item. Chris@0: * Chris@0: * Checks that items are either permanent or did not expire, and returns data Chris@0: * as appropriate. Chris@0: * Chris@0: * @param object $cache Chris@18: * An item loaded from self::get() or self::getMultiple(). Chris@0: * @param bool $allow_invalid Chris@0: * If FALSE, the method returns FALSE if the cache item is not valid. Chris@0: * Chris@0: * @return mixed Chris@0: * The item with data as appropriate or FALSE if there is no Chris@0: * valid item to load. Chris@0: */ Chris@0: protected function prepareItem($cache, $allow_invalid) { Chris@0: if (!isset($cache->data)) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: // Check expire time. Chris@0: $cache->valid = $cache->expire == Cache::PERMANENT || $cache->expire >= REQUEST_TIME; Chris@0: Chris@0: // Check if invalidateTags() has been called with any of the item's tags. Chris@0: if (!$this->checksumProvider->isValid($cache->checksum, $cache->tags)) { Chris@0: $cache->valid = FALSE; Chris@0: } Chris@0: Chris@0: if (!$allow_invalid && !$cache->valid) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: return $cache; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) { Chris@14: assert(Inspector::assertAllStrings($tags), 'Cache Tags must be strings.'); Chris@14: Chris@0: $item = (object) [ Chris@0: 'cid' => $cid, Chris@0: 'data' => $data, Chris@0: 'created' => round(microtime(TRUE), 3), Chris@0: 'expire' => $expire, Chris@0: 'tags' => array_unique($tags), Chris@0: 'checksum' => $this->checksumProvider->getCurrentChecksum($tags), Chris@0: ]; Chris@0: $this->writeItem($this->normalizeCid($cid), $item); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function delete($cid) { Chris@0: $this->storage()->delete($this->normalizeCid($cid)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function deleteMultiple(array $cids) { Chris@0: foreach ($cids as $cid) { Chris@0: $this->delete($cid); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function deleteAll() { Chris@0: $this->storage()->deleteAll(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function invalidate($cid) { Chris@0: $this->invalidatebyHash($this->normalizeCid($cid)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Invalidate one cache item. Chris@0: * Chris@0: * @param string $cidhash Chris@0: * The hashed version of the original cache ID after being normalized. Chris@0: */ Chris@0: protected function invalidatebyHash($cidhash) { Chris@0: if ($item = $this->getByHash($cidhash)) { Chris@0: $item->expire = REQUEST_TIME - 1; Chris@0: $this->writeItem($cidhash, $item); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function invalidateMultiple(array $cids) { Chris@0: foreach ($cids as $cid) { Chris@0: $this->invalidate($cid); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function invalidateAll() { Chris@0: foreach ($this->storage()->listAll() as $cidhash) { Chris@0: $this->invalidatebyHash($cidhash); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function garbageCollection() { Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function removeBin() { Chris@0: $this->cache = []; Chris@0: $this->storage()->deleteAll(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Writes a cache item to PhpStorage. Chris@0: * Chris@0: * @param string $cidhash Chris@0: * The hashed version of the original cache ID after being normalized. Chris@0: * @param \stdClass $item Chris@0: * The cache item to store. Chris@0: */ Chris@0: protected function writeItem($cidhash, \stdClass $item) { Chris@0: $content = 'storage()->save($cidhash, $content); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the PHP code storage object to use. Chris@0: * Chris@0: * @return \Drupal\Component\PhpStorage\PhpStorageInterface Chris@0: */ Chris@0: protected function storage() { Chris@0: if (!isset($this->storage)) { Chris@0: $this->storage = PhpStorageFactory::get($this->bin); Chris@0: } Chris@0: return $this->storage; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures a normalized cache ID. Chris@0: * Chris@0: * @param string $cid Chris@0: * The passed in cache ID. Chris@0: * Chris@0: * @return string Chris@0: * A normalized cache ID. Chris@0: */ Chris@0: protected function normalizeCid($cid) { Chris@0: return Crypt::hashBase64($cid); Chris@0: } Chris@0: Chris@0: }