Some documentation. Let's talk about it in wikitech-l.
[mediawiki.git] / includes / ObjectCache.php
blobfc7406613987435daaabe5bee016dfcabd2c401f
1 <?php
2 # $Id$
4 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
5 # http://www.mediawiki.org/
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # http://www.gnu.org/copyleft/gpl.html
21 /**
25 /**
26 * Simple generic object store
28 * interface is intended to be more or less compatible with
29 * the PHP memcached client.
31 * backends for local hash array and SQL table included:
32 * $bag = new HashBagOStuff();
33 * $bag = new MysqlBagOStuff($tablename); # connect to db first
35 * @abstract
37 class BagOStuff {
38 var $debugmode;
40 function BagOStuff() {
41 $this->set_debug( false );
44 function set_debug($bool) {
45 $this->debugmode = $bool;
48 /* *** THE GUTS OF THE OPERATION *** */
49 /* Override these with functional things in subclasses */
51 function get($key) {
52 /* stub */
53 return false;
56 function set($key, $value, $exptime=0) {
57 /* stub */
58 return false;
61 function delete($key, $time=0) {
62 /* stub */
63 return false;
66 function lock($key, $timeout = 0) {
67 /* stub */
68 return true;
71 function unlock($key) {
72 /* stub */
73 return true;
76 /* *** Emulated functions *** */
77 /* Better performance can likely be got with custom written versions */
78 function get_multi($keys) {
79 $out = array();
80 foreach($keys as $key)
81 $out[$key] = $this->get($key);
82 return $out;
85 function set_multi($hash, $exptime=0) {
86 foreach($hash as $key => $value)
87 $this->set($key, $value, $exptime);
90 function add($key, $value, $exptime=0) {
91 if( $this->get($key) == false ) {
92 $this->set($key, $value, $exptime);
93 return true;
97 function add_multi($hash, $exptime=0) {
98 foreach($hash as $key => $value)
99 $this->add($key, $value, $exptime);
102 function delete_multi($keys, $time=0) {
103 foreach($keys as $key)
104 $this->delete($key, $time);
107 function replace($key, $value, $exptime=0) {
108 if( $this->get($key) !== false )
109 $this->set($key, $value, $exptime);
112 function incr($key, $value=1) {
113 if ( !$this->lock($key) ) {
114 return false;
116 $value = intval($value);
117 if($value < 0) $value = 0;
119 $n = false;
120 if( ($n = $this->get($key)) !== false ) {
121 $n += $value;
122 $this->set($key, $n); // exptime?
124 $this->unlock($key);
125 return $n;
128 function decr($key, $value=1) {
129 if ( !$this->lock($key) ) {
130 return false;
132 $value = intval($value);
133 if($value < 0) $value = 0;
135 $m = false;
136 if( ($n = $this->get($key)) !== false ) {
137 $m = $n - $value;
138 if($m < 0) $m = 0;
139 $this->set($key, $m); // exptime?
141 $this->unlock($key);
142 return $m;
145 function _debug($text) {
146 if($this->debugmode)
147 wfDebug("BagOStuff debug: $text\n");
153 * Functional versions!
154 * @todo document
156 class HashBagOStuff extends BagOStuff {
158 This is a test of the interface, mainly. It stores
159 things in an associative array, which is not going to
160 persist between program runs.
162 var $bag;
164 function HashBagOStuff() {
165 $this->bag = array();
168 function _expire($key) {
169 $et = $this->bag[$key][1];
170 if(($et == 0) || ($et > time()))
171 return false;
172 $this->delete($key);
173 return true;
176 function get($key) {
177 if(!$this->bag[$key])
178 return false;
179 if($this->_expire($key))
180 return false;
181 return $this->bag[$key][0];
184 function set($key,$value,$exptime=0) {
185 if(($exptime != 0) && ($exptime < 3600*24*30))
186 $exptime = time() + $exptime;
187 $this->bag[$key] = array( $value, $exptime );
190 function delete($key,$time=0) {
191 if(!$this->bag[$key])
192 return false;
193 unset($this->bag[$key]);
194 return true;
199 CREATE TABLE objectcache (
200 keyname char(255) binary not null default '',
201 value mediumblob,
202 exptime datetime,
203 unique key (keyname),
204 key (exptime)
209 * @todo document
210 * @abstract
212 class SqlBagOStuff extends BagOStuff {
213 var $table;
215 function SqlBagOStuff($tablename = 'objectcache') {
216 $this->table = $tablename;
219 function get($key) {
220 /* expire old entries if any */
221 $this->expireall();
223 $res = $this->_query(
224 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
225 if(!$res) {
226 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
227 return false;
229 if($row=$this->_fetchobject($res)) {
230 $this->_debug("get: retrieved data; exp time is " . $row->exptime);
231 return unserialize($row->value);
232 } else {
233 $this->_debug('get: no matching rows');
235 return false;
238 function set($key,$value,$exptime=0) {
239 $exptime = intval($exptime);
240 if($exptime < 0) $exptime = 0;
241 if($exptime == 0) {
242 $exp = $this->_maxdatetime();
243 } else {
244 if($exptime < 3600*24*30)
245 $exptime += time();
246 $exp = $this->_fromunixtime($exptime);
248 $this->delete( $key );
249 $this->_query(
250 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
251 $key, serialize($value));
252 return true; /* ? */
255 function delete($key,$time=0) {
256 $this->_query(
257 "DELETE FROM $0 WHERE keyname='$1'", $key );
258 return true; /* ? */
261 function getTableName() {
262 return $this->table;
265 function _query($sql) {
266 $reps = func_get_args();
267 $reps[0] = $this->getTableName();
268 // ewwww
269 for($i=0;$i<count($reps);$i++) {
270 $sql = str_replace(
271 '$' . $i,
272 $this->_strencode($reps[$i]),
273 $sql);
275 $res = $this->_doquery($sql);
276 if($res == false) {
277 $this->_debug('query failed: ' . $this->_dberror($res));
279 return $res;
282 function _strencode($str) {
283 /* Protect strings in SQL */
284 return str_replace( "'", "''", $str );
287 function _doquery($sql) {
288 die( 'abstract function SqlBagOStuff::_doquery() must be defined' );
291 function _fetchrow($res) {
292 die( 'abstract function SqlBagOStuff::_fetchrow() must be defined' );
295 function _freeresult($result) {
296 /* stub */
297 return false;
300 function _dberror($result) {
301 /* stub */
302 return 'unknown error';
305 function _maxdatetime() {
306 die( 'abstract function SqlBagOStuff::_maxdatetime() must be defined' );
309 function _fromunixtime() {
310 die( 'abstract function SqlBagOStuff::_fromunixtime() must be defined' );
313 function expireall() {
314 /* Remove any items that have expired */
315 $now=$this->_fromunixtime(time());
316 $this->_query( "DELETE FROM $0 WHERE exptime<'$now'" );
319 function deleteall() {
320 /* Clear *all* items from cache table */
321 $this->_query( "DELETE FROM $0" );
326 * @todo document
328 class MediaWikiBagOStuff extends SqlBagOStuff {
329 var $tableInitialised = false;
331 function _doquery($sql) {
332 $dbw =& wfGetDB( DB_MASTER );
333 return $dbw->query($sql, 'MediaWikiBagOStuff:_doquery');
335 function _fetchobject($result) {
336 $dbw =& wfGetDB( DB_MASTER );
337 return $dbw->fetchObject($result);
339 function _freeresult($result) {
340 $dbw =& wfGetDB( DB_MASTER );
341 return $dbw->freeResult($result);
343 function _dberror($result) {
344 $dbw =& wfGetDB( DB_MASTER );
345 return $dbw->lastError();
347 function _maxdatetime() {
348 return '9999-12-31 12:59:59';
350 function _fromunixtime($ts) {
351 return gmdate( 'Y-m-d H:i:s', $ts );
353 function _strencode($s) {
354 $dbw =& wfGetDB( DB_MASTER );
355 return $dbw->strencode($s);
357 function getTableName() {
358 if ( !$this->tableInitialised ) {
359 $dbw =& wfGetDB( DB_MASTER );
360 $this->table = $dbw->tableName( $this->table );
361 $this->tableInitialised = true;
363 return $this->table;
368 * @todo document
370 class TurckBagOStuff extends BagOStuff {
371 function get($key) {
372 return mmcache_get( $key );
375 function set($key, $value, $exptime=0) {
376 mmcache_put( $key, $value, $exptime );
377 return true;
380 function delete($key, $time=0) {
381 mmcache_rm( $key );
382 return true;
385 function lock($key, $waitTimeout = 0 ) {
386 mmcache_lock( $key );
387 return true;
390 function unlock($key) {
391 mmcache_unlock( $key );
392 return true;