From 94e94e0a8566f23d16658a04c55b0bbfdd6689aa Mon Sep 17 00:00:00 2001 From: "godzilla80@gmx.net" <> Date: Sun, 14 Feb 2010 01:22:57 +0000 Subject: Merge Branches & Trunk /trunk:r2680,2692,2707-2736 /branches/3.1:r2682-2686,2694-2702,2705,2738-2762 --- framework/Data/SqlMap/DataMapper/TSqlMapCache.php | 80 +++++++++++++++++++++-- 1 file changed, 74 insertions(+), 6 deletions(-) (limited to 'framework/Data/SqlMap/DataMapper') diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapCache.php b/framework/Data/SqlMap/DataMapper/TSqlMapCache.php index 05b72e08..5262bdf8 100644 --- a/framework/Data/SqlMap/DataMapper/TSqlMapCache.php +++ b/framework/Data/SqlMap/DataMapper/TSqlMapCache.php @@ -4,7 +4,7 @@ * * @author Wei Zhuo * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005-2008 PradoSoft + * @copyright Copyright © 2005-2008 PradoSoft * @license http://www.pradosoft.com/license/ * @version $Id$ * @package System.Data.SqlMap @@ -25,16 +25,17 @@ abstract class TSqlMapCache implements ICache protected $_keyList; protected $_cache; protected $_cacheSize = 100; + protected $_cacheModel = null; /** * Create a new cache with limited cache size. - * @param integer maxium number of items to cache. + * @param TSqlMapCacheModel $cacheModel. */ - public function __construct($cacheSize=100) + public function __construct($cacheModel=null) { $this->_cache = new TMap; - $this->_cacheSize = intval($cacheSize); $this->_keyList = new TList; + $this->_cacheModel=$cacheModel; } /** @@ -173,20 +174,71 @@ class TSqlMapLruCache extends TSqlMapCache */ class TSqlMapApplicationCache implements ICache { + protected $_cacheModel=null; + + /** + * Create a new cache with limited cache size. + * @param TSqlMapCacheModel $cacheModel. + */ + public function __construct($cacheModel=null) + { + $this->_cacheModel=$cacheModel; + } + + /** + * + * @return string a KeyListID for the cache model. + */ + protected function getKeyListId() + { + $id='keyList'; + if ($this->_cacheModel instanceof TSqlMapCacheModel) + $id.='_'.$this->_cacheModel->getId(); + return $id; + } + /** + * Retreive keylist from cache or create it if it doesn't exists + * @return TList + */ + protected function getKeyList() + { + if (($keyList=$this->getCache()->get($this->getKeyListId()))===false) + { + $keyList=new TList(); + $this->getCache()->set($this->getKeyListId(), $keyList); + } + return $keyList; + } + + protected function setKeyList($keyList) + { + $this->getCache()->set($this->getKeyListId(), $keyList); + } + /** * @param string item to be deleted. */ public function delete($key) { + $keyList=$this->getKeyList(); + $keyList->remove($key); $this->getCache()->delete($key); + $this->setKeyList($keyList); } /** - * Deletes all items in the cache. + * Deletes all items in the cache, only for data cached by sqlmap cachemodel */ public function flush() { - $this->getCache()->flush(); + $keyList=$this->getKeyList(); + $cache=$this->getCache(); + foreach ($keyList as $key) + { + $cache->delete($key); + } + // Remove the old keylist + $cache->delete($this->getKeyListId()); } /** @@ -195,6 +247,16 @@ class TSqlMapApplicationCache implements ICache public function get($key) { $result = $this->getCache()->get($key); + if ($result === false) + { + // if the key has not been found in cache (e.g expired), remove from keylist + $keyList=$this->getKeyList(); + if ($keyList->contains($key)) + { + $keyList->remove($key); + $this->setKeyList($keyList); + } + } return $result === false ? null : $result; } @@ -206,6 +268,12 @@ class TSqlMapApplicationCache implements ICache public function set($key, $value,$expire=0,$dependency=null) { $this->getCache()->set($key, $value, $expire,$dependency); + $keyList=$this->getKeyList(); + if (!$keyList->contains($key)) + { + $keyList->add($key); + $this->setKeyList($keyList); + } } /** -- cgit v1.2.3