1 | <?php |
||
20 | class GreedyCacheStrategy extends PrivateCacheStrategy |
||
21 | { |
||
22 | const HEADER_TTL = 'X-Kevinrob-GuzzleCache-TTL'; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | protected $defaultTtl; |
||
28 | |||
29 | /** |
||
30 | * @var KeyValueHttpHeader |
||
31 | */ |
||
32 | private $varyHeaders; |
||
33 | |||
34 | 10 | public function __construct(CacheStorageInterface $cache = null, $defaultTtl, KeyValueHttpHeader $varyHeaders = null) |
|
40 | |||
41 | 9 | protected function getCacheKey(RequestInterface $request, KeyValueHttpHeader $varyHeaders = null) |
|
62 | |||
63 | 9 | public function cache(RequestInterface $request, ResponseInterface $response) |
|
82 | |||
83 | 9 | protected function getCacheObject(RequestInterface $request, ResponseInterface $response) |
|
84 | { |
||
85 | 9 | if (!array_key_exists($response->getStatusCode(), $this->statusAccepted)) { |
|
86 | // Don't cache it |
||
87 | 1 | return null; |
|
88 | } |
||
89 | |||
90 | 8 | if (null !== $this->varyHeaders && $this->varyHeaders->has('*')) { |
|
91 | // This will never match with a request |
||
92 | return; |
||
93 | } |
||
94 | |||
95 | 8 | $response = $response->withoutHeader('Etag')->withoutHeader('Last-Modified'); |
|
96 | |||
97 | 8 | $ttl = $this->defaultTtl; |
|
98 | 8 | if ($request->hasHeader(self::HEADER_TTL)) { |
|
99 | 1 | $ttlHeaderValues = $request->getHeader(self::HEADER_TTL); |
|
100 | 1 | $ttl = (int)reset($ttlHeaderValues); |
|
101 | 1 | } |
|
102 | |||
103 | 8 | return new CacheEntry($request->withoutHeader(self::HEADER_TTL), $response, new \DateTime(sprintf('+%d seconds', $ttl))); |
|
104 | } |
||
105 | |||
106 | 9 | public function fetch(RequestInterface $request) |
|
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function delete(RequestInterface $request) |
||
119 | } |
||
120 |