@@ -74,8 +74,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
74
74
75
75
if ($cacheItem->isHit()) {
76
76
$data = $cacheItem->get();
77
- // The isset () is to be removed in 2.0.
78
- if (isset( $data['expiresAt']) && time() < $data['expiresAt']) {
77
+ // The array_key_exists () is to be removed in 2.0.
78
+ if (array_key_exists('expiresAt', $data) && ($data ['expiresAt'] === null || time() < $data['expiresAt']) ) {
79
79
// This item is still valid according to previous cache headers
80
80
return new FulfilledPromise($this->createResponseFromCacheItem($cacheItem));
81
81
}
@@ -103,8 +103,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
103
103
// The cached response we have is still valid
104
104
$data = $cacheItem->get();
105
105
$maxAge = $this->getMaxAge($response);
106
- $data['expiresAt'] = time() + $maxAge;
107
- $cacheItem->set($data)->expiresAfter($this->config['cache_lifetime'] + $maxAge);
106
+ $data['expiresAt'] = $this->calculateResponseExpiresAt( $maxAge) ;
107
+ $cacheItem->set($data)->expiresAfter($this->calculateCacheItemExpiresAfter( $maxAge) );
108
108
$this->pool->save($cacheItem);
109
109
110
110
return $this->createResponseFromCacheItem($cacheItem);
@@ -120,14 +120,13 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
120
120
}
121
121
122
122
$maxAge = $this->getMaxAge($response);
123
- $currentTime = time();
124
123
$cacheItem
125
- ->expiresAfter($this->config['cache_lifetime'] + $maxAge)
124
+ ->expiresAfter($this->calculateCacheItemExpiresAfter( $maxAge) )
126
125
->set([
127
126
'response' => $response,
128
127
'body' => $body,
129
- 'expiresAt' => $currentTime + $maxAge,
130
- 'createdAt' => $currentTime ,
128
+ 'expiresAt' => $this->calculateResponseExpiresAt( $maxAge) ,
129
+ 'createdAt' => time() ,
131
130
'etag' => $response->getHeader('ETag'),
132
131
]);
133
132
$this->pool->save($cacheItem);
@@ -137,6 +136,40 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
137
136
});
138
137
}
139
138
139
+ /**
140
+ * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be
141
+ * returned is $maxAge.
142
+ *
143
+ * @param int|null $maxAge
144
+ *
145
+ * @return int|null Unix system time passed to the PSR-6 cache
146
+ */
147
+ private function calculateCacheItemExpiresAfter($maxAge)
148
+ {
149
+ if ($this->config['cache_lifetime'] === null && $maxAge === null) {
150
+ return;
151
+ }
152
+
153
+ return $this->config['cache_lifetime'] + $maxAge;
154
+ }
155
+
156
+ /**
157
+ * Calculate the timestamp when a response expires. After that timestamp, we need to send a
158
+ * If-Modified-Since / If-None-Match request to validate the response.
159
+ *
160
+ * @param int|null $maxAge
161
+ *
162
+ * @return int|null Unix system time. A null value means that the response expires when the cache item expires
163
+ */
164
+ private function calculateResponseExpiresAt($maxAge)
165
+ {
166
+ if ($maxAge === null) {
167
+ return;
168
+ }
169
+
170
+ return time() + $maxAge;
171
+ }
172
+
140
173
/**
141
174
* Verify that we can cache this response.
142
175
*
@@ -237,12 +270,12 @@ private function configureOptions(OptionsResolver $resolver)
237
270
{
238
271
$resolver->setDefaults([
239
272
'cache_lifetime' => 86400 * 30, // 30 days
240
- 'default_ttl' => null ,
273
+ 'default_ttl' => 0 ,
241
274
'respect_cache_headers' => true,
242
275
'hash_algo' => 'sha1',
243
276
]);
244
277
245
- $resolver->setAllowedTypes('cache_lifetime', 'int');
278
+ $resolver->setAllowedTypes('cache_lifetime', [ 'int', 'null'] );
246
279
$resolver->setAllowedTypes('default_ttl', ['int', 'null']);
247
280
$resolver->setAllowedTypes('respect_cache_headers', 'bool');
248
281
$resolver->setAllowedValues('hash_algo', hash_algos());
0 commit comments