Document GetCacheVaryCookies hook
[mediawiki.git] / includes / BagOStuff.php
blob1cc7ce341e22c9bf9b6227c313891de447ca433f
1 <?php
3 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
20 /**
24 /**
25 * Simple generic object store
27 * interface is intended to be more or less compatible with
28 * the PHP memcached client.
30 * backends for local hash array and SQL table included:
31 * <code>
32 * $bag = new HashBagOStuff();
33 * $bag = new MysqlBagOStuff($tablename); # connect to db first
34 * </code>
37 class BagOStuff {
38 var $debugmode;
40 function __construct() {
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 function keys() {
77 /* stub */
78 return array();
81 /* *** Emulated functions *** */
82 /* Better performance can likely be got with custom written versions */
83 function get_multi($keys) {
84 $out = array();
85 foreach($keys as $key)
86 $out[$key] = $this->get($key);
87 return $out;
90 function set_multi($hash, $exptime=0) {
91 foreach($hash as $key => $value)
92 $this->set($key, $value, $exptime);
95 function add($key, $value, $exptime=0) {
96 if( $this->get($key) == false ) {
97 $this->set($key, $value, $exptime);
98 return true;
102 function add_multi($hash, $exptime=0) {
103 foreach($hash as $key => $value)
104 $this->add($key, $value, $exptime);
107 function delete_multi($keys, $time=0) {
108 foreach($keys as $key)
109 $this->delete($key, $time);
112 function replace($key, $value, $exptime=0) {
113 if( $this->get($key) !== false )
114 $this->set($key, $value, $exptime);
117 function incr($key, $value=1) {
118 if ( !$this->lock($key) ) {
119 return false;
121 $value = intval($value);
122 if($value < 0) $value = 0;
124 $n = false;
125 if( ($n = $this->get($key)) !== false ) {
126 $n += $value;
127 $this->set($key, $n); // exptime?
129 $this->unlock($key);
130 return $n;
133 function decr($key, $value=1) {
134 if ( !$this->lock($key) ) {
135 return false;
137 $value = intval($value);
138 if($value < 0) $value = 0;
140 $m = false;
141 if( ($n = $this->get($key)) !== false ) {
142 $m = $n - $value;
143 if($m < 0) $m = 0;
144 $this->set($key, $m); // exptime?
146 $this->unlock($key);
147 return $m;
150 function _debug($text) {
151 if($this->debugmode)
152 wfDebug("BagOStuff debug: $text\n");
156 * Convert an optionally relative time to an absolute time
158 static function convertExpiry( $exptime ) {
159 if(($exptime != 0) && ($exptime < 3600*24*30)) {
160 return time() + $exptime;
161 } else {
162 return $exptime;
169 * Functional versions!
170 * @todo document
172 class HashBagOStuff extends BagOStuff {
174 This is a test of the interface, mainly. It stores
175 things in an associative array, which is not going to
176 persist between program runs.
178 var $bag;
180 function __construct() {
181 $this->bag = array();
184 function _expire($key) {
185 $et = $this->bag[$key][1];
186 if(($et == 0) || ($et > time()))
187 return false;
188 $this->delete($key);
189 return true;
192 function get($key) {
193 if(!$this->bag[$key])
194 return false;
195 if($this->_expire($key))
196 return false;
197 return $this->bag[$key][0];
200 function set($key,$value,$exptime=0) {
201 $this->bag[$key] = array( $value, BagOStuff::convertExpiry( $exptime ) );
204 function delete($key,$time=0) {
205 if(!$this->bag[$key])
206 return false;
207 unset($this->bag[$key]);
208 return true;
211 function keys() {
212 return array_keys( $this->bag );
217 CREATE TABLE objectcache (
218 keyname char(255) binary not null default '',
219 value mediumblob,
220 exptime datetime,
221 unique key (keyname),
222 key (exptime)
227 * @todo document
228 * @abstract
230 abstract class SqlBagOStuff extends BagOStuff {
231 var $table;
232 var $lastexpireall = 0;
234 function __construct($tablename = 'objectcache') {
235 $this->table = $tablename;
238 function get($key) {
239 /* expire old entries if any */
240 $this->garbageCollect();
242 $res = $this->_query(
243 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
244 if(!$res) {
245 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
246 return false;
248 if($row=$this->_fetchobject($res)) {
249 $this->_debug("get: retrieved data; exp time is " . $row->exptime);
250 if ( $row->exptime != $this->_maxdatetime() &&
251 wfTimestamp( TS_UNIX, $row->exptime ) < time() )
253 $this->_debug("get: key has expired, deleting");
254 $this->delete($key);
255 return false;
257 return $this->_unserialize($this->_blobdecode($row->value));
258 } else {
259 $this->_debug('get: no matching rows');
261 return false;
264 function set($key,$value,$exptime=0) {
265 if ( wfReadOnly() ) {
266 return false;
268 $exptime = intval($exptime);
269 if($exptime < 0) $exptime = 0;
270 if($exptime == 0) {
271 $exp = $this->_maxdatetime();
272 } else {
273 if($exptime < 3.16e8) # ~10 years
274 $exptime += time();
275 $exp = $this->_fromunixtime($exptime);
277 $this->delete( $key );
278 $this->_doinsert($this->getTableName(), array(
279 'keyname' => $key,
280 'value' => $this->_blobencode($this->_serialize($value)),
281 'exptime' => $exp
283 return true; /* ? */
286 function delete($key,$time=0) {
287 if ( wfReadOnly() ) {
288 return false;
290 $this->_query(
291 "DELETE FROM $0 WHERE keyname='$1'", $key );
292 return true; /* ? */
295 function keys() {
296 $res = $this->_query( "SELECT keyname FROM $0" );
297 if(!$res) {
298 $this->_debug("keys: ** error: " . $this->_dberror($res) . " **");
299 return array();
301 $result = array();
302 while( $row = $this->_fetchobject($res) ) {
303 $result[] = $row->keyname;
305 return $result;
308 function getTableName() {
309 return $this->table;
312 function _query($sql) {
313 $reps = func_get_args();
314 $reps[0] = $this->getTableName();
315 // ewwww
316 for($i=0;$i<count($reps);$i++) {
317 $sql = str_replace(
318 '$' . $i,
319 $i > 0 ? $this->_strencode($reps[$i]) : $reps[$i],
320 $sql);
322 $res = $this->_doquery($sql);
323 if($res == false) {
324 $this->_debug('query failed: ' . $this->_dberror($res));
326 return $res;
329 function _strencode($str) {
330 /* Protect strings in SQL */
331 return str_replace( "'", "''", $str );
333 function _blobencode($str) {
334 return $str;
336 function _blobdecode($str) {
337 return $str;
340 abstract function _doinsert($table, $vals);
341 abstract function _doquery($sql);
343 function _freeresult($result) {
344 /* stub */
345 return false;
348 function _dberror($result) {
349 /* stub */
350 return 'unknown error';
353 abstract function _maxdatetime();
354 abstract function _fromunixtime($ts);
356 function garbageCollect() {
357 /* Ignore 99% of requests */
358 if ( !mt_rand( 0, 100 ) ) {
359 $nowtime = time();
360 /* Avoid repeating the delete within a few seconds */
361 if ( $nowtime > ($this->lastexpireall + 1) ) {
362 $this->lastexpireall = $nowtime;
363 $this->expireall();
368 function expireall() {
369 /* Remove any items that have expired */
370 if ( wfReadOnly() ) {
371 return false;
373 $now = $this->_fromunixtime( time() );
374 $this->_query( "DELETE FROM $0 WHERE exptime < '$now'" );
377 function deleteall() {
378 /* Clear *all* items from cache table */
379 if ( wfReadOnly() ) {
380 return false;
382 $this->_query( "DELETE FROM $0" );
386 * Serialize an object and, if possible, compress the representation.
387 * On typical message and page data, this can provide a 3X decrease
388 * in storage requirements.
390 * @param mixed $data
391 * @return string
393 function _serialize( &$data ) {
394 $serial = serialize( $data );
395 if( function_exists( 'gzdeflate' ) ) {
396 return gzdeflate( $serial );
397 } else {
398 return $serial;
403 * Unserialize and, if necessary, decompress an object.
404 * @param string $serial
405 * @return mixed
407 function _unserialize( $serial ) {
408 if( function_exists( 'gzinflate' ) ) {
409 $decomp = @gzinflate( $serial );
410 if( false !== $decomp ) {
411 $serial = $decomp;
414 $ret = unserialize( $serial );
415 return $ret;
420 * @todo document
422 class MediaWikiBagOStuff extends SqlBagOStuff {
423 var $tableInitialised = false;
425 function _doquery($sql) {
426 $dbw = wfGetDB( DB_MASTER );
427 return $dbw->query($sql, 'MediaWikiBagOStuff::_doquery');
429 function _doinsert($t, $v) {
430 $dbw = wfGetDB( DB_MASTER );
431 return $dbw->insert($t, $v, 'MediaWikiBagOStuff::_doinsert',
432 array( 'IGNORE' ) );
434 function _fetchobject($result) {
435 $dbw = wfGetDB( DB_MASTER );
436 return $dbw->fetchObject($result);
438 function _freeresult($result) {
439 $dbw = wfGetDB( DB_MASTER );
440 return $dbw->freeResult($result);
442 function _dberror($result) {
443 $dbw = wfGetDB( DB_MASTER );
444 return $dbw->lastError();
446 function _maxdatetime() {
447 if ( time() > 0x7fffffff ) {
448 return $this->_fromunixtime( 1<<62 );
449 } else {
450 return $this->_fromunixtime( 0x7fffffff );
453 function _fromunixtime($ts) {
454 $dbw = wfGetDB(DB_MASTER);
455 return $dbw->timestamp($ts);
457 function _strencode($s) {
458 $dbw = wfGetDB( DB_MASTER );
459 return $dbw->strencode($s);
461 function _blobencode($s) {
462 $dbw = wfGetDB( DB_MASTER );
463 return $dbw->encodeBlob($s);
465 function _blobdecode($s) {
466 $dbw = wfGetDB( DB_MASTER );
467 return $dbw->decodeBlob($s);
469 function getTableName() {
470 if ( !$this->tableInitialised ) {
471 $dbw = wfGetDB( DB_MASTER );
472 /* This is actually a hack, we should be able
473 to use Language classes here... or not */
474 if (!$dbw)
475 throw new MWException("Could not connect to database");
476 $this->table = $dbw->tableName( $this->table );
477 $this->tableInitialised = true;
479 return $this->table;
484 * This is a wrapper for Turck MMCache's shared memory functions.
486 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
487 * to use a weird custom serializer that randomly segfaults. So we wrap calls
488 * with serialize()/unserialize().
490 * The thing I noticed about the Turck serialized data was that unlike ordinary
491 * serialize(), it contained the names of methods, and judging by the amount of
492 * binary data, perhaps even the bytecode of the methods themselves. It may be
493 * that Turck's serializer is faster, so a possible future extension would be
494 * to use it for arrays but not for objects.
497 class TurckBagOStuff extends BagOStuff {
498 function get($key) {
499 $val = mmcache_get( $key );
500 if ( is_string( $val ) ) {
501 $val = unserialize( $val );
503 return $val;
506 function set($key, $value, $exptime=0) {
507 mmcache_put( $key, serialize( $value ), $exptime );
508 return true;
511 function delete($key, $time=0) {
512 mmcache_rm( $key );
513 return true;
516 function lock($key, $waitTimeout = 0 ) {
517 mmcache_lock( $key );
518 return true;
521 function unlock($key) {
522 mmcache_unlock( $key );
523 return true;
528 * This is a wrapper for APC's shared memory functions
531 class APCBagOStuff extends BagOStuff {
532 function get($key) {
533 $val = apc_fetch($key);
534 if ( is_string( $val ) ) {
535 $val = unserialize( $val );
537 return $val;
540 function set($key, $value, $exptime=0) {
541 apc_store($key, serialize($value), $exptime);
542 return true;
545 function delete($key, $time=0) {
546 apc_delete($key);
547 return true;
553 * This is a wrapper for eAccelerator's shared memory functions.
555 * This is basically identical to the Turck MMCache version,
556 * mostly because eAccelerator is based on Turck MMCache.
559 class eAccelBagOStuff extends BagOStuff {
560 function get($key) {
561 $val = eaccelerator_get( $key );
562 if ( is_string( $val ) ) {
563 $val = unserialize( $val );
565 return $val;
568 function set($key, $value, $exptime=0) {
569 eaccelerator_put( $key, serialize( $value ), $exptime );
570 return true;
573 function delete($key, $time=0) {
574 eaccelerator_rm( $key );
575 return true;
578 function lock($key, $waitTimeout = 0 ) {
579 eaccelerator_lock( $key );
580 return true;
583 function unlock($key) {
584 eaccelerator_unlock( $key );
585 return true;
590 * Wrapper for XCache object caching functions; identical interface
591 * to the APC wrapper
593 class XCacheBagOStuff extends BagOStuff {
596 * Get a value from the XCache object cache
598 * @param string $key Cache key
599 * @return mixed
601 public function get( $key ) {
602 $val = xcache_get( $key );
603 if( is_string( $val ) )
604 $val = unserialize( $val );
605 return $val;
609 * Store a value in the XCache object cache
611 * @param string $key Cache key
612 * @param mixed $value Object to store
613 * @param int $expire Expiration time
614 * @return bool
616 public function set( $key, $value, $expire = 0 ) {
617 xcache_set( $key, serialize( $value ), $expire );
618 return true;
622 * Remove a value from the XCache object cache
624 * @param string $key Cache key
625 * @param int $time Not used in this implementation
626 * @return bool
628 public function delete( $key, $time = 0 ) {
629 xcache_unset( $key );
630 return true;
636 * @todo document
638 class DBABagOStuff extends BagOStuff {
639 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
641 function __construct( $handler = 'db3', $dir = false ) {
642 if ( $dir === false ) {
643 global $wgTmpDirectory;
644 $dir = $wgTmpDirectory;
646 $this->mFile = "$dir/mw-cache-" . wfWikiID();
647 $this->mFile .= '.db';
648 wfDebug( __CLASS__.": using cache file {$this->mFile}\n" );
649 $this->mHandler = $handler;
653 * Encode value and expiry for storage
655 function encode( $value, $expiry ) {
656 # Convert to absolute time
657 $expiry = BagOStuff::convertExpiry( $expiry );
658 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
662 * @return list containing value first and expiry second
664 function decode( $blob ) {
665 if ( !is_string( $blob ) ) {
666 return array( null, 0 );
667 } else {
668 return array(
669 unserialize( substr( $blob, 11 ) ),
670 intval( substr( $blob, 0, 10 ) )
675 function getReader() {
676 if ( file_exists( $this->mFile ) ) {
677 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
678 } else {
679 $handle = $this->getWriter();
681 if ( !$handle ) {
682 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
684 return $handle;
687 function getWriter() {
688 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
689 if ( !$handle ) {
690 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
692 return $handle;
695 function get( $key ) {
696 wfProfileIn( __METHOD__ );
697 wfDebug( __METHOD__."($key)\n" );
698 $handle = $this->getReader();
699 if ( !$handle ) {
700 return null;
702 $val = dba_fetch( $key, $handle );
703 list( $val, $expiry ) = $this->decode( $val );
704 # Must close ASAP because locks are held
705 dba_close( $handle );
707 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
708 # Key is expired, delete it
709 $handle = $this->getWriter();
710 dba_delete( $key, $handle );
711 dba_close( $handle );
712 wfDebug( __METHOD__.": $key expired\n" );
713 $val = null;
715 wfProfileOut( __METHOD__ );
716 return $val;
719 function set( $key, $value, $exptime=0 ) {
720 wfProfileIn( __METHOD__ );
721 wfDebug( __METHOD__."($key)\n" );
722 $blob = $this->encode( $value, $exptime );
723 $handle = $this->getWriter();
724 if ( !$handle ) {
725 return false;
727 $ret = dba_replace( $key, $blob, $handle );
728 dba_close( $handle );
729 wfProfileOut( __METHOD__ );
730 return $ret;
733 function delete( $key, $time = 0 ) {
734 wfProfileIn( __METHOD__ );
735 wfDebug( __METHOD__."($key)\n" );
736 $handle = $this->getWriter();
737 if ( !$handle ) {
738 return false;
740 $ret = dba_delete( $key, $handle );
741 dba_close( $handle );
742 wfProfileOut( __METHOD__ );
743 return $ret;
746 function add( $key, $value, $exptime = 0 ) {
747 wfProfileIn( __METHOD__ );
748 $blob = $this->encode( $value, $exptime );
749 $handle = $this->getWriter();
750 if ( !$handle ) {
751 return false;
753 $ret = dba_insert( $key, $blob, $handle );
754 # Insert failed, check to see if it failed due to an expired key
755 if ( !$ret ) {
756 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
757 if ( $expiry < time() ) {
758 # Yes expired, delete and try again
759 dba_delete( $key, $handle );
760 $ret = dba_insert( $key, $blob, $handle );
761 # This time if it failed then it will be handled by the caller like any other race
765 dba_close( $handle );
766 wfProfileOut( __METHOD__ );
767 return $ret;
770 function keys() {
771 $reader = $this->getReader();
772 $k1 = dba_firstkey( $reader );
773 if( !$k1 ) {
774 return array();
776 $result[] = $k1;
777 while( $key = dba_nextkey( $reader ) ) {
778 $result[] = $key;
780 return $result;