Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Change Log

## UNRELEASED

### Changed

- The default value for ``default_ttl`` is changed from ``null`` to ``0``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as null did not work as expected anyways, i think this change is ok. there only is a problem if somebody explicitly created the plugin with (null, false) - but imo that falls into the category of a bugfix that can be a BC break if you relied on the bug.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed


### 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

Expand Down
47 changes: 37 additions & 10 deletions src/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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->calculateResponseExpiresAt($maxAge);
$cacheItem->set($data)->expiresAfter($this->calculateCacheItemExpiresAfter($maxAge));
$this->pool->save($cacheItem);

return $this->createResponseFromCacheItem($cacheItem);
Expand All @@ -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->calculateResponseExpiresAt($maxAge),
'createdAt' => time(),
'etag' => $response->getHeader('ETag'),
]);
$this->pool->save($cacheItem);
Expand All @@ -137,6 +136,34 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
});
}

/**
* @param int|null $maxAge
*
* @return int|null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unix system time? @see PSR for caching?

*/
private function calculateCacheItemExpiresAfter($maxAge)
{
if ($this->config['cache_lifetime'] === null && $maxAge === null) {
return;
}

return $this->config['cache_lifetime'] + $maxAge;
}

/**
* @param int|null $maxAge
*
* @return int|null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unix system time?

*/
private function calculateResponseExpiresAt($maxAge)
{
if ($maxAge === null) {
return;
}

return time() + $maxAge;
}

/**
* Verify that we can cache this response.
*
Expand Down Expand Up @@ -237,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());
Expand Down