Single quotes
[mediawiki.git] / includes / ObjectCache.php
blob7e182d8d215694638ab50c2e6758ab5feb85bb51
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 /**
23 * @package MediaWiki
26 /**
27 * Simple generic object store
29 * interface is intended to be more or less compatible with
30 * the PHP memcached client.
32 * backends for local hash array and SQL table included:
33 * $bag = new HashBagOStuff();
34 * $bag = new MysqlBagOStuff($tablename); # connect to db first
36 * @package MediaWiki
37 * @abstract
39 class BagOStuff {
40 var $debugmode;
42 function BagOStuff() {
43 $this->set_debug( false );
46 function set_debug($bool) {
47 $this->debugmode = $bool;
50 /* *** THE GUTS OF THE OPERATION *** */
51 /* Override these with functional things in subclasses */
53 function get($key) {
54 /* stub */
55 return false;
58 function set($key, $value, $exptime=0) {
59 /* stub */
60 return false;
63 function delete($key, $time=0) {
64 /* stub */
65 return false;
68 function lock($key, $timeout = 0) {
69 /* stub */
70 return true;
73 function unlock($key) {
74 /* stub */
75 return true;
78 /* *** Emulated functions *** */
79 /* Better performance can likely be got with custom written versions */
80 function get_multi($keys) {
81 $out = array();
82 foreach($keys as $key)
83 $out[$key] = $this->get($key);
84 return $out;
87 function set_multi($hash, $exptime=0) {
88 foreach($hash as $key => $value)
89 $this->set($key, $value, $exptime);
92 function add($key, $value, $exptime=0) {
93 if( $this->get($key) == false ) {
94 $this->set($key, $value, $exptime);
95 return true;
99 function add_multi($hash, $exptime=0) {
100 foreach($hash as $key => $value)
101 $this->add($key, $value, $exptime);
104 function delete_multi($keys, $time=0) {
105 foreach($keys as $key)
106 $this->delete($key, $time);
109 function replace($key, $value, $exptime=0) {
110 if( $this->get($key) !== false )
111 $this->set($key, $value, $exptime);
114 function incr($key, $value=1) {
115 if ( !$this->lock($key) ) {
116 return false;
118 $value = intval($value);
119 if($value < 0) $value = 0;
121 $n = false;
122 if( ($n = $this->get($key)) !== false ) {
123 $n += $value;
124 $this->set($key, $n); // exptime?
126 $this->unlock($key);
127 return $n;
130 function decr($key, $value=1) {
131 if ( !$this->lock($key) ) {
132 return false;
134 $value = intval($value);
135 if($value < 0) $value = 0;
137 $m = false;
138 if( ($n = $this->get($key)) !== false ) {
139 $m = $n - $value;
140 if($m < 0) $m = 0;
141 $this->set($key, $m); // exptime?
143 $this->unlock($key);
144 return $m;
147 function _debug($text) {
148 if($this->debugmode)
149 wfDebug("BagOStuff debug: $text\n");
155 * Functional versions!
156 * @todo document
157 * @package MediaWiki
159 class HashBagOStuff extends BagOStuff {
161 This is a test of the interface, mainly. It stores
162 things in an associative array, which is not going to
163 persist between program runs.
165 var $bag;
167 function HashBagOStuff() {
168 $this->bag = array();
171 function _expire($key) {
172 $et = $this->bag[$key][1];
173 if(($et == 0) || ($et > time()))
174 return false;
175 $this->delete($key);
176 return true;
179 function get($key) {
180 if(!$this->bag[$key])
181 return false;
182 if($this->_expire($key))
183 return false;
184 return $this->bag[$key][0];
187 function set($key,$value,$exptime=0) {
188 if(($exptime != 0) && ($exptime < 3600*24*30))
189 $exptime = time() + $exptime;
190 $this->bag[$key] = array( $value, $exptime );
193 function delete($key,$time=0) {
194 if(!$this->bag[$key])
195 return false;
196 unset($this->bag[$key]);
197 return true;
202 CREATE TABLE objectcache (
203 keyname char(255) binary not null default '',
204 value mediumblob,
205 exptime datetime,
206 unique key (keyname),
207 key (exptime)
212 * @todo document
213 * @abstract
214 * @package MediaWiki
216 class SqlBagOStuff extends BagOStuff {
217 var $table;
219 function SqlBagOStuff($tablename = 'objectcache') {
220 $this->table = $tablename;
223 function get($key) {
224 /* expire old entries if any */
225 $this->expireall();
227 $res = $this->_query(
228 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
229 if(!$res) {
230 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
231 return false;
233 if($row=$this->_fetchobject($res)) {
234 $this->_debug("get: retrieved data; exp time is " . $row->exptime);
235 return unserialize($row->value);
236 } else {
237 $this->_debug('get: no matching rows');
239 return false;
242 function set($key,$value,$exptime=0) {
243 $exptime = intval($exptime);
244 if($exptime < 0) $exptime = 0;
245 if($exptime == 0) {
246 $exp = $this->_maxdatetime();
247 } else {
248 if($exptime < 3600*24*30)
249 $exptime += time();
250 $exp = $this->_fromunixtime($exptime);
252 $this->delete( $key );
253 $this->_query(
254 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
255 $key, serialize($value));
256 return true; /* ? */
259 function delete($key,$time=0) {
260 $this->_query(
261 "DELETE FROM $0 WHERE keyname='$1'", $key );
262 return true; /* ? */
265 function getTableName() {
266 return $this->table;
269 function _query($sql) {
270 $reps = func_get_args();
271 $reps[0] = $this->getTableName();
272 // ewwww
273 for($i=0;$i<count($reps);$i++) {
274 $sql = str_replace(
275 '$' . $i,
276 $this->_strencode($reps[$i]),
277 $sql);
279 $res = $this->_doquery($sql);
280 if($res == false) {
281 $this->_debug('query failed: ' . $this->_dberror($res));
283 return $res;
286 function _strencode($str) {
287 /* Protect strings in SQL */
288 return str_replace( "'", "''", $str );
291 function _doquery($sql) {
292 die( 'abstract function SqlBagOStuff::_doquery() must be defined' );
295 function _fetchrow($res) {
296 die( 'abstract function SqlBagOStuff::_fetchrow() must be defined' );
299 function _freeresult($result) {
300 /* stub */
301 return false;
304 function _dberror($result) {
305 /* stub */
306 return 'unknown error';
309 function _maxdatetime() {
310 die( 'abstract function SqlBagOStuff::_maxdatetime() must be defined' );
313 function _fromunixtime() {
314 die( 'abstract function SqlBagOStuff::_fromunixtime() must be defined' );
317 function expireall() {
318 /* Remove any items that have expired */
319 $now=$this->_fromunixtime(time());
320 $this->_query( "DELETE FROM $0 WHERE exptime<'$now'" );
323 function deleteall() {
324 /* Clear *all* items from cache table */
325 $this->_query( "DELETE FROM $0" );
330 * @todo document
331 * @package MediaWiki
333 class MediaWikiBagOStuff extends SqlBagOStuff {
334 var $tableInitialised = false;
336 function _doquery($sql) {
337 $dbw =& wfGetDB( DB_MASTER );
338 return $dbw->query($sql, 'MediaWikiBagOStuff:_doquery');
340 function _fetchobject($result) {
341 $dbw =& wfGetDB( DB_MASTER );
342 return $dbw->fetchObject($result);
344 function _freeresult($result) {
345 $dbw =& wfGetDB( DB_MASTER );
346 return $dbw->freeResult($result);
348 function _dberror($result) {
349 $dbw =& wfGetDB( DB_MASTER );
350 return $dbw->lastError();
352 function _maxdatetime() {
353 return '9999-12-31 12:59:59';
355 function _fromunixtime($ts) {
356 return gmdate( 'Y-m-d H:i:s', $ts );
358 function _strencode($s) {
359 $dbw =& wfGetDB( DB_MASTER );
360 return $dbw->strencode($s);
362 function getTableName() {
363 if ( !$this->tableInitialised ) {
364 $dbw =& wfGetDB( DB_MASTER );
365 $this->table = $dbw->tableName( $this->table );
366 $this->tableInitialised = true;
368 return $this->table;
373 * This is a wrapper for Turck MMCache's shared memory functions.
375 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
376 * to use a weird custom serializer that randomly segfaults. So we wrap calls
377 * with serialize()/unserialize().
379 * The thing I noticed about the Turck serialized data was that unlike ordinary
380 * serialize(), it contained the names of methods, and judging by the amount of
381 * binary data, perhaps even the bytecode of the methods themselves. It may be
382 * that Turck's serializer is faster, so a possible future extension would be
383 * to use it for arrays but not for objects.
385 * @package MediaWiki
387 class TurckBagOStuff extends BagOStuff {
388 function get($key) {
389 $val = mmcache_get( $key );
390 if ( is_string( $val ) ) {
391 $val = unserialize( $val );
393 return $val;
396 function set($key, $value, $exptime=0) {
397 mmcache_put( $key, serialize( $value ), $exptime );
398 return true;
401 function delete($key, $time=0) {
402 mmcache_rm( $key );
403 return true;
406 function lock($key, $waitTimeout = 0 ) {
407 mmcache_lock( $key );
408 return true;
411 function unlock($key) {
412 mmcache_unlock( $key );
413 return true;