| [ Index ] |
|
Code source de PHP PEAR 1.4.5 |
1 <?php 2 // +----------------------------------------------------------------------+ 3 // | PEAR :: Cache | 4 // +----------------------------------------------------------------------+ 5 // | Copyright (c) 1997-2003 The PHP Group | 6 // +----------------------------------------------------------------------+ 7 // | This source file is subject to version 2.0 of the PHP license, | 8 // | that is bundled with this package in the file LICENSE, and is | 9 // | available at through the world-wide-web at | 10 // | http://www.php.net/license/2_02.txt. | 11 // | If you did not receive a copy of the PHP license and are unable to | 12 // | obtain it through the world-wide-web, please send a note to | 13 // | license@php.net so we can mail you a copy immediately. | 14 // +----------------------------------------------------------------------+ 15 // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> | 16 // | Sebastian Bergmann <sb@sebastian-bergmann.de> | 17 // +----------------------------------------------------------------------+ 18 // 19 // $Id: phplib.php,v 1.3 2003/01/04 11:54:46 mj Exp $ 20 21 require_once 'Cache/Container.php'; 22 23 /** 24 * Stores cache data into a database table using PHPLibs DB abstraction. 25 * 26 * WARNING: Other systems might or might not support certain datatypes of 27 * the tables shown. As far as I know there's no large binary 28 * type in SQL-92 or SQL-99. Postgres seems to lack any 29 * BLOB or TEXT type, for MS-SQL you could use IMAGE, don't know 30 * about other databases. Please add sugestions for other databases to 31 * the inline docs. 32 * 33 * The field 'changed' is used by the garbage collection. Depending on 34 * your databasesystem you might have to subclass fetch() and garbageCollection(). 35 * 36 * For _MySQL_ you need this DB table: 37 * 38 * CREATE TABLE cache ( 39 * id CHAR(32) NOT NULL DEFAULT '', 40 * cachegroup VARCHAR(127) NOT NULL DEFAULT '', 41 * cachedata BLOB NOT NULL DEFAULT '', 42 * userdata VARCHAR(255) NOT NULL DEFAUL '', 43 * expires INT(9) NOT NULL DEFAULT 0, 44 * 45 * changed TIMESTAMP(14) NOT NULL, 46 * 47 * INDEX (expires), 48 * PRIMARY KEY (id, cachegroup) 49 * ) 50 * 51 * 52 * @author Ulf Wendel <ulf.wendel@phpdoc.de>, Sebastian Bergmann <sb@sebastian-bergmann.de> 53 * @version $Id: phplib.php,v 1.3 2003/01/04 11:54:46 mj Exp $ 54 * @package Cache 55 * @see save() 56 */ 57 class Cache_Container_phplib extends Cache_Container { 58 59 /** 60 * Name of the DB table to store caching data 61 * 62 * @see Cache_Container_file::$filename_prefix 63 */ 64 var $cache_table = 'cache'; 65 66 /** 67 * PHPLib object 68 * 69 * @var object PEAR_DB 70 */ 71 var $db; 72 73 /** 74 * Name of the PHPLib DB class to use 75 * 76 * @var string 77 * @see $db_path, $local_path 78 */ 79 var $db_class = ''; 80 81 /** 82 * Filename of your local.inc 83 * 84 * If empty, 'local.inc' is assumed. 85 * 86 * @var string 87 */ 88 var $local_file = ''; 89 90 /** 91 * Include path for you local.inc 92 * 93 * HINT: If your're not using PHPLib's prepend.php you must 94 * take care that all classes (files) references by you 95 * local.inc are included automatically. So you might 96 * want to write a new local2.inc that only referrs to 97 * the database class (file) you're using and includes all required files. 98 * 99 * @var string path to your local.inc - make sure to add a trailing slash 100 * @see $local_file 101 */ 102 var $local_path = ''; 103 104 /** 105 * Creates an instance of a phplib db class to use it for storage. 106 * 107 * @param mixed If empty the object tries to used the 108 * preconfigured class variables. If given it 109 * must be an array with: 110 * db_class => name of the DB class to use 111 * optional: 112 * db_file => filename of the DB class 113 * db_path => path to the DB class 114 * local_file => kind of local.inc 115 * local_patk => path to the local.inc 116 * see $local_path for some hints.s 117 * @see $local_path 118 */ 119 function Cache_Container_phplib($options = '') { 120 if (is_array($options)) 121 $this->setOptions($options, array_merge($this->allowed_options, array('db_class', 'db_file', 'db_path', 'local_file', 'local_path'))); 122 123 if (!$this->db_class) 124 return new Cache_Error('No database class specified.', __FILE__, __LINE__); 125 126 // include the required files 127 if ($this->db_file) 128 include_once($this->db_path . $this->db_file); 129 130 if ($this->local_file) 131 include_once($this->local_path . $this->local_file); 132 133 // create a db object 134 $this->db = new $this->db_class; 135 } // end constructor 136 137 function fetch($id, $group) { 138 $query = sprintf("SELECT expires, cachedata, userdata FROM %s WHERE id = '%s' AND cachegroup = '%s'", 139 $this->cache_table, 140 $id, 141 $group 142 ); 143 $this->db->query($query); 144 if (!$this->db->Next_Record()) 145 return array(NULL, NULL, NULL); 146 147 // last used required by the garbage collection 148 // WARNING: might be MySQL specific 149 $query = sprintf("UPDATE %s SET changed = (NOW() + 0) WHERE id = '%s' AND cachegroup = '%s'", 150 $this->cache_table, 151 $id, 152 $group 153 ); 154 $this->db->query($query); 155 return array($this->db->f('expires'), $this->decode($this->db->f('cachedata')), $this->db->f('userdata')); 156 } // end func fetch 157 158 /** 159 * Stores a dataset. 160 * 161 * WARNING: we use the SQL command REPLACE INTO this might be 162 * MySQL specific. As MySQL is very popular the method should 163 * work fine for 95% of you. 164 */ 165 function save($id, $data, $expires, $group) { 166 $this->flushPreload($id, $group); 167 168 $query = sprintf("REPLACE INTO %s (cachedata, expires, id, cachegroup) VALUES ('%s', %d, '%s', '%s')", 169 $this->cache_table, 170 addslashes($this->encode($data)), 171 $this->getExpiresAbsolute($expires) , 172 $id, 173 $group 174 ); 175 $this->db->query($query); 176 177 return (boolean)$this->db->affected_rows(); 178 } // end func save 179 180 function remove($id, $group) { 181 $this->flushPreload($id, $group); 182 $this->db->query( 183 sprintf("DELETE FROM %s WHERE id = '%s' AND cachegroup = '%s'", 184 $this->cache_table, 185 $id, 186 $group 187 ) 188 ); 189 190 return (boolean)$this->db->affected_rows(); 191 } // end func remove 192 193 function flush($group) { 194 $this->flushPreload(); 195 196 if ($group) { 197 $this->db->query(sprintf("DELETE FROM %s WHERE cachegroup = '%s'", $this->cache_table, $group)); 198 } else { 199 $this->db->query(sprintf("DELETE FROM %s", $this->cache_table)); 200 } 201 202 return $this->db->affected_rows(); 203 } // end func flush 204 205 function idExists($id, $group) { 206 $this->db->query( 207 sprintf("SELECT id FROM %s WHERE ID = '%s' AND cachegroup = '%s'", 208 $this->cache_table, 209 $id, 210 $group 211 ) 212 ); 213 214 return (boolean)$this->db->nf(); 215 } // end func isExists 216 217 function garbageCollection($maxlifetime) { 218 $this->flushPreload(); 219 220 $this->db->query( 221 sprintf("DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)", 222 $this->cache_table, 223 time(), 224 $maxlifetime 225 ) 226 ); 227 228 //check for total size of cache 229 $query = sprintf('select sum(length(cachedata)) as CacheSize from %s', 230 $this->cache_table 231 ); 232 233 $this->db->query($query); 234 $this->db->Next_Record(); 235 $cachesize = $this->db->f('CacheSize'); 236 //if cache is to big. 237 if ($cachesize > $this->highwater) 238 { 239 //find the lowwater mark. 240 $query = sprintf('select length(cachedata) as size, changed from %s order by changed DESC', 241 $this->cache_table 242 ); 243 $this->db->query($query); 244 245 $keep_size=0; 246 while ($keep_size < $this->lowwater && $this->db->Next_Record() ) 247 { 248 $keep_size += $this->db->f('size'); 249 } 250 //delete all entries, which were changed before the "lowwwater mark" 251 $query = sprintf('delete from %s where changed <= '.$this->db->f('changed'), 252 $this->cache_table 253 ); 254 255 $this->db->query($query); 256 } 257 } // end func garbageCollection 258 } 259 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 14:08:00 2007 | par Balluche grâce à PHPXref 0.7 |