From a747977667818789e0bb496fda5b6acb2458dfb4 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 13 Aug 2016 11:41:20 +0200 Subject: [PATCH 01/11] Allow a cache item to be stored forever --- src/CachePlugin.php | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 07f9a7b..b4aa065 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -74,8 +74,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl if ($cacheItem->isHit()) { $data = $cacheItem->get(); - // The isset() is to be removed in 2.0. - if (isset($data['expiresAt']) && time() < $data['expiresAt']) { + // The array_key_exists() is to be removed in 2.0. + if (array_key_exists('expiresAt', $data) && ($data['expiresAt'] === null || time() < $data['expiresAt'])) { // This item is still valid according to previous cache headers return new FulfilledPromise($this->createResponseFromCacheItem($cacheItem)); } @@ -103,8 +103,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl // The cached response we have is still valid $data = $cacheItem->get(); $maxAge = $this->getMaxAge($response); - $data['expiresAt'] = time() + $maxAge; - $cacheItem->set($data)->expiresAfter($this->config['cache_lifetime'] + $maxAge); + $data['expiresAt'] = $this->getResponseExpiresAt($maxAge); + $cacheItem->set($data)->expiresAfter($this->calculateCacheItemExpiresAfter($maxAge)); $this->pool->save($cacheItem); return $this->createResponseFromCacheItem($cacheItem); @@ -120,14 +120,13 @@ public function handleRequest(RequestInterface $request, callable $next, callabl } $maxAge = $this->getMaxAge($response); - $currentTime = time(); $cacheItem - ->expiresAfter($this->config['cache_lifetime'] + $maxAge) + ->expiresAfter($this->calculateCacheItemExpiresAfter($maxAge)) ->set([ 'response' => $response, 'body' => $body, - 'expiresAt' => $currentTime + $maxAge, - 'createdAt' => $currentTime, + 'expiresAt' => $this->getResponseExpiresAt($maxAge), + 'createdAt' => time(), 'etag' => $response->getHeader('ETag'), ]); $this->pool->save($cacheItem); @@ -137,6 +136,34 @@ public function handleRequest(RequestInterface $request, callable $next, callabl }); } + /** + * @param int|null $maxAge + * + * @return int|null + */ + private function calculateCacheItemExpiresAfter($maxAge) + { + if ($this->config['cache_lifetime'] === null && $maxAge === null) { + return null; + } + + return $this->config['cache_lifetime'] + $maxAge; + } + + /** + * @param int|null $maxAge + * + * @return int|null + */ + private function getResponseExpiresAt($maxAge) + { + if ($maxAge !== null) { + return time() + $maxAge; + } + + return null; + } + /** * Verify that we can cache this response. * From e14bbdc378df9da92b3adc94d845f0b6668713ba Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 13 Aug 2016 11:50:45 +0200 Subject: [PATCH 02/11] Minor --- CHANGELOG.md | 5 +++++ src/CachePlugin.php | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8acd670..ecdf048 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## UNRELEASED + +### Fixed + +- Issue when you use `respect_cache_headers=>false` in combination with `default_ttl=>null`. ## 1.1.0 - 2016-08-04 diff --git a/src/CachePlugin.php b/src/CachePlugin.php index b4aa065..5b3fee6 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -144,7 +144,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl private function calculateCacheItemExpiresAfter($maxAge) { if ($this->config['cache_lifetime'] === null && $maxAge === null) { - return null; + return; } return $this->config['cache_lifetime'] + $maxAge; @@ -157,11 +157,11 @@ private function calculateCacheItemExpiresAfter($maxAge) */ private function getResponseExpiresAt($maxAge) { - if ($maxAge !== null) { - return time() + $maxAge; + if ($maxAge === null) { + return; } - return null; + return time() + $maxAge; } /** From 67f5c1a9af3df5ecfd2a3b32d6bfd403348076c0 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 13 Aug 2016 18:57:47 +0200 Subject: [PATCH 03/11] Rename function --- src/CachePlugin.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 5b3fee6..c805eab 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -103,7 +103,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl // The cached response we have is still valid $data = $cacheItem->get(); $maxAge = $this->getMaxAge($response); - $data['expiresAt'] = $this->getResponseExpiresAt($maxAge); + $data['expiresAt'] = $this->calculateResponseExpiresAt($maxAge); $cacheItem->set($data)->expiresAfter($this->calculateCacheItemExpiresAfter($maxAge)); $this->pool->save($cacheItem); @@ -125,7 +125,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl ->set([ 'response' => $response, 'body' => $body, - 'expiresAt' => $this->getResponseExpiresAt($maxAge), + 'expiresAt' => $this->calculateResponseExpiresAt($maxAge), 'createdAt' => time(), 'etag' => $response->getHeader('ETag'), ]); @@ -155,7 +155,7 @@ private function calculateCacheItemExpiresAfter($maxAge) * * @return int|null */ - private function getResponseExpiresAt($maxAge) + private function calculateResponseExpiresAt($maxAge) { if ($maxAge === null) { return; From 32e1257dcce60fd2ac133f2a1de2c8bc73d7f91a Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 14 Aug 2016 00:44:14 +0200 Subject: [PATCH 04/11] Changed default_ttl to 0 --- CHANGELOG.md | 5 +++++ src/CachePlugin.php | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ecdf048..1d5ef4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,14 @@ ## UNRELEASED +### Changed + +- The default value for ``default_ttl`` is changed from ``null`` to ``0``. + ### Fixed - Issue when you use `respect_cache_headers=>false` in combination with `default_ttl=>null`. +- We allow ``cache_lifetime`` to be set to ``null``. ## 1.1.0 - 2016-08-04 diff --git a/src/CachePlugin.php b/src/CachePlugin.php index c805eab..4e1368b 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -264,12 +264,12 @@ private function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'cache_lifetime' => 86400 * 30, // 30 days - 'default_ttl' => null, + 'default_ttl' => 0, 'respect_cache_headers' => true, 'hash_algo' => 'sha1', ]); - $resolver->setAllowedTypes('cache_lifetime', 'int'); + $resolver->setAllowedTypes('cache_lifetime', ['int', 'null']); $resolver->setAllowedTypes('default_ttl', ['int', 'null']); $resolver->setAllowedTypes('respect_cache_headers', 'bool'); $resolver->setAllowedValues('hash_algo', hash_algos()); From bd697b9b2af9b9474337de9d8b93cee5d3677365 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 15 Aug 2016 09:30:41 +0200 Subject: [PATCH 05/11] Added doc blocks --- src/CachePlugin.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 4e1368b..1ec0b3f 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -137,9 +137,12 @@ public function handleRequest(RequestInterface $request, callable $next, callabl } /** + * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value return after + * the calculation is $maxAge. + * * @param int|null $maxAge * - * @return int|null + * @return int|null Unix system time */ private function calculateCacheItemExpiresAfter($maxAge) { @@ -151,6 +154,9 @@ private function calculateCacheItemExpiresAfter($maxAge) } /** + * Calculate the timestamp when a response expires. After that timestamp, we need to send a + * If-Modified-Since / If-None-Match request to validate the response + * * @param int|null $maxAge * * @return int|null From 7dfeaa18a6144267a490ac81aca99d382aae6d89 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 15 Aug 2016 09:33:47 +0200 Subject: [PATCH 06/11] minor --- src/CachePlugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 1ec0b3f..a7a2801 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -142,7 +142,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl * * @param int|null $maxAge * - * @return int|null Unix system time + * @return int|null Unix system time. @see PSR6 for caching */ private function calculateCacheItemExpiresAfter($maxAge) { @@ -159,7 +159,7 @@ private function calculateCacheItemExpiresAfter($maxAge) * * @param int|null $maxAge * - * @return int|null + * @return int|null Unix system time */ private function calculateResponseExpiresAt($maxAge) { From db845535467c3ea29003d494f1e0bb7e07d4d35f Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 15 Aug 2016 09:40:32 +0200 Subject: [PATCH 07/11] style --- src/CachePlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index a7a2801..2eb8d42 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -155,7 +155,7 @@ private function calculateCacheItemExpiresAfter($maxAge) /** * Calculate the timestamp when a response expires. After that timestamp, we need to send a - * If-Modified-Since / If-None-Match request to validate the response + * If-Modified-Since / If-None-Match request to validate the response. * * @param int|null $maxAge * From 135c132638cbe7c9d98ad7667cacd247ad5ea4ff Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 15 Aug 2016 09:41:47 +0200 Subject: [PATCH 08/11] comment --- src/CachePlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 2eb8d42..e8ad609 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -142,7 +142,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl * * @param int|null $maxAge * - * @return int|null Unix system time. @see PSR6 for caching + * @return int|null Unix system time passed to the PSR-6 cache */ private function calculateCacheItemExpiresAfter($maxAge) { From ac289f90c221ffd10f833a6819ec0ff1994f2cd0 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 15 Aug 2016 09:42:37 +0200 Subject: [PATCH 09/11] comment --- src/CachePlugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index e8ad609..00ba34e 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -137,8 +137,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl } /** - * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value return after - * the calculation is $maxAge. + * Calculate the timestamp when this cache item should be dropped from the cache. The lowest value that can be + * returned is $maxAge. * * @param int|null $maxAge * From 7c5da23bbab0411be85a811c9001dd6f770e99af Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 15 Aug 2016 09:47:22 +0200 Subject: [PATCH 10/11] comment --- src/CachePlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 00ba34e..48ebe92 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -159,7 +159,7 @@ private function calculateCacheItemExpiresAfter($maxAge) * * @param int|null $maxAge * - * @return int|null Unix system time + * @return int|null Unix system time. A null value means that the response expires when the cache item expires. */ private function calculateResponseExpiresAt($maxAge) { From b1fee5d0ec68a3312484df88e299b0e124809216 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 15 Aug 2016 09:48:43 +0200 Subject: [PATCH 11/11] period --- src/CachePlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 48ebe92..5053aad 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -159,7 +159,7 @@ private function calculateCacheItemExpiresAfter($maxAge) * * @param int|null $maxAge * - * @return int|null Unix system time. A null value means that the response expires when the cache item expires. + * @return int|null Unix system time. A null value means that the response expires when the cache item expires */ private function calculateResponseExpiresAt($maxAge) {