[pLog-svn] r6000 - plog/branches/lifetype-1.2/class/cache

oscar at devel.lifetype.net oscar at devel.lifetype.net
Mon Oct 22 17:41:01 EDT 2007


Author: oscar
Date: 2007-10-22 17:41:01 -0400 (Mon, 22 Oct 2007)
New Revision: 6000

Added:
   plog/branches/lifetype-1.2/class/cache/basecacheprovider.class.php
   plog/branches/lifetype-1.2/class/cache/nullcache.class.php
Modified:
   plog/branches/lifetype-1.2/class/cache/cache.class.php
   plog/branches/lifetype-1.2/class/cache/cachemanager.class.php
   plog/branches/lifetype-1.2/class/cache/memcache.class.php
Log:
Setting DATA_CACHE_ENABLED to 'false' at the top of model.class.php didn't seem to really disable the cache.

The problem is that the ConfigDbStorage class is the first one to call CacheManager::getCache() and therefore the first one that initializes the static reference to whichever cache class we're using (Cache or MemCache) The problem here is that ConfigDbStorage calls getCache() without parameters, so the static reference to the caching class is initialized with caching enabled and subsequent calls to getCache() will not create new instances of the cache class but return the one that already exists, ignoring any parameters related to enabling or disabling caching. When the Model class calls getCache() using the value of the constants DATA_CACHE_ENABLED, this value is ignored because of the reason just described.

Deactivating the cache by modifying some core code wasn't a very elegant solution either, so I've created a new "pass-through" cache class called NullCache that does basically nothing and therefore is a cache classs that doesn't cache :) In addition to that, there's a new base class called BaseCacheProvider that defines the interface that all cache providers must implement, and I modified the existing cache classes to extend from this one.


Added: plog/branches/lifetype-1.2/class/cache/basecacheprovider.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/cache/basecacheprovider.class.php	                        (rev 0)
+++ plog/branches/lifetype-1.2/class/cache/basecacheprovider.class.php	2007-10-22 21:41:01 UTC (rev 6000)
@@ -0,0 +1,130 @@
+<?php
+        
+	/**
+	 * \ingroup Cache
+	 *
+	 * This is the base class that all cache providers should extends. It defines all the base methods
+	 * that are expected from a base provider.
+	 *
+	 * :TODO:
+	 * This needs to be turned into an interface or base abstract class when moving to PHP5
+	 */
+	class BaseCacheProvider
+	{
+	   
+		function BaseCacheProvider()
+		{			
+		}
+		
+		/** 
+		 * Sets the lifetime of cached data
+		 *
+		 * @param lifeTime
+		 */
+		function setLifeTime( $lifeTime )		
+		{
+			throw( new Exception( "This method must be implemented by child classes!" ));
+		}		
+
+		/**
+		 * Saves data to the cache. Data is identified by an id and a group. Any data
+		 * can be saved to the cache but please check that you are using unique keys for
+		 * different data or else data will be overwritten. If you have data that you know
+		 * beforehand is not going to be unique, please use the setMultipleData method.
+		 *
+		 * @param id Unique identifier for the data.
+		 * @param group The cache group
+		 * @param data Data that is going to be cached
+		 * @return Returns true if successful or false otherwise
+		 */
+        function setData( $id, $group, $data )
+        {
+			throw( new Exception( "This method must be implemented by child classes!" ));	
+        }
+		
+		/** 
+		 * Works in the same way as Cache::setData does, but instead of setting single values,
+		 * it assumes that the value we're setting for the given key is part of an array of values. This
+		 * method is useful for data which we know is not unique, since it will store the data
+		 * identified by data in an array identified by $id, alongside with any other values
+		 * that are sharing the same key.		
+		 *
+		 * @param id Unique identifier for the data.
+		 * @param group The cache group
+		 * @param data Data that is going to be cached
+		 * @return Returns true if successful or false otherwise
+		 */
+		function setMultipleData( $id, $group, $data )
+		{
+			throw( new Exception( "This method must be implemented by child classes!" ));			
+		}
+
+		/**
+		 * Retrieves previously stored data given its key.
+		 *
+		 * @param id Unique identifier under which the data was cached
+		 * @param group Cache group where data was stored
+		 * @return Returns the cached data if found or false otherwise
+		 */
+        function getData( $id, $group )
+        {
+			throw( new Exception( "This method must be implemented by child classes!" ));	
+        }
+
+		/**
+		 * Removes cached data from the cache, given its key and cache group.
+		 *
+		 * @param id Unique identifier under which the data was cached
+		 * @param group Cache group where data was stored
+		 * @return Returns the cached data if found or false otherwise
+		 */
+        function removeData( $id, $group )
+        {
+			throw( new Exception( "This method must be implemented by child classes!" ));	
+        }
+
+		/**
+		 * Clears the data of a whole cache group.
+		 *
+		 * @param group The group identifer whose data we'd like to cleanup
+		 * @return
+		 */
+        function clearCacheByGroup( $group )
+        {
+			throw( new Exception( "This method must be implemented by child classes!" ));	
+        }
+
+		/**
+		 * Clears the entire cache, use with care.
+		 *
+		 * @return Returns true if successful or false otherwise
+		 */
+        function clearCache()
+        {
+			throw( new Exception( "This method must be implemented by child classes!" ));	
+        }
+
+		/**
+		 * Sets the directory where Cache_Lite will store its data. By default this is set to
+		 * ./tmp.
+		 *
+		 * @param temp_folder Folder where cache data will be stored
+		 * @return Always true
+		 */
+        function setCacheDir( $temp_folder )
+        {
+			throw( new Exception( "This method must be implemented by child classes!" ));	
+        }
+		
+		/**
+		 * Returns the total count of cache hits, miss and total queries over the lifetime of the
+		 * script so far.
+		 *
+		 * @return An array with 3 keys: "hits", "total" and "miss"
+		 */
+		function getCacheStats()
+		{
+			throw( new Exception( "This method must be implemented by child classes!" ));			
+		}
+	}
+?>
\ No newline at end of file

