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() {
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 /* *** Emulated functions *** */
60 /* Better performance can likely be got with custom written versions */
61 function get_multi($keys) {
63 foreach($keys as $key)
64 $out[$key] = $this->get($key);
68 function set_multi($hash, $exptime=0) {
69 foreach($hash as $key => $value)
70 $this->set($key, $value, $exptime);
73 function add($key, $value, $exptime=0) {
74 if( $this->get($key) == false ) {
75 $this->set($key, $value, $exptime);
80 function add_multi($hash, $exptime=0) {
81 foreach($hash as $key => $value)
82 $this->add($key, $value, $exptime);
85 function delete_multi($keys, $time=0) {
86 foreach($keys as $key)
87 $this->delete($key, $time);
90 function replace($key, $value, $exptime=0) {
91 if( $this->get($key) !== false )
92 $this->set($key, $value, $exptime);
95 function incr($key, $value=1) {
96 $value = intval($value);
97 if($value < 0) $value = 0;
98 if( ($n = $this->get($key)) !== false ) {
99 $this->set($key, $n+
$value); // exptime?
106 function decr($key, $value=1) {
107 $value = intval($value);
108 if($value < 0) $value = 0;
109 if( ($n = $this->get($key)) !== false ) {
112 $this->set($key, $m); // exptime?
119 function _debug($text) {
121 wfDebug("BagOStuff debug: $text\n");
126 /* Functional versions! */
127 class HashBagOStuff
extends BagOStuff
{
129 This is a test of the interface, mainly. It stores
130 things in an associative array, which is not going to
131 persist between program runs.
135 function HashBagOStuff() {
136 $this->bag
= array();
139 function _expire($key) {
140 $et = $this->bag
[$key][1];
141 if(($et == 0) ||
($et > time()))
148 if(!$this->bag
[$key])
150 if($this->_expire($key))
152 return $this->bag
[$key][0];
155 function set($key,$value,$exptime=0) {
156 if(($exptime != 0) && ($exptime < 3600*24*30))
157 $exptime = time() +
$exptime;
158 $this->bag
[$key] = array( $value, $exptime );
161 function delete($key,$time=0) {
162 if(!$this->bag
[$key])
164 unset($this->bag
[$key]);
170 CREATE TABLE objectcache (
171 keyname char(255) binary not null default '',
174 unique key (keyname),
178 class /* abstract */ SqlBagOStuff
extends BagOStuff
{
180 function SqlBagOStuff($tablename = "objectcache") {
181 $this->table
= $tablename;
185 /* expire old entries if any */
188 $res = $this->_query(
189 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
191 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
194 if($row=$this->_fetchobject($res)) {
195 $this->_debug("get: retrieved data; exp time is " . $row->exptime
);
196 return unserialize($row->value
);
198 $this->_debug("get: no matching rows");
203 function set($key,$value,$exptime=0) {
204 $exptime = intval($exptime);
205 if($exptime < 0) $exptime = 0;
207 $exp = $this->_maxdatetime();
209 if($exptime < 3600*24*30)
211 $exp = $this->_fromunixtime($exptime);
213 $this->delete( $key );
215 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
216 $key, serialize($value));
220 function delete($key,$time=0) {
222 "DELETE FROM $0 WHERE keyname='$1'", $key );
226 function _query($sql) {
227 $reps = func_get_args();
228 $reps[0] = $this->table
;
230 for($i=0;$i<count($reps);$i++
) {
233 $this->_strencode($reps[$i]),
236 $res = $this->_doquery($sql);
238 $this->_debug("query failed: " . $this->_dberror($res));
243 function _strencode($str) {
244 /* Protect strings in SQL */
245 return str_replace( "'", "''", $str );
248 function _doquery($sql) {
249 die( "abstract function SqlBagOStuff::_doquery() must be defined" );
252 function _fetchrow($res) {
253 die( "abstract function SqlBagOStuff::_fetchrow() must be defined" );
256 function _freeresult($result) {
261 function _dberror($result) {
263 return "unknown error";
266 function _maxdatetime() {
267 die( "abstract function SqlBagOStuff::_maxdatetime() must be defined" );
270 function _fromunixtime() {
271 die( "abstract function SqlBagOStuff::_fromunixtime() must be defined" );
274 function expireall() {
275 /* Remove any items that have expired */
276 $now=$this->_fromunixtime(time());
277 $this->_query( "DELETE FROM $0 WHERE exptime<'$now'" );
280 function deleteall() {
281 /* Clear *all* items from cache table */
282 $this->_query( "DELETE FROM $0" );
286 class MediaWikiBagOStuff
extends SqlBagOStuff
{
287 function _doquery($sql) {
288 $dbw = wfGetDB( DB_WRITE
);
289 return $dbw->query($sql, "MediaWikiBagOStuff:_doquery");
291 function _fetchobject($result) {
292 $dbw = wfGetDB( DB_WRITE
);
293 return $dbw->fetchObject($result);
295 function _freeresult($result) {
296 $dbw = wfGetDB( DB_WRITE
);
297 return $dbw->freeResult($result);
299 function _dberror($result) {
300 $dbw = wfGetDB( DB_WRITE
);
301 return $dbw->lastError();
303 function _maxdatetime() {
304 return "9999-12-31 12:59:59";
306 function _fromunixtime($ts) {
307 return gmdate( "Y-m-d H:i:s", $ts );
309 function _strencode($s) {
310 $dbw = wfGetDB( DB_WRITE
);
311 return $dbw->strencode($s);