Skip to content

Commit 718e6c1

Browse files
committed
Made the code easier to read
1 parent d4a5133 commit 718e6c1

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/CachePlugin.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
7575
}
7676

7777
// Add headers to ask the server if this cache is still valid
78-
if ($mod = $this->getModifiedAt($cacheItem)) {
79-
$mod = new \DateTime('@'.$mod);
80-
$mod->setTimezone(new \DateTimeZone('GMT'));
81-
$request = $request->withHeader('If-Modified-Since', sprintf('%s GMT', $mod->format('l, d-M-y H:i:s')));
78+
if ($modifiedSinceValue = $this->getModifiedSinceHeaderValue($cacheItem)) {
79+
$request = $request->withHeader('If-Modified-Since', $modifiedSinceValue);
8280
}
8381

8482
if ($etag = $this->getETag($cacheItem)) {
@@ -113,13 +111,14 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
113111
}
114112

115113
$maxAge = $this->getMaxAge($response);
114+
$currentTime = time();
116115
$cacheItem
117116
->expiresAfter($this->config['cache_lifetime'] + $maxAge)
118117
->set([
119118
'response' => $response,
120119
'body' => $body,
121-
'expiresAt' => time() + $maxAge,
122-
'createdAt' => time(),
120+
'expiresAt' => $currentTime + $maxAge,
121+
'createdAt' => $currentTime,
123122
'etag' => $response->getHeader('ETag'),
124123
]);
125124
$this->pool->save($cacheItem);
@@ -228,7 +227,7 @@ private function getMaxAge(ResponseInterface $response)
228227
private function configureOptions(OptionsResolver $resolver)
229228
{
230229
$resolver->setDefaults([
231-
'cache_lifetime' => 2592000, // 30 days
230+
'cache_lifetime' => 86400 * 30, // 30 days
232231
'default_ttl' => null,
233232
'respect_cache_headers' => true,
234233
]);
@@ -255,20 +254,23 @@ private function createResponseFromCacheItem(CacheItemInterface $cacheItem)
255254
}
256255

257256
/**
258-
* Get the timestamp when the cached response was stored.
257+
* Get the value of the "If-Modified-Since" header.
259258
*
260259
* @param CacheItemInterface $cacheItem
261260
*
262-
* @return int|null
261+
* @return string|null
263262
*/
264-
private function getModifiedAt(CacheItemInterface $cacheItem)
263+
private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem)
265264
{
266265
$data = $cacheItem->get();
267266
if (!isset($data['createdAt'])) {
268267
return;
269268
}
270269

271-
return $data['createdAt'];
270+
$modified = new \DateTime('@'.$data['createdAt']);
271+
$modified->setTimezone(new \DateTimeZone('GMT'));
272+
273+
return sprintf('%s GMT', $modified->format('l, d-M-y H:i:s'));
272274
}
273275

274276
/**

0 commit comments

Comments
 (0)