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
22 # Simple generic object store
23 # interface is intended to be more or less compatible with
24 # the PHP memcached client.
26 # backends for local hash array and SQL table included:
27 # $bag = new HashBagOStuff();
28 # $bag = new MysqlBagOStuff($tablename); # connect to db first
30 class /* abstract */ BagOStuff
{
33 function BagOStuff() {
34 $this->set_debug( false );
37 function set_debug($bool) {
38 $this->debugmode
= $bool;
41 /* *** THE GUTS OF THE OPERATION *** */
42 /* Override these with functional things in subclasses */
49 function set($key, $value, $exptime=0) {
54 function delete($key, $time=0) {
59 function lock($key, $timeout = 0) {
64 function unlock($key) {
69 /* *** Emulated functions *** */
70 /* Better performance can likely be got with custom written versions */
71 function get_multi($keys) {
73 foreach($keys as $key)
74 $out[$key] = $this->get($key);
78 function set_multi($hash, $exptime=0) {
79 foreach($hash as $key => $value)
80 $this->set($key, $value, $exptime);
83 function add($key, $value, $exptime=0) {
84 if( $this->get($key) == false ) {
85 $this->set($key, $value, $exptime);
90 function add_multi($hash, $exptime=0) {
91 foreach($hash as $key => $value)
92 $this->add($key, $value, $exptime);
95 function delete_multi($keys, $time=0) {
96 foreach($keys as $key)
97 $this->delete($key, $time);
100 function replace($key, $value, $exptime=0) {
101 if( $this->get($key) !== false )
102 $this->set($key, $value, $exptime);
105 function incr($key, $value=1) {
106 if ( !$this->lock($key) ) {
109 $value = intval($value);
110 if($value < 0) $value = 0;
113 if( ($n = $this->get($key)) !== false ) {
115 $this->set($key, $n); // exptime?
121 function decr($key, $value=1) {
122 if ( !$this->lock($key) ) {
125 $value = intval($value);
126 if($value < 0) $value = 0;
129 if( ($n = $this->get($key)) !== false ) {
132 $this->set($key, $m); // exptime?
138 function _debug($text) {
140 wfDebug("BagOStuff debug: $text\n");
145 /* Functional versions! */
146 class HashBagOStuff
extends BagOStuff
{
148 This is a test of the interface, mainly. It stores
149 things in an associative array, which is not going to
150 persist between program runs.
154 function HashBagOStuff() {
155 $this->bag
= array();
158 function _expire($key) {
159 $et = $this->bag
[$key][1];
160 if(($et == 0) ||
($et > time()))
167 if(!$this->bag
[$key])
169 if($this->_expire($key))
171 return $this->bag
[$key][0];
174 function set($key,$value,$exptime=0) {
175 if(($exptime != 0) && ($exptime < 3600*24*30))
176 $exptime = time() +
$exptime;
177 $this->bag
[$key] = array( $value, $exptime );
180 function delete($key,$time=0) {
181 if(!$this->bag
[$key])
183 unset($this->bag
[$key]);
189 CREATE TABLE objectcache (
190 keyname char(255) binary not null default '',
193 unique key (keyname),
197 class /* abstract */ SqlBagOStuff
extends BagOStuff
{
200 function SqlBagOStuff($tablename = 'objectcache') {
201 $this->table
= $tablename;
205 /* expire old entries if any */
208 $res = $this->_query(
209 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
211 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
214 if($row=$this->_fetchobject($res)) {
215 $this->_debug("get: retrieved data; exp time is " . $row->exptime
);
216 return unserialize($row->value
);
218 $this->_debug('get: no matching rows');
223 function set($key,$value,$exptime=0) {
224 $exptime = intval($exptime);
225 if($exptime < 0) $exptime = 0;
227 $exp = $this->_maxdatetime();
229 if($exptime < 3600*24*30)
231 $exp = $this->_fromunixtime($exptime);
233 $this->delete( $key );
235 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
236 $key, serialize($value));
240 function delete($key,$time=0) {
242 "DELETE FROM $0 WHERE keyname='$1'", $key );
246 function getTableName() {
250 function _query($sql) {
251 $reps = func_get_args();
252 $reps[0] = $this->getTableName();
254 for($i=0;$i<count($reps);$i++
) {
257 $this->_strencode($reps[$i]),
260 $res = $this->_doquery($sql);
262 $this->_debug('query failed: ' . $this->_dberror($res));
267 function _strencode($str) {
268 /* Protect strings in SQL */
269 return str_replace( "'", "''", $str );
272 function _doquery($sql) {
273 die( 'abstract function SqlBagOStuff::_doquery() must be defined' );
276 function _fetchrow($res) {
277 die( 'abstract function SqlBagOStuff::_fetchrow() must be defined' );
280 function _freeresult($result) {
285 function _dberror($result) {
287 return 'unknown error';
290 function _maxdatetime() {
291 die( 'abstract function SqlBagOStuff::_maxdatetime() must be defined' );
294 function _fromunixtime() {
295 die( 'abstract function SqlBagOStuff::_fromunixtime() must be defined' );
298 function expireall() {
299 /* Remove any items that have expired */
300 $now=$this->_fromunixtime(time());
301 $this->_query( "DELETE FROM $0 WHERE exptime<'$now'" );
304 function deleteall() {
305 /* Clear *all* items from cache table */
306 $this->_query( "DELETE FROM $0" );
310 class MediaWikiBagOStuff
extends SqlBagOStuff
{
311 var $tableInitialised = false;
313 function _doquery($sql) {
314 $dbw =& wfGetDB( DB_MASTER
);
315 return $dbw->query($sql, 'MediaWikiBagOStuff:_doquery');
317 function _fetchobject($result) {
318 $dbw =& wfGetDB( DB_MASTER
);
319 return $dbw->fetchObject($result);
321 function _freeresult($result) {
322 $dbw =& wfGetDB( DB_MASTER
);
323 return $dbw->freeResult($result);
325 function _dberror($result) {
326 $dbw =& wfGetDB( DB_MASTER
);
327 return $dbw->lastError();
329 function _maxdatetime() {
330 return '9999-12-31 12:59:59';
332 function _fromunixtime($ts) {
333 return gmdate( 'Y-m-d H:i:s', $ts );
335 function _strencode($s) {
336 $dbw =& wfGetDB( DB_MASTER
);
337 return $dbw->strencode($s);
339 function getTableName() {
340 if ( !$this->tableInitialised
) {
341 $dbw =& wfGetDB( DB_MASTER
);
342 $this->table
= $dbw->tableName( $this->table
);
343 $this->tableInitialised
= true;
349 class TurckBagOStuff
extends BagOStuff
{
351 return mmcache_get( $key );
354 function set($key, $value, $exptime=0) {
355 mmcache_put( $key, $value, $exptime );
359 function delete($key, $time=0) {
364 function lock($key, $waitTimeout = 0 ) {
365 mmcache_lock( $key );
369 function unlock($key) {
370 mmcache_unlock( $key );