| [ Index ] |
PHP Cross Reference of Akelos Framework |
[Summary view] [Print] [Text view]
1 <?php 2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 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 // +----------------------------------------------------------------------+ 10 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> 17 */ 18 19 20 require_once (AK_LIB_DIR.'/Ak.php'); 21 22 23 /** 24 * Dabase cache driver for the AkCache class 25 * 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> 29 */ 30 class AkAdodbCache 31 { 32 33 /** 34 * Handles an instance of current database conection using 35 * AdoBD 36 * 37 * @see setDb 38 * @access private 39 * @var object $_db 40 */ 41 var $_db = NULL; 42 43 /** 44 * Timestamp of the last valid cache 45 * 46 * @see setRefreshTime 47 * @access private 48 * @var integer $_refreshTime 49 */ 50 var $_refreshTime = NULL; 51 52 /** 53 * Cache lifetime (in seconds) 54 * 55 * @see setLifeTime 56 * @access private 57 * @var integer $_lifeTime 58 */ 59 var $_lifeTime = 3600; 60 61 /** 62 * Enable / Disable "Memory Caching" 63 * 64 * NB : There is no lifetime for memory caching ! 65 * 66 * @see setMemoryCaching 67 * @access private 68 * @var boolean $_memoryCaching 69 */ 70 var $_memoryCaching = false; 71 72 /** 73 * Memory caching container array 74 * 75 * @access private 76 * @var array $_memoryCachingArray 77 */ 78 var $_memoryCachingArray = array(); 79 80 /** 81 * Enable / disable automatic serialization 82 * 83 * It can be used to save directly datas which aren't strings 84 * (but it's slower) 85 * 86 * @see setAutomaticSerialization 87 * @access private 88 * @var boolean $_automaticSerialization 89 */ 90 var $_automaticSerialization = false; 91 92 /** 93 * $this->_db setter 94 * 95 * Use this method to set $this->_db value 96 * 97 * @access public 98 * @see get$db 99 * @param object $db Handles an instance of current database conection 100 * using AdoBD 101 * @return void 102 */ 103 function setDb($db) 104 { 105 $this->_db = $db; 106 107 } 108 109 /** 110 * $this->_refreshTime setter 111 * 112 * Use this method to set $this->_refreshTime value 113 * 114 * @access public 115 * @see get$refreshTime 116 * @param integer $refresh_time Timestamp of the last valid cache 117 * @return void 118 */ 119 function setRefreshTime($refresh_time) 120 { 121 $this->_refreshTime = $refresh_time; 122 123 } 124 125 /** 126 * $this->_lifeTime setter 127 * 128 * Use this method to set $this->_lifeTime value 129 * 130 * @access public 131 * @see get$lifeTime 132 * @param integer $life_time Cache lifetime (in seconds) 133 * @return void 134 */ 135 function setLifeTime($life_time = 3600) 136 { 137 $this->_lifeTime = $life_time; 138 $this->setRefreshTime(time() - $this->_lifeTime); 139 } 140 141 /** 142 * $this->_memoryCaching setter 143 * 144 * Use this method to set $this->_memoryCaching value 145 * 146 * @access public 147 * @see get$memoryCaching 148 * @param boolean $memory_caching Enable / Disable "Memory Caching" 149 * 150 * NB : There is no lifetime for memory caching ! 151 * @return void 152 */ 153 function setMemoryCaching($memory_caching = false) 154 { 155 $this->_memoryCaching = (bool)$memory_caching; 156 157 } 158 159 /** 160 * $this->_automaticSerialization setter 161 * 162 * Use this method to set $this->_automaticSerialization value 163 * 164 * @access public 165 * @see get$automaticSerialization 166 * @param boolean $automatic_serialization Enable / disable automatic serialization 167 * @return void 168 */ 169 function setAutomaticSerialization($automatic_serialization = false) 170 { 171 $this->_automaticSerialization = (bool)$automatic_serialization; 172 173 } 174 175 /** 176 * Class constructor (ALA Akelos Framework) 177 * 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 189 */ 190 function init($options = array()) 191 { 192 $this->_db =& Ak::db(); 193 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; 199 } 200 } 201 $this->_refreshTime = time() - $this->_lifeTime; 202 return $this->_db?true:false; 203 } 204 205 /** 206 * Test if a cache is available and (if yes) return it 207 * 208 * @access public 209 * @param string $id Cache id 210 * @param string $group Name of the cache group. 211 * @return mixed Data of the cache (or false if no cache available) 212 */ 213 function get($id, $group = 'default') 214 { 215 $this->_id = $id; 216 $this->_group = $group; 217 $cache_hash = md5($this->_id).'_'.md5($this->_group); 218 219 if(isset($this->_memoryCachingArray[$cache_hash])){ 220 return $this->_memoryCachingArray[$cache_hash]; 221 } 222 223 $query_result = $this->_db->selectValue(' 224 SELECT cache_data 225 FROM cache 226 WHERE id = '.$this->_db->quote_string($cache_hash).' 227 AND cache_group = '.$this->_db->quote_string($this->_group).' 228 AND expire > '.$this->_db->quote_datetime($this->_refreshTime) 229 ); 230 if (!$query_result) return false; 231 232 $data = $this->_db->unescape_blob($query_result); 233 234 if($this->_automaticSerialization == true){ 235 $data = unserialize($data); 236 } 237 238 if($this->_memoryCaching){ 239 $this->_memoryCachingArray[$cache_hash] = $data; 240 } 241 242 return $data; 243 } 244 245 /** 246 * Save some data in the cache 247 * 248 * @access public 249 * @param string $data Data to put in cache 250 * @param string $id Cache id. By default it will use the Id specified 251 * when calling $this->get 252 * @param string $group Name of the cache group. By default it will use 253 * the group specified when calling $this->get 254 * @return boolean True if no problem 255 */ 256 function save($data, $id = null, $group = null) 257 { 258 $this->_id = isset($id) ? $id : $this->_id; 259 $this->_group = isset($group) ? $group : $this->_group; 260 261 $cache_hash = md5($this->_id).'_'.md5($this->_group); 262 263 if($this->_automaticSerialization == true){ 264 $data = serialize($data); 265 } 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'); 274 275 if(!$ret){ 276 return false; 277 }else{ 278 if($this->_memoryCaching){ 279 $this->_memoryCachingArray[$cache_hash] = $data; 280 } 281 return true; 282 } 283 } 284 285 /** 286 * Remove a cache item from the database 287 * 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 292 */ 293 function remove($id, $group = 'default') 294 { 295 $cache_hash = md5($id).'_'.md5($group); 296 297 if (isset($this->_memoryCachingArray[$cache_hash])) { 298 unset($this->_memoryCachingArray[$cache_hash]); 299 } 300 return (bool)$this->_db->delete('DELETE FROM cache WHERE id = '.$this->_db->quote_string($cache_hash)); 301 } 302 303 /** 304 * Clean the cache 305 * 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 309 * 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: 315 * 316 * - old 317 * - ingroup 318 * - notingroup 319 * @return boolean True if no problem 320 */ 321 function clean($group = false, $mode = 'ingroup') 322 { 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; 332 } 333 } 334 335 } 336 337 338 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Oct 27 12:43:49 2008 | Cross-referenced by PHPXref 0.6 |