From 66843b23960e17991db0b4f7b01487063b2234bc Mon Sep 17 00:00:00 2001
From: xue <>
Date: Tue, 4 Apr 2006 04:08:48 +0000
Subject: Refactored cache classes with support for cache dependency
---
framework/Caching/TAPCCache.php | 152 ++++++++++++++++------------------------
1 file changed, 61 insertions(+), 91 deletions(-)
(limited to 'framework/Caching/TAPCCache.php')
diff --git a/framework/Caching/TAPCCache.php b/framework/Caching/TAPCCache.php
index 0ed1d433..4c5f2779 100644
--- a/framework/Caching/TAPCCache.php
+++ b/framework/Caching/TAPCCache.php
@@ -35,118 +35,88 @@
* cache module. It can be accessed via {@link TApplication::getCache()}.
*
* TAPCCache may be configured in application configuration file as follows
+ *
*
+ *
*
* @author Alban Hanry
* @version $Revision: $ $Date: $
* @package System.Caching
* @since 3.0b
*/
-class TAPCCache extends TModule implements ICache
+class TAPCCache extends TCache
{
- /**
- * @var boolean if the module is initialized
- */
- private $_initialized=false;
-
- /**
- * @var string a unique prefix used to identify this cache instance from the others
- */
- protected $_prefix=null;
-
/**
* Initializes this module.
* This method is required by the IModule interface.
* @param TXmlElement configuration for this module, can be null
* @throws TConfigurationException if apc extension is not installed or not started, check your php.ini
*/
- public function init($config)
- {
+ public function init($config)
+ {
if(!extension_loaded('apc'))
throw new TConfigurationException('apccache_extension_required');
- $application=$this->getApplication();
- $this->_prefix=$application->getUniqueID();
- $application->setCache($this);
- $this->_initialized=true;
- }
-
- /**
- * Retrieves a value from cache with a specified key.
- * @return mixed the value stored in cache, false if the value is not in the cache or expired.
- */
- public function get($key)
- {
- if(($value=apc_fetch($this->_prefix.$key))!==false)
- return Prado::unserialize($value);
- else
- return $value;
- }
+ parent::init($config);
+ }
- /**
- * Stores a value identified by a key into cache.
- * If the cache already contains such a key, the existing value and
- * expiration time will be replaced with the new ones.
- *
- * @param string the key identifying the value to be cached
- * @param mixed the value to be cached
- * @param integer the expiration time of the value,
- * 0 means never expire,
- * @return boolean true if the value is successfully stored into cache, false otherwise
- */
- public function set($key,$value,$expiry=0)
- {
- return apc_store($this->_prefix.$key,Prado::serialize($value),$expiry);
- }
+ /**
+ * Retrieves a value from cache with a specified key.
+ * This is the implementation of the method declared in the parent class.
+ * @param string a unique key identifying the cached value
+ * @return string the value stored in cache, false if the value is not in the cache or expired.
+ */
+ protected function getValue($key)
+ {
+ return apc_fetch($key);
+ }
- /**
- * Stores a value identified by a key into cache if the cache does not contain this key.
- * APC does not support this mode. So calling this method will raise an exception.
- * @param string the key identifying the value to be cached
- * @param mixed the value to be cached
- * @param integer the expiration time of the value,
- * 0 means never expire,
- * @return boolean true if the value is successfully stored into cache, false otherwise
- * @throw new TNotSupportedException if this method is invoked
- */
- public function add($key,$value,$expiry=0)
- {
- throw new TNotSupportedException('apccache_add_unsupported');
- }
+ /**
+ * Stores a value identified by a key in cache.
+ * This is the implementation of the method declared in the parent class.
+ *
+ * @param string the key identifying the value to be cached
+ * @param string the value to be cached
+ * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
+ * @return boolean true if the value is successfully stored into cache, false otherwise
+ */
+ protected function setValue($key,$value,$expire)
+ {
+ return apc_store($key,$value,$expire);
+ }
- /**
- * Stores a value identified by a key into cache only if the cache contains this key.
- * The existing value and expiration time will be overwritten with the new ones.
- * APC does not support this mode. So calling this method will raise an exception.
- * @param string the key identifying the value to be cached
- * @param mixed the value to be cached
- * @param integer the expiration time of the value,
- * 0 means never expire,
- * @return boolean true if the value is successfully stored into cache, false otherwise
- * @throw new TNotSupportedException if this method is invoked
- */
- public function replace($key,$value,$expiry=0)
- {
- throw new TNotSupportedException('apccache_replace_unsupported');
- }
+ /**
+ * Stores a value identified by a key into cache if the cache does not contain this key.
+ * This is the implementation of the method declared in the parent class.
+ *
+ * @param string the key identifying the value to be cached
+ * @param string the value to be cached
+ * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
+ * @return boolean true if the value is successfully stored into cache, false otherwise
+ */
+ protected function addValue($key,$value,$expire)
+ {
+ throw new TNotSupportedException('apccache_add_unsupported');
+ }
- /**
- * Deletes a value with the specified key from cache
- * @param string the key of the value to be deleted
- * @return boolean if no error happens during deletion
- */
- public function delete($key)
- {
- return apc_delete($this->_prefix.$key);
- }
-
- /**
- * Deletes all values from cache.
- */
- public function flush()
- {
- return apc_clear_cache('user');
- }
+ /**
+ * Deletes a value with the specified key from cache
+ * This is the implementation of the method declared in the parent class.
+ * @param string the key of the value to be deleted
+ * @return boolean if no error happens during deletion
+ */
+ protected function deleteValue($key)
+ {
+ return apc_delete($key);
+ }
+ /**
+ * Deletes all values from cache.
+ * Be careful of performing this operation if the cache is shared by multiple applications.
+ */
+ public function flush()
+ {
+ return apc_clear_cache('user');
+ }
}
?>
\ No newline at end of file
--
cgit v1.2.3