[pLog-svn] r1700 - plog/branches/plog-1.1-ben/class/config

ork at devel.plogworld.net ork at devel.plogworld.net
Mon Apr 4 17:17:52 GMT 2005


Author: ork
Date: 2005-04-04 17:17:52 +0000 (Mon, 04 Apr 2005)
New Revision: 1700

Modified:
   plog/branches/plog-1.1-ben/class/config/configdbstorage.class.php
Log:
enabled caching .. 


Modified: plog/branches/plog-1.1-ben/class/config/configdbstorage.class.php
===================================================================
--- plog/branches/plog-1.1-ben/class/config/configdbstorage.class.php	2005-04-04 17:16:25 UTC (rev 1699)
+++ plog/branches/plog-1.1-ben/class/config/configdbstorage.class.php	2005-04-04 17:17:52 UTC (rev 1700)
@@ -1,9 +1,10 @@
 <?php
 
-	include_once( PLOG_CLASS_PATH."class/object/object.class.php" );
-	include_once( PLOG_CLASS_PATH."class/config/configfilestorage.class.php" );
-	include_once( PLOG_CLASS_PATH."class/database/db.class.php" );
+    include_once( PLOG_CLASS_PATH."class/object/object.class.php" );
+    include_once( PLOG_CLASS_PATH."class/config/configfilestorage.class.php" );
+	include_once( PLOG_CLASS_PATH."class/cache/cachemanager.class.php" );
 
+
     /**
 	 * \ingroup Config
 	 *
@@ -34,27 +35,51 @@
         var $_db;
 
         // array used to store the options
-        var $_data;
+        var $_data = array();
 		
     	// information needed to connect to the db server
         var $_dbPrefix;		
+
+		// cache object
+		var $_cache;
         
         /**
          * Connects to the database using the parameters in the config file.
          *
          */
-    	function ConfigDbStorage( $params = null )
+    	function ConfigDbStorage()
         {            
-            // initialize the connection
-            $this->_db =& Db::getDb();
-            // get the prefix
-            $this->_dbPrefix = Db::getPrefix();
+		    $this->_cache =& CacheManager::getCache();
+        }
 
-            // and finally, load the whole data
+        function _loadAllValuesFromDatabase() {
+            // initialize the database
+            $this->_initializeDatabase();
+
+            // load the whole data
             $this->_loadData();
+
+            // and build the cache
+            $this->_cache->setData( CACHE_CONFIGDBSTORAGE, CACHE_GLOBAL, $this->_data );
         }
 
         /**
+         * Initialize the Database to allow db access
+         *
+         */
+        function _initializeDatabase() {
+            if ($this->_db == null) {
+                // source the neccessary class files
+                include_once( PLOG_CLASS_PATH."class/database/db.class.php" );
+
+                // initialize the connection
+                $this->_db =& Db::getDb();
+                // get the prefix
+                $this->_dbPrefix = Db::getPrefix();
+            }
+        }
+
+        /**
          * Internal function that loads all the data from the table and puts in into
          * our array. It should be apparently faster that making an SQL query every time
          * we need to get a value.
@@ -98,12 +123,39 @@
 
         function getValue( $key, $defaultValue = null )
         {
-            if(!array_key_exists($key, $this->_data) ||
-                $this->_data[$key] == "" || 
-                $this->_data[$key] == null) {
-                return $defaultValue;
+            if (!is_object( $this->_cache )) {
+                $this->_cache =& CacheManager::getCache();
+            }
+            if($this->_data == array()) {
+                $data = $this->_data = $this->_cache->getData( CACHE_CONFIGDBSTORAGE, CACHE_GLOBAL );
+                if ($data) {
+                    $this->_data = $data;
+                } else {
+                    $this->_loadAllValuesFromDatabase();
+                }
+            }
+
+            if(array_key_exists($key, $this->_data)) {
+                if ($this->_data[$key] == "" || $this->_data[$key] == null) {
+                    return $defaultValue;
+                } else {
+                    return $this->_data[$key];
+                }
+            }
+        }
+
+        function getUncachedValue( $key, $defaultValue = null )
+        {
+            if($this->_data == array()) {
+                $this->_loadAllValuesFromDatabase();
             } else {
-                return $this->_data[$key];
+                if(array_key_exists($key, $this->_data)) {
+                    if ($this->_data[$key] == "" || $this->_data[$key] == null) {
+                        return $defaultValue;
+                    } else {
+                        return $this->_data[$key];
+                    }
+                }
             }
         }
 
@@ -148,8 +200,11 @@
          */
         function _keyExists( $key )
         {
-        	$query = "SELECT * FROM ".$this->_dbPrefix."config WHERE config_key = '$key'";
+            // initialize the database
+            $this->_initializeDatabase();
 
+            $query = "SELECT * FROM ".$this->_dbPrefix."config WHERE config_key = '$key'";
+
             //$this->_db->debug=true;
             $result = $this->_db->Execute( $query );
 
@@ -167,6 +222,9 @@
 		 */
         function _updateValue( $key, $value )
         {
+            // initialize the database
+            $this->_initializeDatabase();
+
         	// if the key exists, we have to update it
             $type = $this->_getType( $value );
             switch( $type ) {
@@ -204,6 +262,9 @@
 		 */
         function _insertValue( $key, $value )
         {
+            // initialize the database
+            $this->_initializeDatabase();
+
         	$type = $this->_getType( $value );
             switch( $type ) {
             	case TYPE_INTEGER:
@@ -252,6 +313,9 @@
             	$this->saveValue( $key, $value );
             }
 
+            // update the cache
+            $this->_cache->setData( CACHE_CONFIGDBSTORAGE, CACHE_GLOBAL, $this->_data );
+
             // saveValue is already reloading the data for us everytime!
             return true;
         }
@@ -277,8 +341,12 @@
                  $result = $this->_insertValue( $key, $value );
             }
 
+            // update the cache
+            $this->_data[$key] = $value;
+            $this->_cache->removeData( CACHE_CONFIGDBSTORAGE, CACHE_GLOBAL );
+
             // we better reload the data just in case
-            $this->reload();
+            // $this->reload();
 
             return $result;
         }




More information about the pLog-svn mailing list