Use DatabaseFunctions layer instead of copy-paste one :)
[lhc/web/wiklou.git] / includes / ObjectCache.php
1 <?php
2 # $Id$
3 #
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.
11 #
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.
16 #
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
21
22 # Simple generic object store
23 # interface is intended to be more or less compatible with
24 # the PHP memcached client.
25 #
26 # backends for local hash array and SQL table included:
27 # $bag = new HashBagOStuff();
28 # $bag = new MysqlBagOStuff($tablename); # connect to db first
29
30 class /* abstract */ BagOStuff {
31 var $debugmode;
32
33 function BagOStuff() {
34 set_debug( false );
35 }
36
37 function set_debug($bool) {
38 $this->debugmode = $bool;
39 }
40
41 /* *** THE GUTS OF THE OPERATION *** */
42 /* Override these with functional things in subclasses */
43
44 function get($key) {
45 /* stub */
46 return false;
47 }
48
49 function set($key, $value, $exptime=0) {
50 /* stub */
51 return false;
52 }
53
54 function delete($key, $time=0) {
55 /* stub */
56 return false;
57 }
58
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;
66 }
67
68 function set_multi($hash, $exptime=0) {
69 foreach($hash as $key => $value)
70 $this->set($key, $value, $exptime);
71 }
72
73 function add($key, $value, $exptime=0) {
74 if( $this->get($key) === false )
75 $this->set($key, $value, $exptime);
76 }
77
78 function add_multi($hash, $exptime=0) {
79 foreach($hash as $key => $value)
80 $this->add($key, $value, $exptime);
81 }
82
83 function delete_multi($keys, $time=0) {
84 foreach($keys as $key)
85 $this->delete($key, $time);
86 }
87
88 function replace($key, $value, $exptime=0) {
89 if( $this->get($key) !== false )
90 $this->set($key, $value, $exptime);
91 }
92
93 function incr($key, $value=1) {
94 $value = intval($value);
95 if($value < 0) $value = 0;
96 if( ($n = $this->get($key)) !== false ) {
97 $this->set($key, $n+$value); // exptime?
98 return $n+$value;
99 } else {
100 return false;
101 }
102 }
103
104 function decr($key, $value=1) {
105 $value = intval($value);
106 if($value < 0) $value = 0;
107 if( ($n = $this->get($key)) !== false ) {
108 $m = $n - $value;
109 if($m < 0) $m = 0;
110 $this->set($key, $m); // exptime?
111 return $m;
112 } else {
113 return false;
114 }
115 }
116
117 function _debug($text) {
118 if($this->debugmode)
119 wfDebug("BagOStuff debug: $text\n");
120 }
121 }
122
123
124 /* Functional versions! */
125 class HashBagOStuff extends BagOStuff {
126 /*
127 This is a test of the interface, mainly. It stores
128 things in an associative array, which is not going to
129 persist between program runs.
130 */
131 var $bag;
132
133 function HashBagOStuff() {
134 $this->bag = array();
135 }
136
137 function _expire($key) {
138 $et = $this->bag[$key][1];
139 if(($et == 0) || ($et > time()))
140 return false;
141 $this->delete($key);
142 return true;
143 }
144
145 function get($key) {
146 if(!$this->bag[$key])
147 return false;
148 if($this->_expire($key))
149 return false;
150 return $this->bag[$key][0];
151 }
152
153 function set($key,$value,$exptime=0) {
154 if(($exptime != 0) && ($exptime < 3600*24*30))
155 $exptime = time() + $exptime;
156 $this->bag[$key] = array( $value, $exptime );
157 }
158
159 function delete($key,$time=0) {
160 if(!$this->bag[$key])
161 return false;
162 unset($this->bag[$key]);
163 return true;
164 }
165 }
166
167 /*
168 CREATE TABLE objectcache (
169 keyname char(255) binary not null default '',
170 value mediumblob,
171 exptime datetime,
172 unique key (keyname),
173 key (exptime)
174 );
175 */
176 class /* abstract */ SqlBagOStuff extends BagOStuff {
177 var $table;
178 function SqlBagOStuff($tablename = "objectcache") {
179 $this->table = $tablename;
180 }
181
182 function get($key) {
183 /* expire old entries if any */
184 $this->expireall();
185
186 $res = $this->_query(
187 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
188 if(!$res) {
189 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
190 return false;
191 }
192 if($arr = $this->_fetchrow($res)) {
193 $this->_debug("get: retrieved data; exp time is " . $arr['exptime']);
194 return unserialize($arr['value']);
195 } else {
196 $this->_debug("get: no matching rows");
197 }
198 return false;
199 }
200
201 function set($key,$value,$exptime=0) {
202 $exptime = intval($exptime);
203 if($exptime < 0) $exptime = 0;
204 if($exptime == 0) {
205 $exp = $this->_maxdatetime();
206 } else {
207 if($exptime < 3600*24*30)
208 $exptime += time();
209 $exp = $this->_fromunixtime($exptime);
210 }
211 $this->delete( $key );
212 $this->_query(
213 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
214 $key, serialize(&$value));
215 return true; /* ? */
216 }
217
218 function delete($key,$time=0) {
219 $this->_query(
220 "DELETE FROM $0 WHERE keyname='$1'", $key );
221 return true; /* ? */
222 }
223
224 function _query($sql) {
225 $reps = func_get_args();
226 $reps[0] = $this->table;
227 // ewwww
228 for($i=0;$i<count($reps);$i++) {
229 $sql = str_replace(
230 "$" . $i,
231 $this->_strencode($reps[$i]),
232 $sql);
233 }
234 $res = $this->_doquery($sql);
235 if($res === false) {
236 $this->_debug("query failed: " . $this->_dberror($res));
237 }
238 return $res;
239 }
240
241 function _strencode($str) {
242 /* Protect strings in SQL */
243 return str_replace( "'", "''", $str );
244 }
245
246 function _doquery($sql) {
247 die( "abstract function SqlBagOStuff::_doquery() must be defined" );
248 }
249
250 function _fetchrow($res) {
251 die( "abstract function SqlBagOStuff::_fetchrow() must be defined" );
252 }
253
254 function _freeresult($result) {
255 /* stub */
256 return false;
257 }
258
259 function _dberror($result) {
260 /* stub */
261 return "unknown error";
262 }
263
264 function _maxdatetime() {
265 die( "abstract function SqlBagOStuff::_maxdatetime() must be defined" );
266 }
267
268 function _fromunixtime() {
269 die( "abstract function SqlBagOStuff::_fromunixtime() must be defined" );
270 }
271
272 function expireall() {
273 /* Remove any items that have expired */
274 $this->_query( "DELETE FROM $0 WHERE exptime<=NOW()" );
275 }
276
277 function deleteall() {
278 /* Clear *all* items from cache table */
279 $this->_query( "DELETE FROM $0" );
280 }
281 }
282
283 class MediaWikiBagOStuff extends SqlBagOStuff {
284 function _doquery($sql) {
285 return wfQuery($sql, DB_READ, "MediaWikiBagOStuff:_doquery");
286 }
287 function _fetchrow($result) {
288 return wfFetchRow($result);
289 }
290 function _freeresult($result) {
291 return wfFreeResult($result);
292 }
293 function _dberror($result) {
294 return wfLastError();
295 }
296 function _maxdatetime() {
297 return "9999-12-31 12:59:59";
298 }
299 function _fromunixtime($ts) {
300 return gmdate( "Y-m-d H:i:s", $ts );
301 }
302 function _strencode($s) {
303 return wfStrEncode($s);
304 }
305 }
306
307 ?>