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: 11:
12: class DefaultCacheStorage implements CacheStorageInterface
13: {
14: 15: 16:
17: protected $cache;
18:
19: 20: 21:
22: protected $defaultTtl;
23:
24: 25: 26:
27: protected = array(
28: 'Connection', 'Keep-Alive', 'Proxy-Authenticate', 'Proxy-Authorization',
29: 'TE', 'Trailers', 'Transfer-Encoding', 'Upgrade', 'Set-Cookie', 'Set-Cookie2'
30: );
31:
32: 33: 34: 35:
36: public function __construct(CacheAdapterInterface $cache, $defaultTtl = 0)
37: {
38: $this->cache = $cache;
39: $this->defaultTtl = $defaultTtl;
40: }
41:
42: 43: 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:
56: foreach ($this->excludeResponseHeaders as $header) {
57: $response->removeHeader($header);
58: }
59:
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: 73:
74: public function delete($key)
75: {
76: $this->cache->delete($key);
77: }
78:
79: 80: 81:
82: public function fetch($key)
83: {
84: return $this->cache->fetch($key);
85: }
86: }
87: