Index: trunk/bin/acl.cli.php
===================================================================
--- trunk/bin/acl.cli.php	(revision 171)
+++ trunk/bin/acl.cli.php	(revision 172)
@@ -116,4 +116,8 @@
     break;
 
+case 'flush' :
+    echo $acl->initDB(true) ? "Ok\n" : "Error!\n";
+    break;
+
 case 'grant' :
     $aro = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : null;
@@ -195,4 +199,5 @@
 <?php echo $_SERVER['argv'][0]; ?> rmaco <aco_object>
 <?php echo $_SERVER['argv'][0]; ?> rmaxo <axo_object>
+<?php echo $_SERVER['argv'][0]; ?> flush
 <?php echo $_SERVER['argv'][0]; ?> grant <aro_object> [aco_object] [axo_object]
 <?php echo $_SERVER['argv'][0]; ?> revoke <aro_object> [aco_object] [axo_object]
Index: trunk/lib/ACL.inc.php
===================================================================
--- trunk/lib/ACL.inc.php	(revision 171)
+++ trunk/lib/ACL.inc.php	(revision 172)
@@ -12,8 +12,13 @@
 */
 
+require_once dirname(__FILE__) . '/Cache.inc.php';
+
 class ACL {
 
     // Configuration parameters for this object.
     var $_params = array(
+        
+        // If false nothing will be cached or retreived. Useful for testing realtime data requests.
+        'enable_cache' => true,
 
         // Automatically create table and verify columns. Better set to false after site launch.
@@ -27,4 +32,8 @@
     {
 		$app =& App::getInstance();
+
+        // Configure the cache object.
+        $this->cache = new Cache('acl');
+        $this->cache->setParam(array('enabled' => true));
 
         // Get create tables config from global context.
@@ -170,4 +179,5 @@
         }
         $_db_tested = true;
+        return true;
     }
 
@@ -219,5 +229,5 @@
         $qid = $db->query("SELECT 1 FROM $tbl WHERE name = '" . $db->escapeString($name) . "'");
         if (mysql_num_rows($qid) > 0) {
-            $app->logMsg(sprintf('Cannot add %s node, name exists: %s', $type, $name), LOG_WARNING, __FILE__, __LINE__);
+            $app->logMsg(sprintf('Cannot add %s node, already exists: %s', $type, $name), LOG_NOTICE, __FILE__, __LINE__);
             return false;
         }
@@ -299,5 +309,5 @@
         $qid = $db->query("SELECT lft, rgt FROM $tbl WHERE name = '" . $db->escapeString($name) . "'");
         if (!list($lft, $rgt) = mysql_fetch_row($qid)) {
-            $app->logMsg(sprintf('Cannot delete nonexistant %s name: %s', $type, $name), LOG_WARNING, __FILE__, __LINE__);
+            $app->logMsg(sprintf('Cannot delete nonexistant %s name: %s', $type, $name), LOG_NOTICE, __FILE__, __LINE__);
             return false;
         }
@@ -412,20 +422,28 @@
         $aco = is_null($aco) ? 'root' : $aco;
         $axo = is_null($axo) ? 'root' : $axo;
-
-        $qid = $db->query("
-            SELECT acl_tbl.access
-            FROM acl_tbl
-            LEFT JOIN aro_tbl ON (acl_tbl.aro_id = aro_tbl.aro_id)
-            LEFT JOIN aco_tbl ON (acl_tbl.aco_id = aco_tbl.aco_id)
-            LEFT JOIN axo_tbl ON (acl_tbl.axo_id = axo_tbl.axo_id)
-            WHERE aro_tbl.lft <= (SELECT lft FROM aro_tbl WHERE name = '" . $db->escapeString($aro) . "')
-            AND aco_tbl.lft <= (SELECT lft FROM aco_tbl WHERE name = '" . $db->escapeString($aco) . "')
-            AND axo_tbl.lft <= (SELECT lft FROM axo_tbl WHERE name = '" . $db->escapeString($axo) . "')
-            ORDER BY aro_tbl.aro_id DESC, aco_tbl.aco_id DESC, axo_tbl.axo_id DESC
-            LIMIT 1
-        ");
-        if (!list($access) = mysql_fetch_row($qid)) {
-            $app->logMsg(sprintf('Access denyed: %s -> %s -> %s. No records found.', $aro, $aco, $axo), LOG_DEBUG, __FILE__, __LINE__);
-            return false;
+        
+        $cache_hash = $aro . '|' . $aco . '|' . $axo;
+        if ($this->cache->exists($cache_hash) && true === $this->getParam('enable_cache')) {
+            // Access value is cached.
+            $access = $this->cache->get($cache_hash);
+        } else {
+            // Retreive access value from db.
+            $qid = $db->query("
+                SELECT acl_tbl.access
+                FROM acl_tbl
+                LEFT JOIN aro_tbl ON (acl_tbl.aro_id = aro_tbl.aro_id)
+                LEFT JOIN aco_tbl ON (acl_tbl.aco_id = aco_tbl.aco_id)
+                LEFT JOIN axo_tbl ON (acl_tbl.axo_id = axo_tbl.axo_id)
+                WHERE aro_tbl.lft <= (SELECT lft FROM aro_tbl WHERE name = '" . $db->escapeString($aro) . "')
+                AND aco_tbl.lft <= (SELECT lft FROM aco_tbl WHERE name = '" . $db->escapeString($aco) . "')
+                AND axo_tbl.lft <= (SELECT lft FROM axo_tbl WHERE name = '" . $db->escapeString($axo) . "')
+                ORDER BY aro_tbl.aro_id DESC, aco_tbl.aco_id DESC, axo_tbl.axo_id DESC
+                LIMIT 1
+            ");
+            if (!list($access) = mysql_fetch_row($qid)) {
+                $app->logMsg(sprintf('Access denyed: %s -> %s -> %s. No records found.', $aro, $aco, $axo), LOG_DEBUG, __FILE__, __LINE__);
+                return false;
+            }
+            $this->cache->set($cache_hash, $access);
         }
         
Index: trunk/lib/App.inc.php
===================================================================
--- trunk/lib/App.inc.php	(revision 171)
+++ trunk/lib/App.inc.php	(revision 172)
@@ -156,4 +156,9 @@
         // Initialize default parameters.
         $this->_params = array_merge($this->_params, $this->_param_defaults);
+        
+        // Begin timing script.
+        require_once dirname(__FILE__) . '/ScriptTimer.inc.php';
+        $this->timer = new ScriptTimer();
+        $this->timer->start('_app');
     }
 
@@ -334,7 +339,11 @@
         restore_include_path();
         $this->running = false;
+        $num_queries = 0;
         if (true === $this->getParam('enable_db')) {
+            $num_queries = $this->db->numQueries();
             $this->db->close();
         }
+        $this->timer->stop('_app');
+        $this->logMsg(sprintf('Script ended gracefully. Execution time: %s. Number of db queries: %s.', $this->timer->getTime('_app'), $num_queries), LOG_DEBUG, __FILE__, __LINE__);
     }
 
Index: trunk/lib/Cache.inc.php
===================================================================
--- trunk/lib/Cache.inc.php	(revision 171)
+++ trunk/lib/Cache.inc.php	(revision 172)
@@ -47,5 +47,12 @@
     function Cache($namespace='')
     {
+        $app =& App::getInstance();
+        
         $this->_ns = $namespace;
+
+        if (true !== $app->getParam('enable_session')) {
+            $app->logMsg('Cache disabled, enable_session is false.', LOG_DEBUG, __FILE__, __LINE__);
+            $this->setParam(array('enabled' => false));
+        }
         
         if (!isset($_SESSION['_cache'][$this->_ns])) {
@@ -191,4 +198,5 @@
             return unserialize($_SESSION['_cache'][$this->_ns][$keyhash]);
         } else {
+            $app->logMsg(sprintf('Missing %s from cache.', $key), LOG_DEBUG, __FILE__, __LINE__);
             return false;
         }
Index: trunk/lib/DB.inc.php
===================================================================
--- trunk/lib/DB.inc.php	(revision 171)
+++ trunk/lib/DB.inc.php	(revision 172)
@@ -17,4 +17,7 @@
     // Database handle.
     var $dbh;
+    
+    // Count how many queries run during the whole instance.
+    var $_query_count = 0;
 
     // Hash of DB parameters.
@@ -229,5 +232,4 @@
     function query($query, $debug=false)
     {    
-        static $_query_count = 0;
         $app =& App::getInstance();
 
@@ -236,8 +238,9 @@
         }
 
-        $_query_count++;
+        $this->_query_count++;
+
         $debugqry = preg_replace("/\n[\t ]+/", "\n", $query);
         if ($this->getParam('db_always_debug') || $debug) {
-            echo "<!-- ----------------- Query $_query_count ---------------------\n$debugqry\n-->\n";
+            echo "<!-- ----------------- Query $this->_query_count ---------------------\n$debugqry\n-->\n";
         }
 
@@ -342,4 +345,18 @@
         }
     }
+    
+    /*
+    * Return the total number of queries run this for.
+    *
+    * @access   public
+    * @return   int Number of queries
+    * @author   Quinn Comendant <quinn@strangecode.com>
+    * @version  1.0
+    * @since    15 Jun 2006 11:46:05
+    */
+    function numQueries()
+    {
+        return $this->_query_count;
+    }
 
     /**
Index: trunk/lib/ScriptTimer.inc.php
===================================================================
--- trunk/lib/ScriptTimer.inc.php	(revision 171)
+++ trunk/lib/ScriptTimer.inc.php	(revision 172)
@@ -18,4 +18,8 @@
 	function stop($name='default')
 	{
+	    if (!isset($this->_timing_stop_times[$name]) || !isset($this->_timing_cumulative_times[$name])) {
+	        $this->_timing_stop_times[$name] = null;
+	        $this->_timing_cumulative_times[$name] = null;
+	    }
         $this->_timing_stop_times[$name] = explode(' ', microtime());
         $this->_timing_cumulative_times[$name] += $this->getTime($name);
@@ -25,5 +29,5 @@
 	{
         if (!isset($this->_timing_start_times[$name])) {
-            return 0;
+	        return 0;
         }
         if (!isset($this->_timing_stop_times[$name])) {
@@ -48,5 +52,5 @@
 	    }
 	    
-	    $this->_timing_cumulative_times["TOTAL"] = array_sum($this->_timing_cumulative_times);
+	    $this->_timing_cumulative_times['TOTAL'] = array_sum($this->_timing_cumulative_times);
 
         echo '<pre>';
