Skip to content

Commit

Permalink
Improve Cache Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Narloch committed Dec 10, 2016
1 parent f4d6115 commit 4b888e8
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 171 deletions.
57 changes: 15 additions & 42 deletions libraries/joomla/cache/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Public cache handler
*
* @since 11.1
* @note As of 4.0 this class will be abstract
*/
class JCacheController
{
Expand Down Expand Up @@ -66,9 +67,7 @@ public function __construct($options)
*/
public function __call($name, $arguments)
{
$nazaj = call_user_func_array(array($this->cache, $name), $arguments);

return $nazaj;
return call_user_func_array(array($this->cache, $name), $arguments);
}

/**
Expand Down Expand Up @@ -97,13 +96,11 @@ public static function getInstance($type = 'output', $options = array())

$path = JPath::find(self::addIncludePath(), strtolower($type) . '.php');

if ($path === false)
if ($path !== false)
{
throw new RuntimeException('Unable to load Cache Controller: ' . $type, 500);
JLoader::register($class, $path);
}

JLoader::register($class, $path);

// The class should now be loaded
if (!class_exists($class))
{
Expand All @@ -114,34 +111,6 @@ public static function getInstance($type = 'output', $options = array())
return new $class($options);
}

/**
* Set caching enabled state
*
* @param boolean $enabled True to enable caching
*
* @return void
*
* @since 11.1
*/
public function setCaching($enabled)
{
$this->cache->setCaching($enabled);
}

/**
* Set cache lifetime
*
* @param integer $lt Cache lifetime
*
* @return void
*
* @since 11.1
*/
public function setLifeTime($lt)
{
$this->cache->setLifeTime($lt);
}

/**
* Add a directory where JCache should search for controllers. You may either pass a string or an array of directories.
*
Expand Down Expand Up @@ -178,6 +147,7 @@ public static function addIncludePath($path = '')
* @return mixed Boolean false on no result, cached object otherwise
*
* @since 11.1
* @deprecated 4.0 Implement own method in subclass
*/
public function get($id, $group = null)
{
Expand All @@ -187,12 +157,13 @@ public function get($id, $group = null)
{
$locktest = $this->cache->lock($id, $group);

if ($locktest->locked == true && $locktest->locklooped == true)
// If locklooped is true try to get the cached data again; it could exist now.
if ($locktest->locked === true && $locktest->locklooped === true)
{
$data = $this->cache->get($id, $group);
}

if ($locktest->locked == true)
if ($locktest->locked === true)
{
$this->cache->unlock($id, $group);
}
Expand All @@ -219,23 +190,25 @@ public function get($id, $group = null)
* @return boolean True if cache stored
*
* @since 11.1
* @deprecated 4.0 Implement own method in subclass
*/
public function store($data, $id, $group = null, $wrkarounds = true)
{
$locktest = $this->cache->lock($id, $group);

if ($locktest->locked == false && $locktest->locklooped == true)
if ($locktest->locked === false && $locktest->locklooped === true)
{
$locktest = $this->cache->lock($id, $group);
// We can not store data because another process is in the middle of saving
return false;
}

$success = $this->cache->store(serialize($data), $id, $group);
$result = $this->cache->store(serialize($data), $id, $group);

if ($locktest->locked == true)
if ($locktest->locked === true)
{
$this->cache->unlock($id, $group);
}

return $success;
return $result;
}
}
117 changes: 67 additions & 50 deletions libraries/joomla/cache/controller/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class JCacheControllerCallback extends JCacheController
* @return mixed Result of the callback
*
* @since 11.1
* @deprecated 4.0
*/
public function call()
{
Expand Down Expand Up @@ -88,86 +89,102 @@ public function get($callback, $args = array(), $id = false, $wrkarounds = false

$data = $this->cache->get($id);

$locktest = new stdClass;
$locktest->locked = null;
$locktest->locklooped = null;
$locktest = (object) array('locked' => null, 'locklooped' => null);

if ($data === false)
{
$locktest = $this->cache->lock($id);

if ($locktest->locked == true && $locktest->locklooped == true)
// If locklooped is true try to get the cached data again; it could exist now.
if ($locktest->locked === true && $locktest->locklooped === true)
{
$data = $this->cache->get($id);
}
}

$coptions = array();

if ($data !== false)
{
$cached = unserialize(trim($data));
$coptions['mergehead'] = isset($woptions['mergehead']) ? $woptions['mergehead'] : 0;
$output = ($wrkarounds == false) ? $cached['output'] : JCache::getWorkarounds($cached['output'], $coptions);
$result = $cached['result'];

if ($locktest->locked == true)
if ($locktest->locked === true)
{
$this->cache->unlock($id);
}
}
else
{
if (!is_array($args))
{
$referenceArgs = !empty($args) ? array(&$args) : array();
}
else
{
$referenceArgs = &$args;
}

if ($locktest->locked == false)
{
$locktest = $this->cache->lock($id);
}
$data = unserialize(trim($data));

if (isset($woptions['modulemode']) && $woptions['modulemode'] == 1)
if ($wrkarounds)
{
$document = JFactory::getDocument();
$coptions['modulemode'] = 1;
if (method_exists($document, 'getHeadData'))
{
$coptions['headerbefore'] = $document->getHeadData();
}
echo JCache::getWorkarounds(
$data['output'],
array('mergehead' => isset($woptions['mergehead']) ? $woptions['mergehead'] : 0)
);
}
else
{
$coptions['modulemode'] = 0;
echo $data['output'];
}

ob_start();
ob_implicit_flush(false);
return $data['result'];
}

$result = call_user_func_array($callback, $referenceArgs);
$output = ob_get_clean();
if (!is_array($args))
{
$referenceArgs = !empty($args) ? array(&$args) : array();
}
else
{
$referenceArgs = &$args;
}

$coptions['nopathway'] = isset($woptions['nopathway']) ? $woptions['nopathway'] : 1;
$coptions['nohead'] = isset($woptions['nohead']) ? $woptions['nohead'] : 1;
$coptions['nomodules'] = isset($woptions['nomodules']) ? $woptions['nomodules'] : 1;
if ($locktest->locked === false && $locktest->locklooped === true)
{
// We can not store data because another process is in the middle of saving
return call_user_func_array($callback, $referenceArgs);
}

$cached = array(
'output' => ($wrkarounds == false) ? $output : JCache::setWorkarounds($output, $coptions),
'result' => $result,
);
$coptions = array();

// Store the cache data
$this->cache->store(serialize($cached), $id);
if (isset($woptions['modulemode']) && $woptions['modulemode'] == 1)
{
$document = JFactory::getDocument();

if ($locktest->locked == true)
if (method_exists($document, 'getHeadData'))
{
$this->cache->unlock($id);
$coptions['headerbefore'] = $document->getHeadData();
}

$coptions['modulemode'] = 1;
}
else
{
$coptions['modulemode'] = 0;
}

$coptions['nopathway'] = isset($woptions['nopathway']) ? $woptions['nopathway'] : 1;
$coptions['nohead'] = isset($woptions['nohead']) ? $woptions['nohead'] : 1;
$coptions['nomodules'] = isset($woptions['nomodules']) ? $woptions['nomodules'] : 1;

ob_start();
ob_implicit_flush(false);

$result = call_user_func_array($callback, $referenceArgs);
$output = ob_get_clean();

$data = array('result' => $result);

if ($wrkarounds)
{
$data['output'] = JCache::setWorkarounds($output, $coptions);
}
else {
$data['output'] = $output;
}

// Store the cache data
$this->cache->store(serialize($data), $id);

if ($locktest->locked === true)
{
$this->cache->unlock($id);
}

echo $output;
Expand Down

0 comments on commit 4b888e8

Please sign in to comment.