BUG 1653 - Removing hardcoded messages in Special:Allmessages
[mediawiki.git] / includes / ObjectCache.php
blobb4bb22b59e3b8bf942d886f35d28e660e68f6a64
1 <?php
3 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 # http://www.gnu.org/copyleft/gpl.html
20 /**
22 * @package MediaWiki
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 * @package MediaWiki
36 * @abstract
38 class BagOStuff {
39 var $debugmode;
41 function BagOStuff() {
42 $this->set_debug( false );
45 function set_debug($bool) {
46 $this->debugmode = $bool;
49 /* *** THE GUTS OF THE OPERATION *** */
50 /* Override these with functional things in subclasses */
52 function get($key) {
53 /* stub */
54 return false;
57 function set($key, $value, $exptime=0) {
58 /* stub */
59 return false;
62 function delete($key, $time=0) {
63 /* stub */
64 return false;
67 function lock($key, $timeout = 0) {
68 /* stub */
69 return true;
72 function unlock($key) {
73 /* stub */
74 return true;
77 /* *** Emulated functions *** */
78 /* Better performance can likely be got with custom written versions */
79 function get_multi($keys) {
80 $out = array();
81 foreach($keys as $key)
82 $out[$key] = $this->get($key);
83 return $out;
86 function set_multi($hash, $exptime=0) {
87 foreach($hash as $key => $value)
88 $this->set($key, $value, $exptime);
91 function add($key, $value, $exptime=0) {
92 if( $this->get($key) == false ) {
93 $this->set($key, $value, $exptime);
94 return true;
98 function add_multi($hash, $exptime=0) {
99 foreach($hash as $key => $value)
100 $this->add($key, $value, $exptime);
103 function delete_multi($keys, $time=0) {
104 foreach($keys as $key)
105 $this->delete($key, $time);
108 function replace($key, $value, $exptime=0) {
109 if( $this->get($key) !== false )
110 $this->set($key, $value, $exptime);
113 function incr($key, $value=1) {
114 if ( !$this->lock($key) ) {
115 return false;
117 $value = intval($value);
118 if($value < 0) $value = 0;
120 $n = false;
121 if( ($n = $this->get($key)) !== false ) {
122 $n += $value;
123 $this->set($key, $n); // exptime?
125 $this->unlock($key);
126 return $n;
129 function decr($key, $value=1) {
130 if ( !$this->lock($key) ) {
131 return false;
133 $value = intval($value);
134 if($value < 0) $value = 0;
136 $m = false;
137 if( ($n = $this->get($key)) !== false ) {
138 $m = $n - $value;
139 if($m < 0) $m = 0;
140 $this->set($key, $m); // exptime?
142 $this->unlock($key);
143 return $m;
146 function _debug($text) {
147 if($this->debugmode)
148 wfDebug("BagOStuff debug: $text\n");
154 * Functional versions!
155 * @todo document
156 * @package MediaWiki
158 class HashBagOStuff extends BagOStuff {
160 This is a test of the interface, mainly. It stores
161 things in an associative array, which is not going to
162 persist between program runs.
164 var $bag;
166 function HashBagOStuff() {
167 $this->bag = array();
170 function _expire($key) {
171 $et = $this->bag[$key][1];
172 if(($et == 0) || ($et > time()))
173 return false;
174 $this->delete($key);
175 return true;
178 function get($key) {
179 if(!$this->bag[$key])
180 return false;
181 if($this->_expire($key))
182 return false;
183 return $this->bag[$key][0];
186 function set($key,$value,$exptime=0) {
187 if(($exptime != 0) && ($exptime < 3600*24*30))
188 $exptime = time() + $exptime;
189 $this->bag[$key] = array( $value, $exptime );
192 function delete($key,$time=0) {
193 if(!$this->bag[$key])
194 return false;
195 unset($this->bag[$key]);
196 return true;
201 CREATE TABLE objectcache (
202 keyname char(255) binary not null default '',
203 value mediumblob,
204 exptime datetime,
205 unique key (keyname),
206 key (exptime)
211 * @todo document
212 * @abstract
213 * @package MediaWiki
215 class SqlBagOStuff extends BagOStuff {
216 var $table;
217 var $lastexpireall = 0;
219 function SqlBagOStuff($tablename = 'objectcache') {
220 $this->table = $tablename;
223 function get($key) {
224 /* expire old entries if any */
225 $this->garbageCollect();
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 $this->_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, $this->_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 garbageCollect() {
318 $nowtime = time();
319 /* Avoid repeating the delete within a few seconds */
320 if ( $nowtime > ($this->lastexpireall + 1) ) {
321 $this->lastexpireall = $nowtime;
322 $this->expireall();
326 function expireall() {
327 /* Remove any items that have expired */
328 $now = $this->_fromunixtime( time() );
329 $this->_query( "DELETE FROM $0 WHERE exptime<'$now'" );
332 function deleteall() {
333 /* Clear *all* items from cache table */
334 $this->_query( "DELETE FROM $0" );
338 * Serialize an object and, if possible, compress the representation.
339 * On typical message and page data, this can provide a 3X decrease
340 * in storage requirements.
342 * @param mixed $data
343 * @return string
345 function _serialize( &$data ) {
346 $serial = serialize( $data );
347 if( function_exists( 'gzdeflate' ) ) {
348 return gzdeflate( $serial );
349 } else {
350 return $serial;
355 * Unserialize and, if necessary, decompress an object.
356 * @param string $serial
357 * @return mixed
359 function &_unserialize( $serial ) {
360 if( function_exists( 'gzinflate' ) ) {
361 $decomp = @gzinflate( $serial );
362 if( false !== $decomp ) {
363 $serial = $decomp;
366 return unserialize( $serial );
371 * @todo document
372 * @package MediaWiki
374 class MediaWikiBagOStuff extends SqlBagOStuff {
375 var $tableInitialised = false;
377 function _doquery($sql) {
378 $dbw =& wfGetDB( DB_MASTER );
379 return $dbw->query($sql, 'MediaWikiBagOStuff:_doquery');
381 function _fetchobject($result) {
382 $dbw =& wfGetDB( DB_MASTER );
383 return $dbw->fetchObject($result);
385 function _freeresult($result) {
386 $dbw =& wfGetDB( DB_MASTER );
387 return $dbw->freeResult($result);
389 function _dberror($result) {
390 $dbw =& wfGetDB( DB_MASTER );
391 return $dbw->lastError();
393 function _maxdatetime() {
394 return '9999-12-31 12:59:59';
396 function _fromunixtime($ts) {
397 return gmdate( 'Y-m-d H:i:s', $ts );
399 function _strencode($s) {
400 $dbw =& wfGetDB( DB_MASTER );
401 return $dbw->strencode($s);
403 function getTableName() {
404 if ( !$this->tableInitialised ) {
405 $dbw =& wfGetDB( DB_MASTER );
406 $this->table = $dbw->tableName( $this->table );
407 $this->tableInitialised = true;
409 return $this->table;
414 * This is a wrapper for Turck MMCache's shared memory functions.
416 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
417 * to use a weird custom serializer that randomly segfaults. So we wrap calls
418 * with serialize()/unserialize().
420 * The thing I noticed about the Turck serialized data was that unlike ordinary
421 * serialize(), it contained the names of methods, and judging by the amount of
422 * binary data, perhaps even the bytecode of the methods themselves. It may be
423 * that Turck's serializer is faster, so a possible future extension would be
424 * to use it for arrays but not for objects.
426 * @package MediaWiki
428 class TurckBagOStuff extends BagOStuff {
429 function get($key) {
430 $val = mmcache_get( $key );
431 if ( is_string( $val ) ) {
432 $val = unserialize( $val );
434 return $val;
437 function set($key, $value, $exptime=0) {
438 mmcache_put( $key, serialize( $value ), $exptime );
439 return true;
442 function delete($key, $time=0) {
443 mmcache_rm( $key );
444 return true;
447 function lock($key, $waitTimeout = 0 ) {
448 mmcache_lock( $key );
449 return true;
452 function unlock($key) {
453 mmcache_unlock( $key );
454 return true;
459 * This is a wrapper for eAccelerator's shared memory functions.
461 * This is basically identical to the Turck MMCache version,
462 * mostly because eAccelerator is based on Turck MMCache.
464 * @package MediaWiki
466 class eAccelBagOStuff extends BagOStuff {
467 function get($key) {
468 $val = eaccelerator_get( $key );
469 if ( is_string( $val ) ) {
470 $val = unserialize( $val );
472 return $val;
475 function set($key, $value, $exptime=0) {
476 eaccelerator_put( $key, serialize( $value ), $exptime );
477 return true;
480 function delete($key, $time=0) {
481 eaccelerator_rm( $key );
482 return true;
485 function lock($key, $waitTimeout = 0 ) {
486 eaccelerator_lock( $key );
487 return true;
490 function unlock($key) {
491 eaccelerator_unlock( $key );
492 return true;