Skip to content

Commit

Permalink
#304 - Refactor cacheable behavior
Browse files Browse the repository at this point in the history
Rename isCached() to loadCache() and return the cache as an array
  • Loading branch information
johanjanssens committed Apr 6, 2020
1 parent 088f23f commit 56212d0
Showing 1 changed file with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions code/site/components/com_pages/dispatcher/behavior/cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,15 @@ protected function _beforeDispatch(KDispatcherContextInterface $context)
{
if($this->isCacheable())
{
if($file = $this->isCached($this->getCacheKey()))
if($cache = $this->loadCache())
{
//Require the file from cache
$data = require $file;

$content = $this->_prepareContent($data['content']);
$headers = $this->_prepareHeaders($data['headers']);
$status = $data['status'];
$page = $data['page'];
$collections = $data['collections'];

$response = clone $this->getResponse();
$response
->setStatus($status)
->setHeaders($headers)
->setContent($content);
->setStatus($cache['status'])
->setHeaders($cache['headers'])
->setContent($cache['content']);

if(!$this->isBypass() && !$response->isStale() && $this->isValid($page, $collections) === true)
if(!$this->isBypass() && !$response->isStale() && $this->isValid($cache['page'], $cache['collections']) === true)
{
$response->getHeaders()->set('Cache-Status', self::CACHE_HIT);

Expand All @@ -93,8 +84,8 @@ protected function _beforeDispatch(KDispatcherContextInterface $context)
}
else $response->getHeaders()->set('Age', max(time() - $response->getDate()->format('U'), 0));

$data['headers'] = $response->getHeaders()->toArray();
$this->storeCache($this->getCacheKey(), $data);
$cache['headers'] = $response->getHeaders()->toArray();
$this->storeCache($this->getCacheKey(), $cache);

//Terminate the request
$response->send();
Expand Down Expand Up @@ -255,6 +246,38 @@ public function getCollections()
return (array) $this->__collections;
}

public function loadCache($key = null)
{
static $cache;

if(!$key) {
$key = $this->getCacheKey();
}

if(!isset($cache[$key]))
{
if($this->getConfig()->cache)
{
$hash = crc32($key.PHP_VERSION);
$file = $this->getConfig()->cache_path.'/response_'.$hash.'.php';

if(is_file($file))
{
$data = require $file;

$data['content'] = $this->_prepareContent($data['content']);
$data['headers'] = $this->_prepareHeaders($data['headers']);

$cache[$key] = $data;
}
else $cache[$key] = false;
}
else $cache[$key] = false;
}

return $cache[$key];
}

public function storeCache($key, $data)
{
if($this->getConfig()->cache)
Expand Down Expand Up @@ -291,20 +314,6 @@ public function storeCache($key, $data)
return false;
}

public function isCached($key)
{
$result = false;

if($this->getConfig()->cache)
{
$hash = crc32($key.PHP_VERSION);
$cache = $this->getConfig()->cache_path.'/response_'.$hash.'.php';
$result = is_file($cache) ? $cache : false;
}

return $result;
}

public function isValid($page, $collections = array())
{
$valid = true;
Expand Down

0 comments on commit 56212d0

Please sign in to comment.