53e947c24982fff044600dda76ad567b2f52bc72
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
{
199 function SqlBagOStuff($tablename = 'objectcache') {
200 $this->table
= $tablename;
204 /* expire old entries if any */
207 $res = $this->_query(
208 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
210 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
213 if($row=$this->_fetchobject($res)) {
214 $this->_debug("get: retrieved data; exp time is " . $row->exptime
);
215 return unserialize($row->value
);
217 $this->_debug('get: no matching rows');
222 function set($key,$value,$exptime=0) {
223 $exptime = intval($exptime);
224 if($exptime < 0) $exptime = 0;
226 $exp = $this->_maxdatetime();
228 if($exptime < 3600*24*30)
230 $exp = $this->_fromunixtime($exptime);
232 $this->delete( $key );
234 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
235 $key, serialize($value));
239 function delete($key,$time=0) {
241 "DELETE FROM $0 WHERE keyname='$1'", $key );
245 function _query($sql) {
246 $reps = func_get_args();
247 $reps[0] = $this->table
;
249 for($i=0;$i<count($reps);$i++
) {
252 $this->_strencode($reps[$i]),
255 $res = $this->_doquery($sql);
257 $this->_debug('query failed: ' . $this->_dberror($res));
262 function _strencode($str) {
263 /* Protect strings in SQL */
264 return str_replace( "'", "''", $str );
267 function _doquery($sql) {
268 die( 'abstract function SqlBagOStuff::_doquery() must be defined' );
271 function _fetchrow($res) {
272 die( 'abstract function SqlBagOStuff::_fetchrow() must be defined' );
275 function _freeresult($result) {
280 function _dberror($result) {
282 return 'unknown error';
285 function _maxdatetime() {
286 die( 'abstract function SqlBagOStuff::_maxdatetime() must be defined' );
289 function _fromunixtime() {
290 die( 'abstract function SqlBagOStuff::_fromunixtime() must be defined' );
293 function expireall() {
294 /* Remove any items that have expired */
295 $now=$this->_fromunixtime(time());
296 $this->_query( "DELETE FROM $0 WHERE exptime<'$now'" );
299 function deleteall() {
300 /* Clear *all* items from cache table */
301 $this->_query( "DELETE FROM $0" );
305 class MediaWikiBagOStuff
extends SqlBagOStuff
{
306 function _doquery($sql) {
307 $dbw =& wfGetDB( DB_MASTER
);
308 return $dbw->query($sql, 'MediaWikiBagOStuff:_doquery');
310 function _fetchobject($result) {
311 $dbw =& wfGetDB( DB_MASTER
);
312 return $dbw->fetchObject($result);
314 function _freeresult($result) {
315 $dbw =& wfGetDB( DB_MASTER
);
316 return $dbw->freeResult($result);
318 function _dberror($result) {
319 $dbw =& wfGetDB( DB_MASTER
);
320 return $dbw->lastError();
322 function _maxdatetime() {
323 return '9999-12-31 12:59:59';
325 function _fromunixtime($ts) {
326 return gmdate( 'Y-m-d H:i:s', $ts );
328 function _strencode($s) {
329 $dbw =& wfGetDB( DB_MASTER
);
330 return $dbw->strencode($s);
334 class TurckBagOStuff
extends BagOStuff
{
336 return mmcache_get( $key );
339 function set($key, $value, $exptime=0) {
340 mmcache_put( $key, $value, $exptime );
344 function delete($key, $time=0) {
349 function lock($key, $waitTimeout = 0 ) {
350 mmcache_lock( $key );
354 function unlock($key) {
355 mmcache_unlock( $key );