4 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
5 # http://www.mediawiki.org/
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
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
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 */
58 function set($key, $value, $exptime=0) {
63 function delete($key, $time=0) {
68 function lock($key, $timeout = 0) {
73 function unlock($key) {
78 /* *** Emulated functions *** */
79 /* Better performance can likely be got with custom written versions */
80 function get_multi($keys) {
82 foreach($keys as $key)
83 $out[$key] = $this->get($key);
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);
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) ) {
118 $value = intval($value);
119 if($value < 0) $value = 0;
122 if( ($n = $this->get($key)) !== false ) {
124 $this->set($key, $n); // exptime?
130 function decr($key, $value=1) {
131 if ( !$this->lock($key) ) {
134 $value = intval($value);
135 if($value < 0) $value = 0;
138 if( ($n = $this->get($key)) !== false ) {
141 $this->set($key, $m); // exptime?
147 function _debug($text) {
149 wfDebug("BagOStuff debug: $text\n");
155 * Functional versions!
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.
167 function HashBagOStuff() {
168 $this->bag
= array();
171 function _expire($key) {
172 $et = $this->bag
[$key][1];
173 if(($et == 0) ||
($et > time()))
180 if(!$this->bag
[$key])
182 if($this->_expire($key))
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])
196 unset($this->bag
[$key]);
202 CREATE TABLE objectcache (
203 keyname char(255) binary not null default '',
206 unique key (keyname),
216 class SqlBagOStuff
extends BagOStuff
{
219 function SqlBagOStuff($tablename = 'objectcache') {
220 $this->table
= $tablename;
224 /* expire old entries if any */
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 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 );
254 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
255 $key, serialize($value));
259 function delete($key,$time=0) {
261 "DELETE FROM $0 WHERE keyname='$1'", $key );
265 function getTableName() {
269 function _query($sql) {
270 $reps = func_get_args();
271 $reps[0] = $this->getTableName();
273 for($i=0;$i<count($reps);$i++
) {
276 $this->_strencode($reps[$i]),
279 $res = $this->_doquery($sql);
281 $this->_debug('query failed: ' . $this->_dberror($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) {
304 function _dberror($result) {
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" );
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;
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.
387 class TurckBagOStuff
extends BagOStuff
{
389 $val = mmcache_get( $key );
390 if ( is_string( $val ) ) {
391 $val = unserialize( $val );
396 function set($key, $value, $exptime=0) {
397 mmcache_put( $key, serialize( $value ), $exptime );
401 function delete($key, $time=0) {
406 function lock($key, $waitTimeout = 0 ) {
407 mmcache_lock( $key );
411 function unlock($key) {
412 mmcache_unlock( $key );