@@ -30,6 +30,13 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
30
30
{
31
31
private $ cacheable = true ;
32
32
private $ embeddedResponses = 0 ;
33
+ private $ cacheDirectives = array (
34
+ 'no-cache ' => false ,
35
+ 'no-store ' => false ,
36
+ 'must-revalidate ' => false ,
37
+ 'private ' => false ,
38
+ 'proxy-revalidate ' => false ,
39
+ );
33
40
private $ ttls = array ();
34
41
private $ maxAges = array ();
35
42
private $ isNotCacheableResponseEmbedded = false ;
@@ -41,6 +48,17 @@ public function add(Response $response)
41
48
{
42
49
if (!$ response ->isFresh () || !$ response ->isCacheable ()) {
43
50
$ this ->cacheable = false ;
51
+
52
+ $ hasCacheDirective = $ response ->headers ->hasCacheControlDirective ('public ' );
53
+ foreach ($ this ->cacheDirectives as $ directive => $ status ) {
54
+ if ($ response ->headers ->hasCacheControlDirective ($ directive )) {
55
+ $ this ->cacheDirectives [$ directive ] = $ hasCacheDirective = true ;
56
+ }
57
+ }
58
+
59
+ if (!$ hasCacheDirective || !$ response ->isFresh ()) {
60
+ $ this ->cacheDirectives ['no-cache ' ] = $ this ->cacheDirectives ['must-revalidate ' ] = true ;
61
+ }
44
62
} else {
45
63
$ maxAge = $ response ->getMaxAge ();
46
64
$ this ->ttls [] = $ response ->getTtl ();
@@ -72,19 +90,14 @@ public function update(Response $response)
72
90
$ response ->setLastModified (null );
73
91
}
74
92
75
- if (!$ response ->isFresh ()) {
76
- $ this ->cacheable = false ;
77
- }
93
+ $ this ->add ($ response );
78
94
79
95
if (!$ this ->cacheable ) {
80
- $ response ->headers ->set ('Cache-Control ' , ' no-cache, must-revalidate ' );
96
+ $ response ->headers ->set ('Cache-Control ' , implode ( ' , ' , array_keys ( array_filter ( $ this -> cacheDirectives ))) );
81
97
82
98
return ;
83
99
}
84
100
85
- $ this ->ttls [] = $ response ->getTtl ();
86
- $ this ->maxAges [] = $ response ->getMaxAge ();
87
-
88
101
if ($ this ->isNotCacheableResponseEmbedded ) {
89
102
$ response ->headers ->removeCacheControlDirective ('s-maxage ' );
90
103
} elseif (null !== $ maxAge = min ($ this ->maxAges )) {
0 commit comments