@@ -28,13 +28,17 @@ class CachePlugin implements Plugin
28
28
private $ defaultTtl ;
29
29
30
30
/**
31
- * Look at the cache headers to know how long this response is going to be cached.
31
+ * Look at the cache headers to know whether this response may be cached and to
32
+ * decide how it can be cached.
32
33
*
33
- * @var bool
34
+ * @var bool Defaults to true
34
35
*/
35
36
private $ respectCacheHeaders ;
36
37
37
38
/**
39
+ * Available options are
40
+ * - respect_cache_headers: Whether to look at the cache directives or ignore them.
41
+ *
38
42
* @param CacheItemPoolInterface $pool
39
43
* @param array $options
40
44
*/
@@ -86,21 +90,26 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
86
90
*/
87
91
protected function isCacheable (ResponseInterface $ response )
88
92
{
89
- $ cachableCodes = [200 , 203 , 300 , 301 , 302 , 404 , 410 ];
90
- $ privateHeaders = $ this ->getCacheControlDirective ($ response , 'no-store ' ) || $ this ->getCacheControlDirective ($ response , 'private ' );
93
+ if (!in_array ($ response ->getStatusCode (), [200 , 203 , 300 , 301 , 302 , 404 , 410 ])) {
94
+ return false ;
95
+ }
96
+ if (!$ this ->respectCacheHeaders ) {
97
+ return true ;
98
+ }
99
+ if ($ this ->getCacheControlDirective ($ response , 'no-store ' ) || $ this ->getCacheControlDirective ($ response , 'private ' )) {
100
+ return false ;
101
+ }
91
102
92
- // If http status code is cachable and if we respect the headers, make sure there is no private cache headers.
93
- return in_array ($ response ->getStatusCode (), $ cachableCodes ) && !($ this ->respectCacheHeaders && $ privateHeaders );
103
+ return true ;
94
104
}
95
105
96
106
/**
97
- * Returns the value of a parameter in the cache control header. If not found we return false. If found with no
98
- * value return true.
107
+ * Get the value of a parameter in the cache control header.
99
108
*
100
109
* @param ResponseInterface $response
101
- * @param string $name
110
+ * @param string $name The field of Cache-Control to fetch
102
111
*
103
- * @return bool|string
112
+ * @return bool|string The value of the directive, true if directive without value, false if directive not present.
104
113
*/
105
114
private function getCacheControlDirective (ResponseInterface $ response , $ name )
106
115
{
0 commit comments