Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • CachePlugin
  • CallbackCacheKeyProvider
  • CallbackCanCacheStrategy
  • DefaultCacheKeyProvider
  • DefaultCacheStorage
  • DefaultCanCacheStrategy
  • DefaultRevalidation
  • DenyRevalidation
  • SkipRevalidation

Interfaces

  • CacheKeyProviderInterface
  • CacheStorageInterface
  • CanCacheStrategyInterface
  • RevalidationInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
 1: <?php
 2: 
 3: namespace Guzzle\Plugin\Cache;
 4: 
 5: use Guzzle\Cache\CacheAdapterInterface;
 6: use Guzzle\Http\ClientInterface;
 7: use Guzzle\Http\Message\Response;
 8: 
 9: /**
10:  * Default cache storage implementation
11:  */
12: class DefaultCacheStorage implements CacheStorageInterface
13: {
14:     /**
15:      * @var CacheAdapterInterface Cache used to store cache data
16:      */
17:     protected $cache;
18: 
19:     /**
20:      * @var int Default cache TTL
21:      */
22:     protected $defaultTtl;
23: 
24:     /**
25:      * @var array Headers are excluded from the caching (see RFC 2616:13.5.1)
26:      */
27:     protected $excludeResponseHeaders = array(
28:         'Connection', 'Keep-Alive', 'Proxy-Authenticate', 'Proxy-Authorization',
29:         'TE', 'Trailers', 'Transfer-Encoding', 'Upgrade', 'Set-Cookie', 'Set-Cookie2'
30:     );
31: 
32:     /**
33:      * @param CacheAdapterInterface $cache      Cache used to store cache data
34:      * @param int                   $defaultTtl Default cache TTL
35:      */
36:     public function __construct(CacheAdapterInterface $cache, $defaultTtl = 0)
37:     {
38:         $this->cache = $cache;
39:         $this->defaultTtl = $defaultTtl;
40:     }
41: 
42:     /**
43:      * {@inheritdoc}
44:      */
45:     public function cache($key, Response $response, $ttl = null)
46:     {
47:         if ($ttl === null) {
48:             $ttl = $this->defaultTtl;
49:         }
50: 
51:         $ttl += $response->getMaxAge();
52: 
53:         if ($ttl) {
54:             $response->setHeader('X-Guzzle-Cache', "key={$key}; ttl={$ttl}");
55:             // Remove excluded headers from the response  (see RFC 2616:13.5.1)
56:             foreach ($this->excludeResponseHeaders as $header) {
57:                 $response->removeHeader($header);
58:             }
59:             // Add a Date header to the response if none is set (for validation)
60:             if (!$response->getDate()) {
61:                 $response->setHeader('Date', gmdate(ClientInterface::HTTP_DATE));
62:             }
63:             $this->cache->save(
64:                 $key,
65:                 array($response->getStatusCode(), $response->getHeaders()->getAll(), $response->getBody(true)),
66:                 $ttl
67:             );
68:         }
69:     }
70: 
71:     /**
72:      * {@inheritdoc}
73:      */
74:     public function delete($key)
75:     {
76:         $this->cache->delete($key);
77:     }
78: 
79:     /**
80:      * {@inheritdoc}
81:      */
82:     public function fetch($key)
83:     {
84:         return $this->cache->fetch($key);
85:     }
86: }
87: 
php-coveralls API documentation generated by ApiGen 2.8.0