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
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
38 abstract class BagOStuff
{
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 */
57 function set($key, $value, $exptime=0) {
62 function delete($key, $time=0) {
67 function lock($key, $timeout = 0) {
72 function unlock($key) {
77 /* *** Emulated functions *** */
78 /* Better performance can likely be got with custom written versions */
79 function get_multi($keys) {
81 foreach($keys as $key)
82 $out[$key] = $this->get($key);
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);
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) ) {
117 $value = intval($value);
118 if($value < 0) $value = 0;
121 if( ($n = $this->get($key)) !== false ) {
123 $this->set($key, $n); // exptime?
129 function decr($key, $value=1) {
130 if ( !$this->lock($key) ) {
133 $value = intval($value);
134 if($value < 0) $value = 0;
137 if( ($n = $this->get($key)) !== false ) {
140 $this->set($key, $m); // exptime?
146 function _debug($text) {
148 wfDebug("BagOStuff debug: $text\n");
154 * Functional versions!
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.
166 function HashBagOStuff() {
167 $this->bag
= array();
170 function _expire($key) {
171 $et = $this->bag
[$key][1];
172 if(($et == 0) ||
($et > time()))
179 if(!$this->bag
[$key])
181 if($this->_expire($key))
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])
195 unset($this->bag
[$key]);
201 CREATE TABLE objectcache (
202 keyname char(255) binary not null default '',
205 unique key (keyname),
215 abstract class SqlBagOStuff
extends BagOStuff
{
217 var $lastexpireall = 0;
219 function SqlBagOStuff($tablename = 'objectcache') {
220 $this->table
= $tablename;
224 /* expire old entries if any */
225 $this->garbageCollect();
227 $res = $this->_query(
228 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
230 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
233 if($row=$this->_fetchobject($res)) {
234 $this->_debug("get: retrieved data; exp time is " . $row->exptime
);
235 return $this->_unserialize($row->value
);
237 $this->_debug('get: no matching rows');
242 function set($key,$value,$exptime=0) {
243 $exptime = intval($exptime);
244 if($exptime < 0) $exptime = 0;
246 $exp = $this->_maxdatetime();
248 if($exptime < 3600*24*30)
250 $exp = $this->_fromunixtime($exptime);
252 $this->delete( $key );
253 $this->_doinsert($this->getTableName(), array(
255 'value' => $this->_blobencode($this->_serialize($value)),
261 function delete($key,$time=0) {
263 "DELETE FROM $0 WHERE keyname='$1'", $key );
267 function getTableName() {
271 function _query($sql) {
272 $reps = func_get_args();
273 $reps[0] = $this->getTableName();
275 for($i=0;$i<count($reps);$i++
) {
278 $i > 0 ?
$this->_strencode($reps[$i]) : $reps[$i],
281 $res = $this->_doquery($sql);
283 $this->_debug('query failed: ' . $this->_dberror($res));
288 function _strencode($str) {
289 /* Protect strings in SQL */
290 return str_replace( "'", "''", $str );
292 function _blobencode($str) {
296 abstract function _doinsert($table, $vals);
297 abstract function _doquery($sql);
299 function _freeresult($result) {
304 function _dberror($result) {
306 return 'unknown error';
309 abstract function _maxdatetime();
310 abstract function _fromunixtime($ts);
312 function garbageCollect() {
313 /* Ignore 99% of requests */
314 if ( !mt_rand( 0, 100 ) ) {
316 /* Avoid repeating the delete within a few seconds */
317 if ( $nowtime > ($this->lastexpireall +
1) ) {
318 $this->lastexpireall
= $nowtime;
324 function expireall() {
325 /* Remove any items that have expired */
326 $now = $this->_fromunixtime( time() );
327 $this->_query( "DELETE FROM $0 WHERE exptime < '$now'" );
330 function deleteall() {
331 /* Clear *all* items from cache table */
332 $this->_query( "DELETE FROM $0" );
336 * Serialize an object and, if possible, compress the representation.
337 * On typical message and page data, this can provide a 3X decrease
338 * in storage requirements.
343 function _serialize( &$data ) {
344 $serial = serialize( $data );
345 if( function_exists( 'gzdeflate' ) ) {
346 return gzdeflate( $serial );
353 * Unserialize and, if necessary, decompress an object.
354 * @param string $serial
357 function _unserialize( $serial ) {
358 if( function_exists( 'gzinflate' ) ) {
359 $decomp = @gzinflate
( $serial );
360 if( false !== $decomp ) {
364 $ret = unserialize( $serial );
373 class MediaWikiBagOStuff
extends SqlBagOStuff
{
374 var $tableInitialised = false;
376 function _doquery($sql) {
377 $dbw =& wfGetDB( DB_MASTER
);
378 return $dbw->query($sql, 'MediaWikiBagOStuff::_doquery');
380 function _doinsert($t, $v) {
381 $dbw =& wfGetDB( DB_MASTER
);
382 return $dbw->insert($t, $v, 'MediaWikiBagOStuff::_doinsert');
384 function _fetchobject($result) {
385 $dbw =& wfGetDB( DB_MASTER
);
386 return $dbw->fetchObject($result);
388 function _freeresult($result) {
389 $dbw =& wfGetDB( DB_MASTER
);
390 return $dbw->freeResult($result);
392 function _dberror($result) {
393 $dbw =& wfGetDB( DB_MASTER
);
394 return $dbw->lastError();
396 function _maxdatetime() {
397 $dbw =& wfGetDB(DB_MASTER
);
398 return $dbw->timestamp('9999-12-31 12:59:59');
400 function _fromunixtime($ts) {
401 $dbw =& wfGetDB(DB_MASTER
);
402 return $dbw->timestamp($ts);
404 function _strencode($s) {
405 $dbw =& wfGetDB( DB_MASTER
);
406 return $dbw->strencode($s);
408 function _blobencode($s) {
409 $dbw =& wfGetDB( DB_MASTER
);
410 return $dbw->encodeBlob($s);
412 function getTableName() {
413 if ( !$this->tableInitialised
) {
414 $dbw =& wfGetDB( DB_MASTER
);
415 /* This is actually a hack, we should be able
416 to use Language classes here... or not */
418 throw new MWException("Could not connect to database");
419 $this->table
= $dbw->tableName( $this->table
);
420 $this->tableInitialised
= true;
427 * This is a wrapper for Turck MMCache's shared memory functions.
429 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
430 * to use a weird custom serializer that randomly segfaults. So we wrap calls
431 * with serialize()/unserialize().
433 * The thing I noticed about the Turck serialized data was that unlike ordinary
434 * serialize(), it contained the names of methods, and judging by the amount of
435 * binary data, perhaps even the bytecode of the methods themselves. It may be
436 * that Turck's serializer is faster, so a possible future extension would be
437 * to use it for arrays but not for objects.
441 class TurckBagOStuff
extends BagOStuff
{
443 $val = mmcache_get( $key );
444 if ( is_string( $val ) ) {
445 $val = unserialize( $val );
450 function set($key, $value, $exptime=0) {
451 mmcache_put( $key, serialize( $value ), $exptime );
455 function delete($key, $time=0) {
460 function lock($key, $waitTimeout = 0 ) {
461 mmcache_lock( $key );
465 function unlock($key) {
466 mmcache_unlock( $key );
472 * This is a wrapper for APC's shared memory functions
477 class APCBagOStuff
extends BagOStuff
{
479 $val = apc_fetch($key);
483 function set($key, $value, $exptime=0) {
484 apc_store($key, $value, $exptime);
488 function delete($key) {
496 * This is a wrapper for eAccelerator's shared memory functions.
498 * This is basically identical to the Turck MMCache version,
499 * mostly because eAccelerator is based on Turck MMCache.
503 class eAccelBagOStuff
extends BagOStuff
{
505 $val = eaccelerator_get( $key );
506 if ( is_string( $val ) ) {
507 $val = unserialize( $val );
512 function set($key, $value, $exptime=0) {
513 eaccelerator_put( $key, serialize( $value ), $exptime );
517 function delete($key, $time=0) {
518 eaccelerator_rm( $key );
522 function lock($key, $waitTimeout = 0 ) {
523 eaccelerator_lock( $key );
527 function unlock($key) {
528 eaccelerator_unlock( $key );