Fixes #149
[akelos.git] / lib / AkCache / AkAdodbCache.php
blob81842343242815f26864647d205cad3011a70753
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 require_once(AK_LIB_DIR.'/Ak.php');
23 /**
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>
30 class AkAdodbCache
33 /**
34 * Handles an instance of current database conection using
35 * AdoBD
37 * @see setDb
38 * @access private
39 * @var object $_db
41 var $_db = NULL;
43 /**
44 * Timestamp of the last valid cache
46 * @see setRefreshTime
47 * @access private
48 * @var integer $_refreshTime
50 var $_refreshTime = NULL;
52 /**
53 * Cache lifetime (in seconds)
55 * @see setLifeTime
56 * @access private
57 * @var integer $_lifeTime
59 var $_lifeTime = 3600;
61 /**
62 * Enable / Disable "Memory Caching"
64 * NB : There is no lifetime for memory caching !
66 * @see setMemoryCaching
67 * @access private
68 * @var boolean $_memoryCaching
70 var $_memoryCaching = false;
72 /**
73 * Memory caching container array
75 * @access private
76 * @var array $_memoryCachingArray
78 var $_memoryCachingArray = array();
80 /**
81 * Enable / disable automatic serialization
83 * It can be used to save directly datas which aren't strings
84 * (but it's slower)
86 * @see setAutomaticSerialization
87 * @access private
88 * @var boolean $_automaticSerialization
90 var $_automaticSerialization = false;
92 /**
93 * $this->_db setter
95 * Use this method to set $this->_db value
97 * @access public
98 * @see get$db
99 * @param object $db Handles an instance of current database conection
100 * using AdoBD
101 * @return void
103 function setDb($db)
105 $this->_db = $db;
110 * $this->_refreshTime setter
112 * Use this method to set $this->_refreshTime value
114 * @access public
115 * @see get$refreshTime
116 * @param integer $refresh_time Timestamp of the last valid cache
117 * @return void
119 function setRefreshTime($refresh_time)
121 $this->_refreshTime = $refresh_time;
126 * $this->_lifeTime setter
128 * Use this method to set $this->_lifeTime value
130 * @access public
131 * @see get$lifeTime
132 * @param integer $life_time Cache lifetime (in seconds)
133 * @return void
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
146 * @access public
147 * @see get$memoryCaching
148 * @param boolean $memory_caching Enable / Disable "Memory Caching"
150 * NB : There is no lifetime for memory caching !
151 * @return void
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
164 * @access public
165 * @see get$automaticSerialization
166 * @param boolean $automatic_serialization Enable / disable automatic serialization
167 * @return void
169 function setAutomaticSerialization($automatic_serialization = false)
171 $this->_automaticSerialization = (bool)$automatic_serialization;
176 * Class constructor (ALA Akelos Framework)
178 * @access public
179 * @param array $options
180 * <code>
181 * $options = array(
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)
186 * );
187 * </code>
188 * @return void
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
207 * @access public
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')
214 $this->_id = $id;
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('
223 SELECT cache_data
224 FROM cache
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;
241 return $data;
245 * Save some data in the cache
247 * @access public
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(
268 'cache', array(
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)),
273 'id');
275 if($ret == 0){
276 return false;
277 }else{
278 if($this->_memoryCaching){
279 $this->_memoryCachingArray[$cache_hash] = $data;
281 return true;
286 * Remove a cache item from the database
288 * @access public
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));
304 * Clean the cache
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
310 * @access public
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:
316 * - old
317 * - ingroup
318 * - notingroup
319 * @return boolean True if no problem
321 function clean($group = false, $mode = 'ingroup')
323 switch ($mode) {
324 case 'ingroup':
325 return (bool)$this->_db->delete('DELETE FROM cache WHERE cache_group = '.$this->_db->quote_string($group));
326 case 'notingroup':
327 return (bool)$this->_db->delete('DELETE FROM cache WHERE cache_group NOT LIKE '.$this->_db->quote_string($group));
328 case 'old':
329 return (bool)$this->_db->delete('DELETE FROM cache WHERE expire < '.$this->_db->quote_datetime(time()));
330 default:
331 return true;