namespace Guzzle\Plugin\Cache;
use Guzzle\Cache\CacheAdapterFactory;
use Guzzle\Cache\CacheAdapterInterface;
use Guzzle\Http\EntityBodyInterface;
use Guzzle\Http\Message\MessageInterface;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Message\Response;
* Default cache storage implementation
class DefaultCacheStorage implements CacheStorageInterface
/** @var CacheAdapterInterface Cache used to store cache data */
/** @var int Default cache TTL */
* @param mixed $cache Cache used to store cache data
* @param string $keyPrefix Provide an optional key prefix to prefix on all cache keys
* @param int $defaultTtl Default cache TTL
public function __construct($cache, $keyPrefix = '', $defaultTtl = 3600)
$this->cache = CacheAdapterFactory::fromCache($cache);
$this->defaultTtl = $defaultTtl;
$this->keyPrefix = $keyPrefix;
public function cache(RequestInterface $request, Response $response)
$overrideTtl = $request->getParams()->get('cache.override_ttl');
$maxAge = $response->getMaxAge();
$ttl = $this->defaultTtl;
if ($cacheControl = $response->getHeader('Cache-Control')) {
$stale = $cacheControl->getDirective('stale-if-error');
} else if (is_numeric($stale)) {
// Determine which manifest key should be used
$key = $this->getCacheKey($request);
$persistedRequest = $this->persistHeaders($request);
if ($manifest = $this->cache->fetch($key)) {
// Determine which cache entries should still be in the cache
$vary = $response->getVary();
foreach (unserialize($manifest) as $entry) {
// Check if the entry is expired
if ($entry[4] < $currentTime) {
$entry[1]['vary'] = isset($entry[1]['vary']) ? $entry[1]['vary'] : '';
if ($vary != $entry[1]['vary'] || !$this->requestsMatch($vary, $entry[0], $persistedRequest)) {
// Persist the response body if needed
if ($response->getBody() && $response->getBody()->getContentLength() > 0) {
$bodyDigest = $this->getBodyKey($request->getUrl(), $response->getBody());
$this->cache->save($bodyDigest, (string) $response->getBody(), $ttl);
array_unshift($entries, array(
$this->persistHeaders($response),
$response->getStatusCode(),
$this->cache->save($key, serialize($entries));
public function delete(RequestInterface $request)
$key = $this->getCacheKey($request);
if ($entries = $this->cache->fetch($key)) {
// Delete each cached body
foreach (unserialize($entries) as $entry) {
$this->cache->delete($entry[3]);
$this->cache->delete($key);
public function purge($url)
foreach (array('GET', 'HEAD', 'POST', 'PUT', 'DELETE') as $method) {
$this->delete(new Request($method, $url));
public function fetch(RequestInterface $request)
$key = $this->getCacheKey($request);
if (!($entries = $this->cache->fetch($key))) {
$headers = $this->persistHeaders($request);
$entries = unserialize($entries);
foreach ($entries as $index => $entry) {
if ($this->requestsMatch(isset($entry[1]['vary']) ? $entry[1]['vary'] : '', $headers, $entry[0])) {
// Ensure that the response is not expired
if ($match[4] < time()) {
$response = new Response($match[2], $match[1]);
if ($body = $this->cache->fetch($match[3])) {
$response->setBody($body);
// The response is not valid because the body was somehow deleted
// Remove the entry from the metadata and update the cache
$this->cache->save($key, serialize($entries));
$this->cache->delete($key);
* Hash a request URL into a string that returns cache metadata
* @param RequestInterface $request
protected function getCacheKey(RequestInterface $request)
// Allow cache.key_filter to trim down the URL cache key by removing generate query string values (e.g. auth)
if ($filter = $request->getParams()->get('cache.key_filter')) {
$url = $request->getUrl(true);
foreach (explode(',', $filter) as $remove) {
$url->getQuery()->remove(trim($remove));
$url = $request->getUrl();
return $this->keyPrefix . md5($request->getMethod() . ' ' . $url);
* Create a cache key for a response's body
* @param string $url URL of the entry
* @param EntityBodyInterface $body Response body
protected function getBodyKey($url, EntityBodyInterface $body)
return $this->keyPrefix . md5($url) . $body->getContentMd5();
* Determines whether two Request HTTP header sets are non-varying
* @param string $vary Response vary header
* @param array $r1 HTTP header array
* @param array $r2 HTTP header array
private function requestsMatch($vary, $r1, $r2)
foreach (explode(',', $vary) as $header) {
$key = trim(strtolower($header));
$v1 = isset($r1[$key]) ? $r1[$key] : null;
$v2 = isset($r2[$key]) ? $r2[$key] : null;
* Creates an array of cacheable and normalized message headers
* @param MessageInterface $message
private function persistHeaders(MessageInterface $message)
// Headers are excluded from the caching (see RFC 2616:13.5.1)
'proxy-authenticate' => true,
'proxy-authorization' => true,
'transfer-encoding' => true,
// Clone the response to not destroy any necessary headers when caching
$headers = $message->getHeaders()->getAll();
$headers = array_diff_key($headers, $noCache);
// Cast the headers to a string
$headers = array_map(function ($h) { return (string) $h; }, $headers);