Fixing file upload params ($_FILES) normalization. Closes #75
[akelos.git] / lib / AkCache / AkAdodbCache.php
blobc83bce2129641555fbe23d00f4ecd7974fab63dd
1 <?php
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 // +----------------------------------------------------------------------+
11 /**
12 * @package ActiveSupport
13 * @subpackage Cache
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');
26 /**
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>
33 class AkAdodbCache
35 // {{{ properties
38 // --- Private properties --- //
41 /**
42 * Handles an instance of current database conection using
43 * AdoBD
45 * @see setDb
46 * @access private
47 * @var object $_db
49 var $_db = NULL;
51 /**
52 * Timestamp of the last valid cache
54 * @see setRefreshTime
55 * @access private
56 * @var integer $_refreshTime
58 var $_refreshTime = NULL;
60 /**
61 * Cache lifetime (in seconds)
63 * @see setLifeTime
64 * @access private
65 * @var integer $_lifeTime
67 var $_lifeTime = 3600;
69 /**
70 * Enable / Disable "Memory Caching"
72 * NB : There is no lifetime for memory caching !
74 * @see setMemoryCaching
75 * @access private
76 * @var boolean $_memoryCaching
78 var $_memoryCaching = false;
80 /**
81 * Memory caching container array
83 * @access private
84 * @var array $_memoryCachingArray
86 var $_memoryCachingArray = array();
88 /**
89 * Enable / disable automatic serialization
91 * It can be used to save directly datas which aren't strings
92 * (but it's slower)
94 * @see setAutomaticSerialization
95 * @access private
96 * @var boolean $_automaticSerialization
98 var $_automaticSerialization = false;
100 // }}}
104 // ------ CLASS METHODS ------ //
108 // ---- Setters ---- //
111 // {{{ setDb()
114 * $this->_db setter
116 * Use this method to set $this->_db value
118 * @access public
119 * @see get$db
120 * @param object $db Handles an instance of current database conection
121 * using AdoBD
122 * @return void
124 function setDb($db)
126 $this->_db = $db;
130 // }}}
131 // {{{ setRefreshTime()
134 * $this->_refreshTime setter
136 * Use this method to set $this->_refreshTime value
138 * @access public
139 * @see get$refreshTime
140 * @param integer $refresh_time Timestamp of the last valid cache
141 * @return void
143 function setRefreshTime($refresh_time)
145 $this->_refreshTime = $refresh_time;
149 // }}}
150 // {{{ setLifeTime()
153 * $this->_lifeTime setter
155 * Use this method to set $this->_lifeTime value
157 * @access public
158 * @see get$lifeTime
159 * @param integer $life_time Cache lifetime (in seconds)
160 * @return void
162 function setLifeTime($life_time = 3600)
164 $this->_lifeTime = $life_time;
165 $this->setRefreshTime(time() - $this->_lifeTime);
168 // }}}
169 // {{{ setMemoryCaching()
172 * $this->_memoryCaching setter
174 * Use this method to set $this->_memoryCaching value
176 * @access public
177 * @see get$memoryCaching
178 * @param boolean $memory_caching Enable / Disable "Memory Caching"
180 * NB : There is no lifetime for memory caching !
181 * @return void
183 function setMemoryCaching($memory_caching = false)
185 $this->_memoryCaching = (bool)$memory_caching;
189 // }}}
190 // {{{ setAutomaticSerialization()
193 * $this->_automaticSerialization setter
195 * Use this method to set $this->_automaticSerialization value
197 * @access public
198 * @see get$automaticSerialization
199 * @param boolean $automatic_serialization Enable / disable automatic serialization
200 * @return void
202 function setAutomaticSerialization($automatic_serialization = false)
204 $this->_automaticSerialization = (bool)$automatic_serialization;
208 // }}}
211 // ---- Public methods ---- //
214 // {{{ init()
217 * Class constructor (ALA Akelos Framework)
219 * @access public
220 * @param array $options
221 * <code>
222 * $options = array(
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)
227 * );
228 * </code>
229 * @return void
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;
245 // }}}
246 // {{{ get()
249 * Test if a cache is available and (if yes) return it
251 * @access public
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')
258 $this->_id = $id;
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('
267 SELECT cache_data
268 FROM cache
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);
276 }else{
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;
288 return $data;
291 return false;
295 // }}}
296 // {{{ save()
299 * Save some data in the cache
301 * @access public
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(
321 'cache', array(
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)),
326 'id');
328 if($ret == 0){
329 return false;
330 }else{
331 if($this->_memoryCaching){
332 $this->_memoryCachingArray[$cache_hash] = $data;
334 return true;
338 // }}}
339 // {{{ remove()
342 * Remove a cache item from the database
344 * @access public
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();
362 // }}}
363 // {{{ clean()
366 * Clean the cache
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
372 * @access public
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:
378 * - old
379 * - ingroup
380 * - notingroup
381 * @return boolean True if no problem
383 function clean($group = false, $mode = 'ingroup')
385 switch ($mode) {
386 case '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();
391 case 'notingroup':
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();
397 case 'old':
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();
403 default:
404 return true;
408 // }}}
412 }// End of if(!defined('AK_ADODBCACHE_CLASS_INCLUDED')){