30c5e0b536829c13a7cff63fb6fa4041ce23e7c6
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);
78 function add_multi($hash, $exptime=0) {
79 foreach($hash as $key => $value)
80 $this->add($key, $value, $exptime);
83 function delete_multi($keys, $time=0) {
84 foreach($keys as $key)
85 $this->delete($key, $time);
88 function replace($key, $value, $exptime=0) {
89 if( $this->get($key) !== false )
90 $this->set($key, $value, $exptime);
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?
104 function decr($key, $value=1) {
105 $value = intval($value);
106 if($value < 0) $value = 0;
107 if( ($n = $this->get($key)) !== false ) {
110 $this->set($key, $m); // exptime?
117 function _debug($text) {
119 wfDebug("BagOStuff debug: $text\n");
124 /* Functional versions! */
125 class HashBagOStuff
extends BagOStuff
{
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.
133 function HashBagOStuff() {
134 $this->bag
= array();
137 function _expire($key) {
138 $et = $this->bag
[$key][1];
139 if(($et == 0) ||
($et > time()))
146 if(!$this->bag
[$key])
148 if($this->_expire($key))
150 return $this->bag
[$key][0];
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 );
159 function delete($key,$time=0) {
160 if(!$this->bag
[$key])
162 unset($this->bag
[$key]);
168 CREATE TABLE objectcache (
169 keyname char(255) binary not null default '',
172 unique key (keyname),
176 class /* abstract */ SqlBagOStuff
extends BagOStuff
{
178 function SqlBagOStuff($tablename = "objectcache") {
179 $this->table
= $tablename;
183 /* expire old entries if any */
186 $res = $this->_query(
187 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
189 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
192 if($arr = $this->_fetchrow($res)) {
193 $this->_debug("get: retrieved data; exp time is " . $arr['exptime']);
194 return unserialize($arr['value']);
196 $this->_debug("get: no matching rows");
201 function set($key,$value,$exptime=0) {
202 $exptime = intval($exptime);
203 if($exptime < 0) $exptime = 0;
205 $exp = $this->_maxdatetime();
207 if($exptime < 3600*24*30)
209 $exp = $this->_fromunixtime($exptime);
211 $this->delete( $key );
213 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
214 $key, serialize(&$value));
218 function delete($key,$time=0) {
220 "DELETE FROM $0 WHERE keyname='$1'", $key );
224 function _query($sql) {
225 $reps = func_get_args();
226 $reps[0] = $this->table
;
228 for($i=0;$i<count($reps);$i++
) {
231 $this->_strencode($reps[$i]),
234 $res = $this->_doquery($sql);
236 $this->_debug("query failed: " . $this->_dberror($res));
241 function _strencode($str) {
242 /* Protect strings in SQL */
243 return str_replace( "'", "''", $str );
246 function _doquery($sql) {
247 die( "abstract function SqlBagOStuff::_doquery() must be defined" );
250 function _fetchrow($res) {
251 die( "abstract function SqlBagOStuff::_fetchrow() must be defined" );
254 function _freeresult($result) {
259 function _dberror($result) {
261 return "unknown error";
264 function _maxdatetime() {
265 die( "abstract function SqlBagOStuff::_maxdatetime() must be defined" );
268 function _fromunixtime() {
269 die( "abstract function SqlBagOStuff::_fromunixtime() must be defined" );
272 function expireall() {
273 /* Remove any items that have expired */
274 $this->_query( "DELETE FROM $0 WHERE exptime<=NOW()" );
277 function deleteall() {
278 /* Clear *all* items from cache table */
279 $this->_query( "DELETE FROM $0" );
283 class MediaWikiBagOStuff
extends SqlBagOStuff
{
284 function _doquery($sql) {
285 return wfQuery($sql, DB_READ
, "MediaWikiBagOStuff:_doquery");
287 function _fetchrow($result) {
288 return wfFetchRow($result);
290 function _freeresult($result) {
291 return wfFreeResult($result);
293 function _dberror($result) {
294 return wfLastError();
296 function _maxdatetime() {
297 return "9999-12-31 12:59:59";
299 function _fromunixtime($ts) {
300 return gmdate( "Y-m-d H:i:s", $ts );
302 function _strencode($s) {
303 return wfStrEncode($s);