* abstraction layer fixups
[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 return true;
77 }
78 }
79
80 function add_multi($hash, $exptime=0) {
81 foreach($hash as $key => $value)
82 $this->add($key, $value, $exptime);
83 }
84
85 function delete_multi($keys, $time=0) {
86 foreach($keys as $key)
87 $this->delete($key, $time);
88 }
89
90 function replace($key, $value, $exptime=0) {
91 if( $this->get($key) !== false )
92 $this->set($key, $value, $exptime);
93 }
94
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;
103 }
104 }
105
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;
116 }
117 }
118
119 function _debug($text) {
120 if($this->debugmode)
121 wfDebug("BagOStuff debug: $text\n");
122 }
123 }
124
125
126 /* Functional versions! */
127 class HashBagOStuff extends BagOStuff {
128 /*
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.
132 */
133 var $bag;
134
135 function HashBagOStuff() {
136 $this->bag = array();
137 }
138
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;
145 }
146
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];
153 }
154
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 );
159 }
160
161 function delete($key,$time=0) {
162 if(!$this->bag[$key])
163 return false;
164 unset($this->bag[$key]);
165 return true;
166 }
167 }
168
169 /*
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)
176 );
177 */
178 class /* abstract */ SqlBagOStuff extends BagOStuff {
179 var $table;
180 function SqlBagOStuff($tablename = "objectcache") {
181 $this->table = $tablename;
182 }
183
184 function get($key) {
185 /* expire old entries if any */
186 $this->expireall();
187
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;
193 }
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");
199 }
200 return false;
201 }
202
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);
212 }
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; /* ? */
218 }
219
220 function delete($key,$time=0) {
221 $this->_query(
222 "DELETE FROM $0 WHERE keyname='$1'", $key );
223 return true; /* ? */
224 }
225
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);
235 }
236 $res = $this->_doquery($sql);
237 if($res == false) {
238 $this->_debug("query failed: " . $this->_dberror($res));
239 }
240 return $res;
241 }
242
243 function _strencode($str) {
244 /* Protect strings in SQL */
245 return str_replace( "'", "''", $str );
246 }
247
248 function _doquery($sql) {
249 die( "abstract function SqlBagOStuff::_doquery() must be defined" );
250 }
251
252 function _fetchrow($res) {
253 die( "abstract function SqlBagOStuff::_fetchrow() must be defined" );
254 }
255
256 function _freeresult($result) {
257 /* stub */
258 return false;
259 }
260
261 function _dberror($result) {
262 /* stub */
263 return "unknown error";
264 }
265
266 function _maxdatetime() {
267 die( "abstract function SqlBagOStuff::_maxdatetime() must be defined" );
268 }
269
270 function _fromunixtime() {
271 die( "abstract function SqlBagOStuff::_fromunixtime() must be defined" );
272 }
273
274 function expireall() {
275 /* Remove any items that have expired */
276 $now=$this->_fromunixtime(time());
277 $this->_query( "DELETE FROM $0 WHERE exptime<'$now'" );
278 }
279
280 function deleteall() {
281 /* Clear *all* items from cache table */
282 $this->_query( "DELETE FROM $0" );
283 }
284 }
285
286 class MediaWikiBagOStuff extends SqlBagOStuff {
287 function _doquery($sql) {
288 return wfQuery($sql, DB_READ, "MediaWikiBagOStuff:_doquery");
289 }
290 function _fetchobject($result) {
291 return wfFetchObject($result);
292 }
293 function _freeresult($result) {
294 return wfFreeResult($result);
295 }
296 function _dberror($result) {
297 return wfLastError();
298 }
299 function _maxdatetime() {
300 return "9999-12-31 12:59:59";
301 }
302 function _fromunixtime($ts) {
303 return gmdate( "Y-m-d H:i:s", $ts );
304 }
305 function _strencode($s) {
306 return wfStrEncode($s);
307 }
308 }
309
310 ?>