Modified: plog/branches/lifetype-1.2/class/cache/cache.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/cache/cache.class.php	2007-10-22 21:25:53 UTC (rev 5999)
+++ plog/branches/lifetype-1.2/class/cache/cache.class.php	2007-10-22 21:41:01 UTC (rev 6000)
@@ -1,5 +1,7 @@
-<?php
+<?php                                                           
 
+	lt_include( PLOG_CLASS_PATH."class/cache/basecacheprovider.class.php" );
+
 	/**
      * \defgroup Cache
 	 *
@@ -24,8 +26,9 @@
      *
      * @see CacheManager
      * @see Cache_Lite
+	 * @see BaseCacheProvider
      */
-    class Cache
+    class Cache extends BaseCacheProvider
     {
         var $cache;
         var $lifeTime;
@@ -37,6 +40,8 @@
 		 */
         function Cache( $cacheProperties )
         {
+	    	$this->BaseCacheProvider();
+		
             lt_include( PLOG_CLASS_PATH . "class/cache/Cache_Lite/Lite.php" );
             
             $this->cache = new Cache_Lite( $cacheProperties );

Modified: plog/branches/lifetype-1.2/class/cache/cachemanager.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/cache/cachemanager.class.php	2007-10-22 21:25:53 UTC (rev 5999)
+++ plog/branches/lifetype-1.2/class/cache/cachemanager.class.php	2007-10-22 21:41:01 UTC (rev 6000)
@@ -27,8 +27,9 @@
                 lt_include( PLOG_CLASS_PATH . "class/config/configfilestorage.class.php" );
 
 				$config = new ConfigFileStorage( Array( "file" => PLOG_CLASS_PATH."config/cache.properties.php" ));
-				
-				if( $config->getValue( 'cache_method' ) == 'memcached' ) {
+				      
+				$cacheProvider = $config->getValue( 'cache_method' );
+				if( $cacheProvider == 'memcached' ) {
 					lt_include( PLOG_CLASS_PATH . "class/cache/memcache.class.php" );
 					// define defaults
 					$cacheParameter = array(
@@ -42,6 +43,14 @@
 					// build a new cache object
 					$cache = new MemCache( $cacheParameter);
 				}
+				elseif( $cacheProvider == 'null' ) {
+					// 'null' cache means that it does no caching, it basically is some sort of a
+					// pass-through cache
+					lt_include( PLOG_CLASS_PATH . "class/cache/nullcache.class.php" );					
+					
+	                // build a new cache object
+	                $cache = new NullCache();					
+				}				
 				else {					
                 	lt_include( PLOG_CLASS_PATH . "class/cache/cache.class.php" );					
 	                // configure the Cache_Lite parameters, but providing some defaults in case the config file isn't there

Modified: plog/branches/lifetype-1.2/class/cache/memcache.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/cache/memcache.class.php	2007-10-22 21:25:53 UTC (rev 5999)
+++ plog/branches/lifetype-1.2/class/cache/memcache.class.php	2007-10-22 21:41:01 UTC (rev 6000)
@@ -1,5 +1,7 @@
 <?php
 
+	lt_include( PLOG_CLASS_PATH."class/cache/basecacheprovider.class.php" );
+
 	$__memcache_hits = 0;
 	$__memcache_misses = 0;
 	$__memcache_queries = 0;
@@ -9,7 +11,7 @@
 	 *
 	 * Support for caching via memcached
 	 */
-    class MemCache
+    class MemCache extends BaseCacheProvider
     {
         var $cache;
         var $lifeTime;
@@ -18,6 +20,8 @@
 
         function MemCache( $cacheProperties )
         {
+			$this->BaseCacheProvider();
+	
             lt_include( PLOG_CLASS_PATH . "class/cache/Memcached_Client/memcached-client.php" );
             
             $this->cache = new memcached( $cacheProperties );

Added: plog/branches/lifetype-1.2/class/cache/nullcache.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/cache/nullcache.class.php	                        (rev 0)
+++ plog/branches/lifetype-1.2/class/cache/nullcache.class.php	2007-10-22 21:41:01 UTC (rev 6000)
@@ -0,0 +1,133 @@
+<?php
+  
+	lt_include( PLOG_CLASS_PATH."class/cache/basecacheprovider.class.php" );
+	 
+	/**
+	 * \ingroup Cache
+	 *
+	 * This is a cache class that does nothing, and that should be used whenever we want to
+	 * totally deactivate the data cache. Just set $config["cache_method"] to 'null' in file
+	 * config/cache.properties.php and the CacheManager factory will take care of loading
+	 * this file.
+	 *
+	 * This cache provider implements the BaseCacheProvider interface but it does no caching
+	 * and methods such as NullCache::getData() always return false.   
+	 */
+	class NullCache extends BaseCacheProvider	
+	{
+		function BaseCacheProvider()
+		{			
+		}
+		
+		/** 
+		 * Sets the lifetime of cached data
+		 *
+		 * @param lifeTime
+		 */
+		function setLifeTime( $lifeTime )		
+		{
+			return( true );
+		}		
+
+		/**
+		 * Saves data to the cache. Data is identified by an id and a group. Any data
+		 * can be saved to the cache but please check that you are using unique keys for
+		 * different data or else data will be overwritten. If you have data that you know
+		 * beforehand is not going to be unique, please use the setMultipleData method.
+		 *
+		 * @param id Unique identifier for the data.
+		 * @param group The cache group
+		 * @param data Data that is going to be cached
+		 * @return Returns true if successful or false otherwise
+		 */
+        function setData( $id, $group, $data )
+        {
+			return( true );
+        }
+		
+		/** 
+		 * Works in the same way as Cache::setData does, but instead of setting single values,
+		 * it assumes that the value we're setting for the given key is part of an array of values. This
+		 * method is useful for data which we know is not unique, since it will store the data
+		 * identified by data in an array identified by $id, alongside with any other values
+		 * that are sharing the same key.		
+		 *
+		 * @param id Unique identifier for the data.
+		 * @param group The cache group
+		 * @param data Data that is going to be cached
+		 * @return Returns true if successful or false otherwise
+		 */
+		function setMultipleData( $id, $group, $data )
+		{
+			return( true );
+		}
+
+		/**
+		 * Retrieves previously stored data given its key.
+		 *
+		 * @param id Unique identifier under which the data was cached
+		 * @param group Cache group where data was stored
+		 * @return Returns the cached data if found or false otherwise
+		 */
+        function getData( $id, $group )
+        {
+			return( false );
+        }
+
+		/**
+		 * Removes cached data from the cache, given its key and cache group.
+		 *
+		 * @param id Unique identifier under which the data was cached
+		 * @param group Cache group where data was stored
+		 * @return Returns the cached data if found or false otherwise
+		 */
+        function removeData( $id, $group )
+        {
+			return( false );
+        }
+
+		/**
+		 * Clears the data of a whole cache group.
+		 *
+		 * @param group The group identifer whose data we'd like to cleanup
+		 * @return
+		 */
+        function clearCacheByGroup( $group )
+        {
+			return( true );
+        }
+
+		/**
+		 * Clears the entire cache, use with care.
+		 *
+		 * @return Returns true if successful or false otherwise
+		 */
+        function clearCache()
+        {
+			return( false );
+        }
+
+		/**
+		 * Sets the directory where Cache_Lite will store its data. By default this is set to
+		 * ./tmp.
+		 *
+		 * @param temp_folder Folder where cache data will be stored
+		 * @return Always true
+		 */
+        function setCacheDir( $temp_folder )
+        {
+			return( true );
+        }
+		
+		/**
+		 * Returns the total count of cache hits, miss and total queries over the lifetime of the
+		 * script so far.
+		 *
+		 * @return An array with 3 keys: "hits", "total" and "miss"
+		 */
+		function getCacheStats()
+		{
+			return( Array( "hits" => 0, "total" => 0, "miss" => 0 ));
+		}		
+	}
+?>
\ No newline at end of file



More information about the pLog-svn mailing list