From ba34a9f62cd4280520c5d21f4ec8624030a5b3e1 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Thu, 15 Feb 2007 03:17:50 +0000 Subject: [PATCH] Merging the changes for the database-driven template caching system from r809 on the 2.1.x branch back to the trunk --- TemplateFs.php | 53 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/TemplateFs.php b/TemplateFs.php index fe3d92d..f9ed091 100644 --- a/TemplateFs.php +++ b/TemplateFs.php @@ -32,7 +32,12 @@ require_once('ISSO/Functions.php'); * 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 @@ -54,6 +59,12 @@ class BSTemplateFs extends BSTemplate */ private $extension = 'tpl'; + /** + * The database table name for the template cache + * @var string + */ + private $dbCacheTable = null; + // ################################################################### /** * Constructor (overriding so we don't require the Db module) @@ -82,6 +93,17 @@ class BSTemplateFs extends BSTemplate $this->extension = $ext; } + // ################################################################### + /** + * Sets the name of the table to access for the datbase cache + * + * @param string DB table name + */ + public function setDatabaseCache($table) + { + $this->dbCacheTable = $table; + } + // ################################################################### /** * Takes an array of template names, loads them, and then stores a @@ -97,10 +119,35 @@ class BSTemplateFs extends BSTemplate } else { + $dbCache = array(); + if ($this->dbCacheTable) + { + $db =& BSRegister::GetType('Db'); + $cache = $db->query("SELECT * FROM {$this->dbCacheTable} WHERE filename IN ('" . implode("', '", $namearray) . "')"); + while ($tpl = $db->fetchArray($cache)) + { + $time = filemtime(BSRegister::GetAppPath() . $this->templateDir . $tpl['filename'] . '.' . $this->extension); + $template = $tpl['template']; + if ($time > $tpl['timestamp']) + { + $template = $this->_parseTemplate($this->_loadTemplate($tpl['filename'])); + $db->query("UPDATE {$this->dbCacheTable} SET template = '" . $db->escapeString($template) . "', timestamp = " . TIMENOW . " WHERE filename = '" . $tpl['filename'] . "'"); + $tpl['template'] = $template; + } + $dbCache["$tpl[filename]"] = $template; + } + } foreach ($namearray AS $name) { - $template = $this->_loadTemplate($name); - $template = $this->_parseTemplate($template); + if (isset($dbCache["$name"])) + { + $template = $dbCache["$name"]; + } + else + { + $template = $this->_parseTemplate($this->_loadTemplate($name)); + $db->query("INSERT INTO {$this->dbCacheTable} (filename, template, timestamp) VALUES ('$name', '" . $db->escapeString($template) . "', " . TIMENOW . ")"); + } $this->cache["$name"] = $template; $this->usage["$name"] = 0; } -- 2.22.5