Skip to content

Commit

Permalink
Merge pull request #834 from joomlatools/feature/833-timing
Browse files Browse the repository at this point in the history
Add timing info to http caching
  • Loading branch information
johanjanssens authored Dec 4, 2021
2 parents 3eb04c7 + d0aebd4 commit 7e52311
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 9 additions & 1 deletion code/dispatcher/behavior/cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,17 @@ protected function _actionCache(KDispatcherContextInterface $context)
$response->setDate(new DateTime('now'));
$response->setLastModified($response->getDate());

//If the cache exists and it has not been modified do not reset the Last-Modified date
$time = null;
if($start = $response->getRequest()->getTime()) {
$time = (microtime(true) - $start) * 1000;
}

//If the cache exists and it has not been modified do not reset the Last-Modified date and timing
if($this->loadCache() && $this->isIdentical())
{
$cache = $this->loadCache();

//Do not reset LastModified
$response->setLastModified(new DateTime($cache['headers']['Last-Modified']));
}

Expand All @@ -201,6 +208,7 @@ protected function _actionCache(KDispatcherContextInterface $context)
'headers' => $response->headers->toArray(),
'content' => (string) $response->getContent(),
'language' => $this->getRoute()->getPage()->language,
'timing' => (int) $time
);

$result = $this->storeCache($data);
Expand Down
5 changes: 4 additions & 1 deletion code/http/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function send(KHttpRequestInterface $request)
//Revalidate the cache
if(!$valid)
{
$start = microtime(true);

$response = parent::send($request);

if($cache && ($response->isError() || $response->isNotModified()))
Expand All @@ -82,7 +84,8 @@ public function send(KHttpRequestInterface $request)
'status' => $response->getStatusCode(),
'format' => $response->getFormat(),
'headers' => $response->getHeaders()->toArray(),
'content' => $response->getContent()
'content' => $response->getContent(),
'timing' => (int) ((microtime(true) - $start) * 1000),
]);
}
}
Expand Down

0 comments on commit 7e52311

Please sign in to comment.