category redirect bug fix
[mediawiki.git] / includes / ObjectCache.php
blob18705d08d905d83d3ae91fd0c932b97aaa851fb2
1 <?php
2 # $Id$
4 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
5 # http://www.mediawiki.org/
6 #
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 {
31 var $debugmode;
33 function BagOStuff() {
34 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 */
44 function get($key) {
45 /* stub */
46 return false;
49 function set($key, $value, $exptime=0) {
50 /* stub */
51 return false;
54 function delete($key, $time=0) {
55 /* stub */
56 return false;
59 /* *** Emulated functions *** */
60 /* Better performance can likely be got with custom written versions */
61 function get_multi($keys) {
62 $out = array();
63 foreach($keys as $key)
64 $out[$key] = $this->get($key);
65 return $out;
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);
76 return true;
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?
100 return $n+$value;
101 } else {
102 return false;
106 function decr($key, $value=1) {
107 $value = intval($value);
108 if($value < 0) $value = 0;
109 if( ($n = $this->get($key)) !== false ) {
110 $m = $n - $value;
111 if($m < 0) $m = 0;
112 $this->set($key, $m); // exptime?
113 return $m;
114 } else {
115 return false;
119 function _debug($text) {
120 if($this->debugmode)
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.
133 var $bag;
135 function HashBagOStuff() {
136 $this->bag = array();
139 function _expire($key) {
140 $et = $this->bag[$key][1];
141 if(($et == 0) || ($et > time()))
142 return false;
143 $this->delete($key);
144 return true;
147 function get($key) {
148 if(!$this->bag[$key])
149 return false;
150 if($this->_expire($key))
151 return false;
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])
163 return false;
164 unset($this->bag[$key]);
165 return true;
170 CREATE TABLE objectcache (
171 keyname char(255) binary not null default '',
172 value mediumblob,
173 exptime datetime,
174 unique key (keyname),
175 key (exptime)
178 class /* abstract */ SqlBagOStuff extends BagOStuff {
179 var $table;
180 function SqlBagOStuff($tablename = "objectcache") {
181 $this->table = $tablename;
184 function get($key) {
185 /* expire old entries if any */
186 $this->expireall();
188 $res = $this->_query(
189 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
190 if(!$res) {
191 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
192 return false;
194 if($row=$this->_fetchobject($res)) {
195 $this->_debug("get: retrieved data; exp time is " . $row->exptime);
196 return unserialize($row->value);
197 } else {
198 $this->_debug("get: no matching rows");
200 return false;
203 function set($key,$value,$exptime=0) {
204 $exptime = intval($exptime);
205 if($exptime < 0) $exptime = 0;
206 if($exptime == 0) {
207 $exp = $this->_maxdatetime();
208 } else {
209 if($exptime < 3600*24*30)
210 $exptime += time();
211 $exp = $this->_fromunixtime($exptime);
213 $this->delete( $key );
214 $this->_query(
215 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
216 $key, serialize($value));
217 return true; /* ? */
220 function delete($key,$time=0) {
221 $this->_query(
222 "DELETE FROM $0 WHERE keyname='$1'", $key );
223 return true; /* ? */
226 function _query($sql) {
227 $reps = func_get_args();
228 $reps[0] = $this->table;
229 // ewwww
230 for($i=0;$i<count($reps);$i++) {
231 $sql = str_replace(
232 "$" . $i,
233 $this->_strencode($reps[$i]),
234 $sql);
236 $res = $this->_doquery($sql);
237 if($res == false) {
238 $this->_debug("query failed: " . $this->_dberror($res));
240 return $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) {
257 /* stub */
258 return false;
261 function _dberror($result) {
262 /* stub */
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_MASTER );
289 return $dbw->query($sql, "MediaWikiBagOStuff:_doquery");
291 function _fetchobject($result) {
292 $dbw =& wfGetDB( DB_MASTER );
293 return $dbw->fetchObject($result);
295 function _freeresult($result) {
296 $dbw =& wfGetDB( DB_MASTER );
297 return $dbw->freeResult($result);
299 function _dberror($result) {
300 $dbw =& wfGetDB( DB_MASTER );
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_MASTER );
311 return $dbw->strencode($s);