Skip to content
This repository was archived by the owner on Jun 27, 2022. It is now read-only.

Commit 6e31045

Browse files
committed
Increment the age of the response #59
1 parent 51431a5 commit 6e31045

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/CacheSubscriber.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ public function onComplete(CompleteEvent $event)
164164
&& call_user_func($this->canCache, $request)
165165
&& Utils::canCacheResponse($response)
166166
) {
167+
// Store the date when the response was cached
168+
$response->setHeader('X-Guzzle-Cache-Date', gmdate('D, d M Y H:i:s T', time()));
167169
$this->storage->cache($request, $response);
168170
}
169171

src/Utils.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ public static function getDirective(MessageInterface $message, $part)
4444
public static function getResponseAge(ResponseInterface $response)
4545
{
4646
if ($response->hasHeader('Age')) {
47-
return (int) $response->getHeader('Age');
47+
$age = (int) $response->getHeader('Age');
48+
49+
// Increment the age based on the X-Guzzle-Cache-Date
50+
if ($cacheDate = $response->getHeader('X-Guzzle-Cache-Date')) {
51+
$age += (time() - strtotime($cacheDate));
52+
}
53+
54+
return $age;
4855
}
4956

5057
$date = strtotime($response->getHeader('Date') ?: 'now');

0 commit comments

Comments
 (0)