Skip to content

Commit cea6121

Browse files
committed
fixes
1 parent 8a2f41a commit cea6121

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

spec/CachePluginSpec.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
3535
$response->getHeader('Expires')->willReturn(array());
3636

3737
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
38-
$item->exists()->willReturn(false);
39-
$item->set($response, 60)->shouldBeCalled();
38+
$item->isHit()->willReturn(false);
39+
$item->set($response)->willReturn($item)->shouldBeCalled();
40+
$item->expiresAfter(60)->willReturn($item)->shouldBeCalled();
4041
$pool->save($item)->shouldBeCalled();
4142

4243
$next = function (RequestInterface $request) use ($response) {
@@ -55,7 +56,7 @@ function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheIte
5556
$response->getHeader('Expires')->willReturn(array());
5657

5758
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
58-
$item->exists()->willReturn(false);
59+
$item->isHit()->willReturn(false);
5960

6061
$next = function (RequestInterface $request) use ($response) {
6162
return new FulfilledPromise($response->getWrappedObject());
@@ -87,10 +88,11 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
8788
$response->getHeader('Expires')->willReturn(array());
8889

8990
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
90-
$item->exists()->willReturn(false);
91+
$item->isHit()->willReturn(false);
9192

9293
// 40-15 should be 25
93-
$item->set($response, 25)->shouldBeCalled();
94+
$item->set($response)->willReturn($item)->shouldBeCalled();
95+
$item->expiresAfter(25)->willReturn($item)->shouldBeCalled();
9496
$pool->save($item)->shouldBeCalled();
9597

9698
$next = function (RequestInterface $request) use ($response) {

src/CachePlugin.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
6060
$key = $this->createCacheKey($request);
6161
$cacheItem = $this->pool->getItem($key);
6262

63-
if ($cacheItem->exists()) {
63+
if ($cacheItem->isHit()) {
6464
// return cached response
6565
return $cacheItem->get();
6666
}
6767

6868
return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) {
6969
if ($this->isCacheable($response)) {
70-
$cacheItem->set($response, $this->getMaxAge($response));
70+
$cacheItem->set($response)
71+
->expiresAfter($this->getMaxAge($response));
7172
$this->pool->save($cacheItem);
7273
}
7374

@@ -132,11 +133,11 @@ private function createCacheKey(RequestInterface $request)
132133
}
133134

134135
/**
135-
* Get a ttl in seconds or as a \DateTime object.
136+
* Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl.
136137
*
137138
* @param ResponseInterface $response
138139
*
139-
* @return int|\DateTime
140+
* @return int|null
140141
*/
141142
private function getMaxAge(ResponseInterface $response)
142143
{
@@ -158,7 +159,7 @@ private function getMaxAge(ResponseInterface $response)
158159
// check for ttl in the Expires header
159160
$headers = $response->getHeader('Expires');
160161
foreach ($headers as $header) {
161-
return new \DateTime($header);
162+
return (new \DateTime($header))->getTimestamp() - (new \DateTime())->getTimestamp();
162163
}
163164

164165
return $this->defaultTtl;

0 commit comments

Comments
 (0)