Skip to content

Commit

Permalink
added a simple form of caching to the lookup class
Browse files Browse the repository at this point in the history
  • Loading branch information
kanduvisla committed Apr 3, 2012
1 parent 4ac380a commit 2de6935
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions symphony/lib/toolkit/class.lookup.php
Expand Up @@ -20,10 +20,18 @@ class Lookup
/* @var $_index SimpleXMLElement */
private $_index;

// Keep track of the type
private $_type;

// The path to the XML-files (workspace/pages and workspace/sections)
private $_path;

// The element name to use as root-tag
private $_element_name;

// Internal cache (for this class)
private $_cache;

/**
* Get the index
*
Expand All @@ -49,7 +57,7 @@ public static function index($type)
*/
private function __construct($type)
{
$this->_type = $type;
$this->_type = $type;
// Create an index:
switch($this->_type)
{
Expand Down Expand Up @@ -145,8 +153,12 @@ public function xpath($path, $singleValue = false)
*/
public function getId($hash)
{
return Symphony::Database()->fetchVar('id', 0,
sprintf('SELECT `id` FROM `tbl_lookup_pages` WHERE `hash` = \'%s\';', $hash));
if(!isset($this->_cache['hash'][$hash]))
{
$this->_cache['hash'][$hash] = Symphony::Database()->fetchVar('id', 0,
sprintf('SELECT `id` FROM `tbl_lookup_pages` WHERE `hash` = \'%s\';', $hash));
}
return $this->_cache['hash'][$hash];
}

/**
Expand All @@ -158,8 +170,12 @@ public function getId($hash)
*/
public function getHash($id)
{
return Symphony::Database()->fetchVar('hash', 0,
sprintf('SELECT `hash` FROM `tbl_lookup_pages` WHERE `id` = %d;', $id));
if(!isset($this->_cache['id'][$id]))
{
$this->_cache['id'][$id] = Symphony::Database()->fetchVar('hash', 0,
sprintf('SELECT `hash` FROM `tbl_lookup_pages` WHERE `id` = %d;', $id));
}
return $this->_cache['id'][$id];
}

/**
Expand Down Expand Up @@ -341,6 +357,10 @@ public function getMax($name)
*/
public function reIndex()
{
// Clear the cache:
$this->_cache = array('id' => array(), 'hash' => array());

// Build the index:
$this->_index = new SimpleXMLElement('<'.$this->_element_name.'/>');
$_pages = glob($this->_path);
foreach($_pages as $_page)
Expand Down

0 comments on commit 2de6935

Please sign in to comment.