Skip to content

Commit

Permalink
Merge pull request #40 from joomlatools/feature/21-caching
Browse files Browse the repository at this point in the history
Refactor cacheable behavior
  • Loading branch information
johanjanssens authored Jan 8, 2019
2 parents 13a9f02 + 000b565 commit b2ddce4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 55 deletions.
102 changes: 47 additions & 55 deletions code/site/components/com_pages/dispatcher/behavior/cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,16 @@ protected function _beforeDispatch(KDispatcherContextInterface $context)
$content = $this->_prepareContent($data['content']);
$headers = $this->_prepareHeaders($data['headers']);

if(!$this->getObject('lib:http.response', ['headers' => $headers])->isStale())
{
$this->getObject('response')
->setHeaders($headers)
->setContent($content);

$this->send();

return false;
$response = clone $this->getResponse();
$response
->setHeaders($headers)
->setContent($content);

//Send the response and terminate the request
if(!$response->isStale()) {
$response->send();
}
}
//Create a buffer to capture output for caching
else ob_start();
}
}

Expand All @@ -57,7 +54,7 @@ protected function _beforeSend(KDispatcherContextInterface $context)
if($this->isCacheable())
{
//Disable caching
if ($page = $context->request->query->get('page', 'url', false))
if ($page = $context->getRequest()->query->get('page', 'url', false))
{
$cache = $this->getObject('page.registry')
->getPage($page)
Expand All @@ -66,7 +63,7 @@ protected function _beforeSend(KDispatcherContextInterface $context)
if ($cache !== false)
{
if(is_int($cache)) {
$this->getMixer()->getResponse()->setMaxAge($cache);
$context->getResponse()->setMaxAge($cache);
}
}
else $this->getConfig()->cache = false;
Expand All @@ -78,29 +75,46 @@ protected function _beforeSend(KDispatcherContextInterface $context)

protected function _beforeTerminate(KDispatcherContextInterface $context)
{
$response = $this->getResponse();

//Proxy Koowa Output
if($this->isCacheable() && $this->getResponse()->isCacheable() && !$this->getResponse()->isStale())
if($this->isCacheable() && $response->isCacheable())
{
$data = array(
'headers' => $this->getResponse()->getHeaders(),
'content' => $this->getResponse()->getContent()
);
if($content = $response->getContent())
{
$data = array(
'headers' => $this->getResponse()->getHeaders(),
'content' => $content,
);

$this->_getCache()->store($data, $this->_getCacheKey());
$this->_getCache()->store($data, $this->_getCacheKey());
}
}
}

public function onAfterApplicationRespond(KEventInterface $event)
{
$response = $this->getResponse();

//Proxy Joomla Output
if($this->isCacheable() && $this->getResponse()->isCacheable() && !$this->getResponse()->isStale())
if($this->isCacheable() && $response->isCacheable())
{
$data = array(
'headers' => $this->getResponse()->getHeaders(),
'content' => $this->getResponse()->getContent()
);
if($content = $event->getTarget()->getBody())
{
$headers = array();
foreach (headers_list() as $header)
{
$parts = explode(':', $header, 2);
$headers[trim($parts[0])] = trim($parts[1]);
}

$data = array(
'headers' => $headers,
'content' => $content
);

$this->_getCache()->store($data, $this->_getCacheKey());
$this->_getCache()->store($data, $this->_getCacheKey());
}
}
}

Expand All @@ -109,9 +123,9 @@ protected function _getCache()
if (!$this->__cache)
{
$options = array(
'caching' => true,
'defaultgroup' => 'com_koowa.pages',
'lifetime' => 60*24*7, //1 week
'caching' => true,
'defaultgroup' => 'com_pages',
'lifetime' => 60*24*7, //1 week
);

$this->__cache = JCache::getInstance('output', $options);
Expand All @@ -122,37 +136,13 @@ protected function _getCache()

protected function _getCacheKey()
{
$url = $this->getRequest()->getUrl()->toString(KHttpUrl::HOST + KHttpUrl::PATH + KHttpUrl::QUERY);
$format = $this->getRequest()->getFormat();
$user = $this->getUser()->getId();
$url = $this->getRequest()->getUrl()->toString(KHttpUrl::HOST + KHttpUrl::PATH + KHttpUrl::QUERY);
$format = $this->getRequest()->getFormat();
$user = $this->getUser()->getId();

return crc32($url.$format.$user);
}

protected function _getContent()
{
return ob_get_clean();
}

protected function _getHeaders()
{
$headers = array();

//Remove headers set by Joomla
header_remove('Pragma');
header_remove('Last-Modified');
header_remove('Expires');

$headers = array();
foreach (headers_list() as $header)
{
$parts = explode(':', $header, 2);
$headers[trim($parts[0])] = trim($parts[1]);
}

return $headers;
}

protected function _prepareContent($content)
{
//Search for a token in the content and refresh it
Expand All @@ -165,6 +155,8 @@ protected function _prepareContent($content)

protected function _prepareHeaders($headers)
{
unset($headers['Expires']);

return $headers;
}
}
27 changes: 27 additions & 0 deletions code/site/components/com_pages/template/filter/form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Joomlatools Framework - https://www.joomlatools.com/developer/framework/
*
* @copyright Copyright (C) 2007 Johan Janssens and Timble CVBA. (http://www.timble.net)
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link https://github.com/joomlatools/joomlatools-framework for the canonical source repository
*/

class ComPagesTemplateFilterForm extends KTemplateFilterForm
{
/**
* Handle form replacements
*
* @param string
* @return $this
*/
public function filter(&$text)
{
//$this->_addMetatag($text);
$this->_addAction($text);
//$this->_addToken($text);
$this->_addQueryParameters($text);

return $this;
}
}

0 comments on commit b2ddce4

Please sign in to comment.