From a1aaefbd7a4cb2f6e8683215baf076bb2eff9c18 Mon Sep 17 00:00:00 2001 From: wei <> Date: Fri, 9 Nov 2007 01:09:06 +0000 Subject: TCache implements ArrayAccess. duplicate copyFrom in TActiveRecord --- framework/Caching/TCache.php | 51 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'framework/Caching') diff --git a/framework/Caching/TCache.php b/framework/Caching/TCache.php index f44482e0..e60cecdd 100644 --- a/framework/Caching/TCache.php +++ b/framework/Caching/TCache.php @@ -38,12 +38,15 @@ Prado::using('System.Collections.TList'); * - {@link deleteValue} * and optionally {@link flush} * + * Since version 3.1.2, TCache implements the ArrayAccess interface such that + * the cache acts as an array. + * * @author Qiang Xue * @version $Id$ * @package System.Caching * @since 3.0 */ -abstract class TCache extends TModule implements ICache +abstract class TCache extends TModule implements ICache, ArrayAccess { private $_prefix=null; private $_primary=true; @@ -231,6 +234,52 @@ abstract class TCache extends TModule implements ICache * @return boolean if no error happens during deletion */ abstract protected function deleteValue($key); + + /** + * Returns whether there is a cache entry with a specified key. + * This method is required by the interface ArrayAccess. + * @param string a key identifying the cached value + * @return boolean + */ + public function offsetExists($id) + { + return $this->get($id) !== false; + } + + /* + * Retrieves the value from cache with a specified key. + * This method is required by the interface ArrayAccess. + * @param string a key identifying the cached value + * @return mixed the value stored in cache, false if the value is not in the cache or expired. + */ + public function offsetGet($id) + { + return $this->get($id); + } + + /* + * Stores the value identified by a key into cache. + * If the cache already contains such a key, the existing value will be + * replaced with the new ones. To add expiration and dependencies, use the set() method. + * This method is required by the interface ArrayAccess. + * @param string the key identifying the value to be cached + * @param mixed the value to be cached + */ + public function offsetSet($id, $value) + { + $this->set($id, $value); + } + + /* + * Deletes the value with the specified key from cache + * This method is required by the interface ArrayAccess. + * @param string the key of the value to be deleted + * @return boolean if no error happens during deletion + */ + public function offsetUnset($id) + { + $this->delete($id); + } } -- cgit v1.2.3