Skip to content

Commit eb2cd58

Browse files
committed
Made the code easier to read
1 parent bf2e511 commit eb2cd58

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
@@ -76,10 +76,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
7676
}
7777

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

8583
if ($etag = $this->getETag($cacheItem)) {
@@ -117,13 +115,14 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
117115
}
118116

119117
$maxAge = $this->getMaxAge($response);
118+
$currentTime = time();
120119
$cacheItem
121120
->expiresAfter($this->config['cache_lifetime'] + $maxAge)
122121
->set([
123122
'response' => $response,
124123
'body' => $body,
125-
'expiresAt' => time() + $maxAge,
126-
'createdAt' => time(),
124+
'expiresAt' => $currentTime + $maxAge,
125+
'createdAt' => $currentTime,
127126
'etag' => $response->getHeader('ETag'),
128127
]);
129128
$this->pool->save($cacheItem);
@@ -232,7 +231,7 @@ private function getMaxAge(ResponseInterface $response)
232231
private function configureOptions(OptionsResolver $resolver)
233232
{
234233
$resolver->setDefaults([
235-
'cache_lifetime' => 2592000, // 30 days
234+
'cache_lifetime' => 86400 * 30, // 30 days
236235
'default_ttl' => null,
237236
'respect_cache_headers' => true,
238237
'hash_algo' => 'sha1',
@@ -261,20 +260,23 @@ private function createResponseFromCacheItem(CacheItemInterface $cacheItem)
261260
}
262261

263262
/**
264-
* Get the timestamp when the cached response was stored.
263+
* Get the value of the "If-Modified-Since" header.
265264
*
266265
* @param CacheItemInterface $cacheItem
267266
*
268-
* @return int|null
267+
* @return string|null
269268
*/
270-
private function getModifiedAt(CacheItemInterface $cacheItem)
269+
private function getModifiedSinceHeaderValue(CacheItemInterface $cacheItem)
271270
{
272271
$data = $cacheItem->get();
273272
if (!isset($data['createdAt'])) {
274273
return;
275274
}
276275

277-
return $data['createdAt'];
276+
$modified = new \DateTime('@'.$data['createdAt']);
277+
$modified->setTimezone(new \DateTimeZone('GMT'));
278+
279+
return sprintf('%s GMT', $modified->format('l, d-M-y H:i:s'));
278280
}
279281

280282
/**

0 commit comments

Comments
 (0)