Chris@0: prefix = $prefix; Chris@0: $this->collection = $collection; Chris@0: Chris@0: if (isset($cache_backend_class)) { Chris@0: $this->cache = new $cache_backend_class($cache_backend_configuration); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function get($filepath) { Chris@0: $filepaths = [$filepath]; Chris@0: $cached = $this->getMultiple($filepaths); Chris@0: return isset($cached[$filepath]) ? $cached[$filepath] : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getMultiple(array $filepaths) { Chris@0: $file_data = []; Chris@0: $remaining_cids = []; Chris@0: Chris@0: // First load from the static cache what we can. Chris@0: foreach ($filepaths as $filepath) { Chris@0: if (!file_exists($filepath)) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $realpath = realpath($filepath); Chris@0: // If the file exists but realpath returns nothing, it is using a stream Chris@0: // wrapper, those are not supported. Chris@0: if (empty($realpath)) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $cid = $this->prefix . ':' . $this->collection . ':' . $realpath; Chris@0: if (isset(static::$cached[$cid]) && static::$cached[$cid]['mtime'] == filemtime($filepath)) { Chris@0: $file_data[$filepath] = static::$cached[$cid]['data']; Chris@0: } Chris@0: else { Chris@0: // Collect a list of cache IDs that we still need to fetch from cache Chris@0: // backend. Chris@0: $remaining_cids[$cid] = $filepath; Chris@0: } Chris@0: } Chris@0: Chris@0: // If there are any cache IDs left to fetch from the cache backend. Chris@0: if ($remaining_cids && $this->cache) { Chris@0: $cache_results = $this->cache->fetch(array_keys($remaining_cids)) ?: []; Chris@0: foreach ($cache_results as $cid => $cached) { Chris@0: $filepath = $remaining_cids[$cid]; Chris@0: if ($cached['mtime'] == filemtime($filepath)) { Chris@0: $file_data[$cached['filepath']] = $cached['data']; Chris@0: static::$cached[$cid] = $cached; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $file_data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function set($filepath, $data) { Chris@0: $realpath = realpath($filepath); Chris@0: $cached = [ Chris@0: 'mtime' => filemtime($filepath), Chris@0: 'filepath' => $filepath, Chris@0: 'data' => $data, Chris@0: ]; Chris@0: Chris@0: $cid = $this->prefix . ':' . $this->collection . ':' . $realpath; Chris@0: static::$cached[$cid] = $cached; Chris@0: if ($this->cache) { Chris@0: $this->cache->store($cid, $cached); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function delete($filepath) { Chris@0: $realpath = realpath($filepath); Chris@0: $cid = $this->prefix . ':' . $this->collection . ':' . $realpath; Chris@0: Chris@0: unset(static::$cached[$cid]); Chris@0: if ($this->cache) { Chris@0: $this->cache->delete($cid); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resets the static cache. Chris@0: * Chris@0: * @todo Replace this once https://www.drupal.org/node/2260187 is in. Chris@0: */ Chris@0: public static function reset() { Chris@0: static::$cached = []; Chris@0: } Chris@0: Chris@0: }