Skip to content

Commit 4682a15

Browse files
authored
Merge 32e1257 into a9dbc04
2 parents a9dbc04 + 32e1257 commit 4682a15

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Change Log
22

3+
## UNRELEASED
4+
5+
### Changed
6+
7+
- The default value for ``default_ttl`` is changed from ``null`` to ``0``.
8+
9+
### Fixed
10+
11+
- Issue when you use `respect_cache_headers=>false` in combination with `default_ttl=>null`.
12+
- We allow ``cache_lifetime`` to be set to ``null``.
313

414
## 1.1.0 - 2016-08-04
515

src/CachePlugin.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
7474

7575
if ($cacheItem->isHit()) {
7676
$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'])) {
7979
// This item is still valid according to previous cache headers
8080
return new FulfilledPromise($this->createResponseFromCacheItem($cacheItem));
8181
}
@@ -103,8 +103,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
103103
// The cached response we have is still valid
104104
$data = $cacheItem->get();
105105
$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));
108108
$this->pool->save($cacheItem);
109109

110110
return $this->createResponseFromCacheItem($cacheItem);
@@ -120,14 +120,13 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
120120
}
121121

122122
$maxAge = $this->getMaxAge($response);
123-
$currentTime = time();
124123
$cacheItem
125-
->expiresAfter($this->config['cache_lifetime'] + $maxAge)
124+
->expiresAfter($this->calculateCacheItemExpiresAfter($maxAge))
126125
->set([
127126
'response' => $response,
128127
'body' => $body,
129-
'expiresAt' => $currentTime + $maxAge,
130-
'createdAt' => $currentTime,
128+
'expiresAt' => $this->calculateResponseExpiresAt($maxAge),
129+
'createdAt' => time(),
131130
'etag' => $response->getHeader('ETag'),
132131
]);
133132
$this->pool->save($cacheItem);
@@ -137,6 +136,34 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
137136
});
138137
}
139138

139+
/**
140+
* @param int|null $maxAge
141+
*
142+
* @return int|null
143+
*/
144+
private function calculateCacheItemExpiresAfter($maxAge)
145+
{
146+
if ($this->config['cache_lifetime'] === null && $maxAge === null) {
147+
return;
148+
}
149+
150+
return $this->config['cache_lifetime'] + $maxAge;
151+
}
152+
153+
/**
154+
* @param int|null $maxAge
155+
*
156+
* @return int|null
157+
*/
158+
private function calculateResponseExpiresAt($maxAge)
159+
{
160+
if ($maxAge === null) {
161+
return;
162+
}
163+
164+
return time() + $maxAge;
165+
}
166+
140167
/**
141168
* Verify that we can cache this response.
142169
*
@@ -237,12 +264,12 @@ private function configureOptions(OptionsResolver $resolver)
237264
{
238265
$resolver->setDefaults([
239266
'cache_lifetime' => 86400 * 30, // 30 days
240-
'default_ttl' => null,
267+
'default_ttl' => 0,
241268
'respect_cache_headers' => true,
242269
'hash_algo' => 'sha1',
243270
]);
244271

245-
$resolver->setAllowedTypes('cache_lifetime', 'int');
272+
$resolver->setAllowedTypes('cache_lifetime', ['int', 'null']);
246273
$resolver->setAllowedTypes('default_ttl', ['int', 'null']);
247274
$resolver->setAllowedTypes('respect_cache_headers', 'bool');
248275
$resolver->setAllowedValues('hash_algo', hash_algos());

0 commit comments

Comments
 (0)