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 require_once(AK_LIB_DIR
.'/Ak.php');
24 * Dabase cache driver for the AkCache class
26 * @author Bermi Ferrer <bermi at akelos dot com>
27 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
28 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
34 * Handles an instance of current database conection using
44 * Timestamp of the last valid cache
48 * @var integer $_refreshTime
50 var $_refreshTime = NULL;
53 * Cache lifetime (in seconds)
57 * @var integer $_lifeTime
59 var $_lifeTime = 3600;
62 * Enable / Disable "Memory Caching"
64 * NB : There is no lifetime for memory caching !
66 * @see setMemoryCaching
68 * @var boolean $_memoryCaching
70 var $_memoryCaching = false;
73 * Memory caching container array
76 * @var array $_memoryCachingArray
78 var $_memoryCachingArray = array();
81 * Enable / disable automatic serialization
83 * It can be used to save directly datas which aren't strings
86 * @see setAutomaticSerialization
88 * @var boolean $_automaticSerialization
90 var $_automaticSerialization = false;
95 * Use this method to set $this->_db value
99 * @param object $db Handles an instance of current database conection
110 * $this->_refreshTime setter
112 * Use this method to set $this->_refreshTime value
115 * @see get$refreshTime
116 * @param integer $refresh_time Timestamp of the last valid cache
119 function setRefreshTime($refresh_time)
121 $this->_refreshTime
= $refresh_time;
126 * $this->_lifeTime setter
128 * Use this method to set $this->_lifeTime value
132 * @param integer $life_time Cache lifetime (in seconds)
135 function setLifeTime($life_time = 3600)
137 $this->_lifeTime
= $life_time;
138 $this->setRefreshTime(time() - $this->_lifeTime
);
142 * $this->_memoryCaching setter
144 * Use this method to set $this->_memoryCaching value
147 * @see get$memoryCaching
148 * @param boolean $memory_caching Enable / Disable "Memory Caching"
150 * NB : There is no lifetime for memory caching !
153 function setMemoryCaching($memory_caching = false)
155 $this->_memoryCaching
= (bool)$memory_caching;
160 * $this->_automaticSerialization setter
162 * Use this method to set $this->_automaticSerialization value
165 * @see get$automaticSerialization
166 * @param boolean $automatic_serialization Enable / disable automatic serialization
169 function setAutomaticSerialization($automatic_serialization = false)
171 $this->_automaticSerialization
= (bool)$automatic_serialization;
176 * Class constructor (ALA Akelos Framework)
179 * @param array $options
182 * //This options are valid for both cache contains (database and file based)
183 * 'lifeTime' => cache lifetime in seconds (int),
184 * 'memoryCaching' => enable / disable memory caching (boolean),
185 * 'automaticSerialization' => enable / disable automatic serialization (boolean)
190 function init($options = array())
192 $this->_db
=& Ak
::db();
194 $available_options = array('memoryCaching', 'lifeTime', 'automaticSerialization');
195 foreach($options as $key => $value) {
196 if(in_array($key, $available_options)) {
197 $property = '_'.$key;
198 $this->$property = $value;
201 $this->_refreshTime
= time() - $this->_lifeTime
;
205 * Test if a cache is available and (if yes) return it
208 * @param string $id Cache id
209 * @param string $group Name of the cache group.
210 * @return mixed Data of the cache (or false if no cache available)
212 function get($id, $group = 'default')
215 $this->_group
= $group;
216 $cache_hash = md5($this->_id
).'_'.md5($this->_group
);
218 if(isset($this->_memoryCachingArray
[$cache_hash])){
219 return $this->_memoryCachingArray
[$cache_hash];
222 $query_result = $this->_db
->selectValue('
225 WHERE id = '.$this->_db
->quote_string($cache_hash).'
226 AND cache_group = '.$this->_db
->quote_string($this->_group
).'
227 AND expire > '.$this->_db
->quote_datetime($this->_refreshTime
)
229 if (!$query_result) return false;
231 $data = $this->_db
->unescape_blob($query_result);
233 if($this->_automaticSerialization
== true){
234 $data = unserialize($data);
237 if($this->_memoryCaching
){
238 $this->_memoryCachingArray
[$cache_hash] = $data;
245 * Save some data in the cache
248 * @param string $data Data to put in cache
249 * @param string $id Cache id. By default it will use the Id specified
250 * when calling $this->get
251 * @param string $group Name of the cache group. By default it will use
252 * the group specified when calling $this->get
253 * @return boolean True if no problem
255 function save($data, $id = null, $group = null)
257 $this->_id
= isset($id) ?
$id : $this->_id
;
258 $this->_group
= isset($group) ?
$group : $this->_group
;
260 $cache_hash = md5($this->_id
).'_'.md5($this->_group
);
262 if($this->_automaticSerialization
== true){
263 $data = serialize($data);
266 // TODO replace with AkDbAdapter statement
267 $ret = $this->_db
->connection
->Replace(
269 'id'=>$this->_db
->quote_string($cache_hash),
270 'cache_data'=>$this->_db
->quote_string($this->_db
->escape_blob($data)),
271 'cache_group'=>$this->_db
->quote_string($this->_group
),
272 'expire'=>$this->_db
->quote_datetime(time() +
$this->_lifeTime
)),
278 if($this->_memoryCaching
){
279 $this->_memoryCachingArray
[$cache_hash] = $data;
286 * Remove a cache item from the database
289 * @param string $id Cache id
290 * @param string $group Name of the cache group
291 * @return boolean True if no problem
293 function remove($id, $group = 'default')
295 $cache_hash = md5($id).'_'.md5($group);
297 if (isset($this->_memoryCachingArray
[$cache_hash])) {
298 unset($this->_memoryCachingArray
[$cache_hash]);
300 return (bool)$this->_db
->delete('DELETE FROM cache WHERE id = '.$this->_db
->quote_string($cache_hash));
306 * If no group is specified all cache items will be removed
307 * from the database else only cache items of the specified
308 * group will be destroyed
311 * @param string $group If no group is specified all cache items will be
312 * removed from the database else only cache items
313 * of the specified group will be destroyed
314 * @param string $mode Flush cache mode. Options are:
319 * @return boolean True if no problem
321 function clean($group = false, $mode = 'ingroup')
325 return (bool)$this->_db
->delete('DELETE FROM cache WHERE cache_group = '.$this->_db
->quote_string($group));
327 return (bool)$this->_db
->delete('DELETE FROM cache WHERE cache_group NOT LIKE '.$this->_db
->quote_string($group));
329 return (bool)$this->_db
->delete('DELETE FROM cache WHERE expire < '.$this->_db
->quote_datetime(time()));