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
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
40 function BagOStuff() {
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 */
56 function set($key, $value, $exptime=0) {
61 function delete($key, $time=0) {
66 function lock($key, $timeout = 0) {
71 function unlock($key) {
76 /* *** Emulated functions *** */
77 /* Better performance can likely be got with custom written versions */
78 function get_multi($keys) {
80 foreach($keys as $key)
81 $out[$key] = $this->get($key);
85 function set_multi($hash, $exptime=0) {
86 foreach($hash as $key => $value)
87 $this->set($key, $value, $exptime);
90 function add($key, $value, $exptime=0) {
91 if( $this->get($key) == false ) {
92 $this->set($key, $value, $exptime);
97 function add_multi($hash, $exptime=0) {
98 foreach($hash as $key => $value)
99 $this->add($key, $value, $exptime);
102 function delete_multi($keys, $time=0) {
103 foreach($keys as $key)
104 $this->delete($key, $time);
107 function replace($key, $value, $exptime=0) {
108 if( $this->get($key) !== false )
109 $this->set($key, $value, $exptime);
112 function incr($key, $value=1) {
113 if ( !$this->lock($key) ) {
116 $value = intval($value);
117 if($value < 0) $value = 0;
120 if( ($n = $this->get($key)) !== false ) {
122 $this->set($key, $n); // exptime?
128 function decr($key, $value=1) {
129 if ( !$this->lock($key) ) {
132 $value = intval($value);
133 if($value < 0) $value = 0;
136 if( ($n = $this->get($key)) !== false ) {
139 $this->set($key, $m); // exptime?
145 function _debug($text) {
147 wfDebug("BagOStuff debug: $text\n");
153 * Functional versions!
156 class HashBagOStuff
extends BagOStuff
{
158 This is a test of the interface, mainly. It stores
159 things in an associative array, which is not going to
160 persist between program runs.
164 function HashBagOStuff() {
165 $this->bag
= array();
168 function _expire($key) {
169 $et = $this->bag
[$key][1];
170 if(($et == 0) ||
($et > time()))
177 if(!$this->bag
[$key])
179 if($this->_expire($key))
181 return $this->bag
[$key][0];
184 function set($key,$value,$exptime=0) {
185 if(($exptime != 0) && ($exptime < 3600*24*30))
186 $exptime = time() +
$exptime;
187 $this->bag
[$key] = array( $value, $exptime );
190 function delete($key,$time=0) {
191 if(!$this->bag
[$key])
193 unset($this->bag
[$key]);
199 CREATE TABLE objectcache (
200 keyname char(255) binary not null default '',
203 unique key (keyname),
212 class SqlBagOStuff
extends BagOStuff
{
215 function SqlBagOStuff($tablename = 'objectcache') {
216 $this->table
= $tablename;
220 /* expire old entries if any */
223 $res = $this->_query(
224 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
226 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
229 if($row=$this->_fetchobject($res)) {
230 $this->_debug("get: retrieved data; exp time is " . $row->exptime
);
231 return unserialize($row->value
);
233 $this->_debug('get: no matching rows');
238 function set($key,$value,$exptime=0) {
239 $exptime = intval($exptime);
240 if($exptime < 0) $exptime = 0;
242 $exp = $this->_maxdatetime();
244 if($exptime < 3600*24*30)
246 $exp = $this->_fromunixtime($exptime);
248 $this->delete( $key );
250 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
251 $key, serialize($value));
255 function delete($key,$time=0) {
257 "DELETE FROM $0 WHERE keyname='$1'", $key );
261 function getTableName() {
265 function _query($sql) {
266 $reps = func_get_args();
267 $reps[0] = $this->getTableName();
269 for($i=0;$i<count($reps);$i++
) {
272 $this->_strencode($reps[$i]),
275 $res = $this->_doquery($sql);
277 $this->_debug('query failed: ' . $this->_dberror($res));
282 function _strencode($str) {
283 /* Protect strings in SQL */
284 return str_replace( "'", "''", $str );
287 function _doquery($sql) {
288 die( 'abstract function SqlBagOStuff::_doquery() must be defined' );
291 function _fetchrow($res) {
292 die( 'abstract function SqlBagOStuff::_fetchrow() must be defined' );
295 function _freeresult($result) {
300 function _dberror($result) {
302 return 'unknown error';
305 function _maxdatetime() {
306 die( 'abstract function SqlBagOStuff::_maxdatetime() must be defined' );
309 function _fromunixtime() {
310 die( 'abstract function SqlBagOStuff::_fromunixtime() must be defined' );
313 function expireall() {
314 /* Remove any items that have expired */
315 $now=$this->_fromunixtime(time());
316 $this->_query( "DELETE FROM $0 WHERE exptime<'$now'" );
319 function deleteall() {
320 /* Clear *all* items from cache table */
321 $this->_query( "DELETE FROM $0" );
328 class MediaWikiBagOStuff
extends SqlBagOStuff
{
329 var $tableInitialised = false;
331 function _doquery($sql) {
332 $dbw =& wfGetDB( DB_MASTER
);
333 return $dbw->query($sql, 'MediaWikiBagOStuff:_doquery');
335 function _fetchobject($result) {
336 $dbw =& wfGetDB( DB_MASTER
);
337 return $dbw->fetchObject($result);
339 function _freeresult($result) {
340 $dbw =& wfGetDB( DB_MASTER
);
341 return $dbw->freeResult($result);
343 function _dberror($result) {
344 $dbw =& wfGetDB( DB_MASTER
);
345 return $dbw->lastError();
347 function _maxdatetime() {
348 return '9999-12-31 12:59:59';
350 function _fromunixtime($ts) {
351 return gmdate( 'Y-m-d H:i:s', $ts );
353 function _strencode($s) {
354 $dbw =& wfGetDB( DB_MASTER
);
355 return $dbw->strencode($s);
357 function getTableName() {
358 if ( !$this->tableInitialised
) {
359 $dbw =& wfGetDB( DB_MASTER
);
360 $this->table
= $dbw->tableName( $this->table
);
361 $this->tableInitialised
= true;
370 class TurckBagOStuff
extends BagOStuff
{
372 return mmcache_get( $key );
375 function set($key, $value, $exptime=0) {
376 mmcache_put( $key, $value, $exptime );
380 function delete($key, $time=0) {
385 function lock($key, $waitTimeout = 0 ) {
386 mmcache_lock( $key );
390 function unlock($key) {
391 mmcache_unlock( $key );