2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
12 * @package ActiveSupport
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
20 if(!defined('AK_ADODBCACHE_CLASS_INCLUDED')){ define('AK_ADODBCACHE_CLASS_INCLUDED',true); // Class overriding trick
22 // ---- Required Files ---- //
23 require_once(AK_LIB_DIR
.'/Ak.php');
27 * Dabase cache driver for the AkCache class
29 * @author Bermi Ferrer <bermi at akelos dot com>
30 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
31 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
38 // --- Private properties --- //
42 * Handles an instance of current database conection using
52 * Timestamp of the last valid cache
56 * @var integer $_refreshTime
58 var $_refreshTime = NULL;
61 * Cache lifetime (in seconds)
65 * @var integer $_lifeTime
67 var $_lifeTime = 3600;
70 * Enable / Disable "Memory Caching"
72 * NB : There is no lifetime for memory caching !
74 * @see setMemoryCaching
76 * @var boolean $_memoryCaching
78 var $_memoryCaching = false;
81 * Memory caching container array
84 * @var array $_memoryCachingArray
86 var $_memoryCachingArray = array();
89 * Enable / disable automatic serialization
91 * It can be used to save directly datas which aren't strings
94 * @see setAutomaticSerialization
96 * @var boolean $_automaticSerialization
98 var $_automaticSerialization = false;
104 // ------ CLASS METHODS ------ //
108 // ---- Setters ---- //
116 * Use this method to set $this->_db value
120 * @param object $db Handles an instance of current database conection
131 // {{{ setRefreshTime()
134 * $this->_refreshTime setter
136 * Use this method to set $this->_refreshTime value
139 * @see get$refreshTime
140 * @param integer $refresh_time Timestamp of the last valid cache
143 function setRefreshTime($refresh_time)
145 $this->_refreshTime
= $refresh_time;
153 * $this->_lifeTime setter
155 * Use this method to set $this->_lifeTime value
159 * @param integer $life_time Cache lifetime (in seconds)
162 function setLifeTime($life_time = 3600)
164 $this->_lifeTime
= $life_time;
165 $this->setRefreshTime(time() - $this->_lifeTime
);
169 // {{{ setMemoryCaching()
172 * $this->_memoryCaching setter
174 * Use this method to set $this->_memoryCaching value
177 * @see get$memoryCaching
178 * @param boolean $memory_caching Enable / Disable "Memory Caching"
180 * NB : There is no lifetime for memory caching !
183 function setMemoryCaching($memory_caching = false)
185 $this->_memoryCaching
= (bool)$memory_caching;
190 // {{{ setAutomaticSerialization()
193 * $this->_automaticSerialization setter
195 * Use this method to set $this->_automaticSerialization value
198 * @see get$automaticSerialization
199 * @param boolean $automatic_serialization Enable / disable automatic serialization
202 function setAutomaticSerialization($automatic_serialization = false)
204 $this->_automaticSerialization
= (bool)$automatic_serialization;
211 // ---- Public methods ---- //
217 * Class constructor (ALA Akelos Framework)
220 * @param array $options
223 * //This options are valid for both cache contains (database and file based)
224 * 'lifeTime' => cache lifetime in seconds (int),
225 * 'memoryCaching' => enable / disable memory caching (boolean),
226 * 'automaticSerialization' => enable / disable automatic serialization (boolean)
231 function init($options = array())
233 $this->_db
=& Ak
::db();
235 $available_options = array('memoryCaching', 'lifeTime', 'automaticSerialization');
236 foreach($options as $key => $value) {
237 if(in_array($key, $available_options)) {
238 $property = '_'.$key;
239 $this->$property = $value;
242 $this->_refreshTime
= time() - $this->_lifeTime
;
249 * Test if a cache is available and (if yes) return it
252 * @param string $id Cache id
253 * @param string $group Name of the cache group.
254 * @return mixed Data of the cache (or false if no cache available)
256 function get($id, $group = 'default')
259 $this->_group
= $group;
260 $cache_hash = md5($this->_id
).'_'.md5($this->_group
);
262 if(isset($this->_memoryCachingArray
[$cache_hash])){
263 return $this->_memoryCachingArray
[$cache_hash];
266 $query_result = $this->_db
->Execute('
269 WHERE id = '.$this->_db
->qstr($cache_hash).'
270 AND cache_group = '.$this->_db
->qstr($this->_group
).'
271 AND expire > '.$this->_db
->DBTimeStamp($this->_refreshTime
)
274 if(!$query_result && AK_DEBUG
){
275 trigger_error($this->_db
->ErrorMsg(), E_USER_NOTICE
);
278 $data = $this->_db
->BlobDecode($query_result->fields
[0]);
280 if($this->_automaticSerialization
== true){
281 $data = unserialize($data);
284 if($this->_memoryCaching
){
285 $this->_memoryCachingArray
[$cache_hash] = $data;
299 * Save some data in the cache
302 * @param string $data Data to put in cache
303 * @param string $id Cache id. By default it will use the Id specified
304 * when calling $this->get
305 * @param string $group Name of the cache group. By default it will use
306 * the group specified when calling $this->get
307 * @return boolean True if no problem
309 function save($data, $id = null, $group = null)
311 $this->_id
= isset($id) ?
$id : $this->_id
;
312 $this->_group
= isset($group) ?
$group : $this->_group
;
314 $cache_hash = md5($this->_id
).'_'.md5($this->_group
);
316 if($this->_automaticSerialization
== true){
317 $data = serialize($data);
320 $ret = $this->_db
->Replace(
322 'id'=>$this->_db
->qstr($cache_hash),
323 'cache_data'=>$this->_db
->qstr($this->_db
->BlobEncode($data)),
324 'cache_group'=>$this->_db
->qstr($this->_group
),
325 'expire'=>$this->_db
->DBTimeStamp(time() +
$this->_lifeTime
)),
331 if($this->_memoryCaching
){
332 $this->_memoryCachingArray
[$cache_hash] = $data;
342 * Remove a cache item from the database
345 * @param string $id Cache id
346 * @param string $group Name of the cache group
347 * @return boolean True if no problem
349 function remove($id, $group = 'default')
351 $cache_hash = md5($id).'_'.md5($group);
353 if (isset($this->_memoryCachingArray
[$cache_hash])) {
354 unset($this->_memoryCachingArray
[$cache_hash]);
356 if(!$this->_db
->Execute('DELETE FROM cache WHERE id = '.$this->_db
->qstr($cache_hash)) && AK_DEBUG
){
357 trigger_error($this->_db
->ErrorMsg(), E_USER_NOTICE
);
359 return (bool)$this->_db
->Affected_Rows();
368 * If no group is specified all cache items will be removed
369 * from the database else only cache items of the specified
370 * group will be destroyed
373 * @param string $group If no group is specified all cache items will be
374 * removed from the database else only cache items
375 * of the specified group will be destroyed
376 * @param string $mode Flush cache mode. Options are:
381 * @return boolean True if no problem
383 function clean($group = false, $mode = 'ingroup')
387 if(!$this->_db
->Execute('DELETE FROM cache WHERE cache_group = '.$this->_db
->qstr($group)) && AK_DEBUG
){
388 trigger_error($this->_db
->ErrorMsg(), E_USER_NOTICE
);
390 return (bool)$this->_db
->Affected_Rows();
393 if(!$this->_db
->Execute('DELETE FROM cache WHERE cache_group NOT LIKE '.$this->_db
->qstr($group)) && AK_DEBUG
){
394 trigger_error($this->_db
->ErrorMsg(), E_USER_NOTICE
);
396 return (bool)$this->_db
->Affected_Rows();
399 if(!$this->_db
->Execute('DELETE FROM cache WHERE expire < '.$this->_db
->DBTimeStamp(time())) && AK_DEBUG
){
400 trigger_error($this->_db
->ErrorMsg(), E_USER_NOTICE
);
402 return (bool)$this->_db
->Affected_Rows();
412 }// End of if(!defined('AK_ADODBCACHE_CLASS_INCLUDED')){