Permalink
Browse files

Add cache functionality

  • Loading branch information...
1 parent 577e51f commit d96d41136b9198bf0772bc3a953df07fcf53b230 @harrydeluxe harrydeluxe committed Feb 10, 2011
Showing with 96 additions and 4 deletions.
  1. +3 −0 Liquid.class.php
  2. +23 −0 lib/LiquidDocument.class.php
  3. +21 −1 lib/LiquidTemplate.class.php
  4. +49 −3 lib/Tag/LiquidTagInclude.class.php
View
@@ -104,6 +104,9 @@
defined('LIQUID_PATH') or define('LIQUID_PATH',dirname(__FILE__));
+defined('LIQUID_TMPPATH') or define('LIQUID_TMPPATH', null);
+defined('LIQUID_CACHE') or define('LIQUID_CACHE', false);
+
class Liquid
{
@@ -33,6 +33,29 @@ function __construct($tokens, &$file_system)
/**
+ * check for cached includes
+ *
+ * @return string
+ */
+ public function checkIncludes()
+ {
+ $return = false;
+ foreach($this->_nodelist as $token)
+ {
+ if(is_object($token))
+ {
+ if(get_class($token) == 'LiquidTagInclude')
+ {
+ if($token->checkIncludes() == true)
+ $return = true;
+ }
+ }
+ }
+ return $return;
+ }
+
+
+ /**
* There isn't a real delimiter
*
* @return string
@@ -86,7 +86,27 @@ public static function tokenize($source)
*/
public function parse($source)
{
- $this->root = new LiquidDocument(LiquidTemplate::tokenize($source), $this->file_system);
+ $parseNew = true;
+
+ $tmpname = LIQUID_TMPPATH.md5($source);
+
+ if(LIQUID_CACHE === true && is_file($tmpname))
+ {
+ $this->root = unserialize(file_get_contents($tmpname));
+ $parseNew = $this->root->checkIncludes();
+ }
+
+ if($parseNew)
+ {
+ $this->root = new LiquidDocument(LiquidTemplate::tokenize($source), $this->file_system);
+
+ if(LIQUID_CACHE === true)
+ {
+ if(!@file_put_contents($tmpname, serialize($this->root)))
+ throw new LiquidException("Tempfile failed to open stream");
+ }
+ }
+
return $this;
}
@@ -40,6 +40,9 @@ class LiquidTagInclude extends LiquidTag
* @var LiquidDocument The LiquidDocument that represents the included template
*/
private $document;
+
+
+ protected $_hash;
/**
@@ -87,15 +90,58 @@ function parse($tokens)
{
throw new LiquidException("No file system");
}
-
+
// read the source of the template and create a new sub document
$source = $this->file_system->read_template_file($this->template_name);
- $tokens = LiquidTemplate::tokenize($source);
- $this->document = new LiquidDocument($tokens, $this->file_system);
+
+ //$this->document = new LiquidDocument(LiquidTemplate::tokenize($source), $this->file_system);
+
+
+ $this->_hash = md5($source);
+
+ $parseNew = true;
+
+ $tmpname = LIQUID_TMPPATH.$this->_hash;
+
+ if(LIQUID_CACHE === true && is_file($tmpname))
+ {
+ $this->document = unserialize(file_get_contents($tmpname));
+ $parseNew = $this->document->checkIncludes();
+ }
+
+ if($parseNew)
+ {
+ $this->document = new LiquidDocument(LiquidTemplate::tokenize($source), $this->file_system);
+ if(LIQUID_CACHE === true)
+ {
+ if(!@file_put_contents($tmpname, serialize($this->document)))
+ throw new LiquidException("Tempfile failed to open stream");
+ }
+ }
}
/**
+ * check for cached includes
+ *
+ * @return string
+ */
+ public function checkIncludes()
+ {
+ if($this->document->checkIncludes() == true)
+ return true;
+
+ $source = $this->file_system->read_template_file($this->template_name);
+ $tmpname = LIQUID_TMPPATH.md5($source);
+
+ if(is_file($tmpname) && $this->_hash == md5($source))
+ return false;
+
+ return true;
+ }
+
+
+ /**
* Renders the node
*
* @param LiquidContext $context

0 comments on commit d96d411

Please sign in to comment.