Added a very nifty caching system for filesystem templates. Apparently, the main...
authorRobert Sesek <rsesek@bluestatic.org>
Thu, 15 Feb 2007 02:26:59 +0000 (02:26 +0000)
committerRobert Sesek <rsesek@bluestatic.org>
Thu, 15 Feb 2007 02:26:59 +0000 (02:26 +0000)
template_fs.php

index 98e0ec9a8a3a727c99295873dcd186615d2b9f62..d573eeef201af3f1bff19a8f288a2d340cfedc54 100644 (file)
@@ -32,7 +32,12 @@ $this->load('template', null);
 * File System Template System
 *
 * This framework merely replaces the template loading functions with
-* file-system based ones.
+* file-system based ones. It has an optional caching system in which
+* template data will remain stored in the database as long as the filesystem
+* file is modified. To do this, pass a table name to setDatabaseCache() and make sure
+* there's a DB module that has access to a table with this schema:
+*
+* CREATE TABLE template (filename VARCHAR (250) NOT NULL, template TEXT NOT NULL, timestamp INT NOT NULL);
 *
 * @author              Blue Static
 * @copyright   Copyright ©2002 - [#]year[#], Blue Static
@@ -56,6 +61,13 @@ class Template_FS extends Template
        */
        var $extension = 'tpl';
        
+       /**
+       * The database table name for the template cache
+       * @var string
+       * @access       private
+       */
+       var $dbCacheTable = null;
+       
        // ###################################################################
        /**
        * Constructor
@@ -100,6 +112,17 @@ class Template_FS extends Template
                $this->extension = $ext;
        }
        
+       // ###################################################################
+       /**
+       * Sets the name of the table to access for the datbase cache
+       *
+       * @param        string  DB table name
+       */
+       function setDatabaseCache($table)
+       {
+               $this->dbCacheTable = $table;
+       }
+       
        // ###################################################################
        /**
        * Takes an array of template names, loads them, and then stores a
@@ -117,10 +140,35 @@ class Template_FS extends Template
                }
                else
                {
+                       $dbCache = array();
+                       if ($this->dbCacheTable)
+                       {
+                               $db =& $this->registry->modules[ISSO_DB_LAYER];
+                               $cache = $db->query("SELECT * FROM {$this->dbCacheTable} WHERE filename IN ('" . implode("', '", $namearray) . "')");
+                               while ($tpl = $db->fetch_array($cache))
+                               {
+                                       $time = filemtime($this->registry->apppath . $this->templatedir . $tpl['filename'] . '.' . $this->extension);
+                                       $template = $tpl['template'];
+                                       if ($time > $tpl['timestamp'])
+                                       {
+                                               $template = $this->_parse($this->_load($tpl['filename']));
+                                               $db->query("UPDATE {$this->dbCacheTable} SET template = '" . $db->escape_string($template) . "', timestamp = " . TIMENOW . " WHERE filename = '" . $tpl['filename'] . "'");
+                                               $tpl['template'] = $template;
+                                       }
+                                       $dbCache["$tpl[filename]"] = $template;
+                               }
+                       }
                        foreach ($namearray AS $name)
                        {
-                               $template = $this->_load($name);
-                               $template = $this->_parse($template);
+                               if (isset($dbCache["$name"]))
+                               {
+                                       $template = $dbCache["$name"];
+                               }
+                               else
+                               {
+                                       $template = $this->_parse($this->_load($name));
+                                       $db->query("INSERT INTO {$this->dbCacheTable} (filename, template, timestamp) VALUES ('$name', '" . $db->escape_string($template) . "', " . TIMENOW . ")");
+                               }
                                $this->cache["$name"] = $template;
                                $this->usage["$name"] = 0;
                        }