/** * Loads a cached resource * * @param ResourceInterface $resource * * @return array */ public function load(ResourceInterface $resource) { if (!$resource instanceof CacheResource) { return array(); } if (!$this->cache->has($resource->getName())) { return array(); } $formulas = unserialize($this->cache->get($resource->getName())); foreach ($formulas as $idx => $formula) { $formulas[$idx] = $this->restoreFormulaFilters($formula); } return $formulas; }
/** * Applies dump filters and returns the asset as a string. * * @param FilterInterface|null $additionalFilter * @return string */ public function dump(FilterInterface $additionalFilter = null) { $cacheKey = $this->getCacheKey($this->asset, $additionalFilter, 'dump'); if ($this->cache->has($cacheKey)) { return $this->cache->get($cacheKey); } $content = $this->asset->dump($additionalFilter); $this->cache->set($cacheKey, $content); return $content; }
/** * If we make it here then we have a cached version of this asset * found in the underlying $cache driver. So we will check the * header HTTP_IF_MODIFIED_SINCE and if that is not less than * the last time we cached ($lastModified) then we will exit * with 304 header. * * @param string $key * @return string */ public function get($key) { $lastModified = $this->getLastTimeModified($key); $modifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : 0; header('Last-Modified: ' . $lastModified); if ($modifiedSince >= $lastModified) { header('HTTP/1.0 304 Not Modified'); exit; } return $this->cache->get($key); }
/** * Searches a fresh cached file for the given file. * * @throws RuntimeException filesystem errors */ private function getCache($file) { if (null === $this->cache) { return null; } $key = md5($file); // File already present if ($this->cache->has($key)) { list($mtime, $path) = unserialize($this->cache->get($key)); if ($mtime === filemtime($file)) { return $path; } $delete = $this->directory . '/' . $path; if (file_exists($delete) && false === @unlink($delete)) { throw new \RuntimeException('Unable to remove file ' . $delete); } } return null; }