71469ab34511be3a9f413d339b01b610775349c5
[lhc/web/wiklou.git] / includes / objectcache / BagOStuff.php
1 <?php
2 /**
3 * Classes to cache objects in PHP accelerators, SQL database or DBA files
4 *
5 * Copyright © 2003-2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Cache
25 */
26
27 /**
28 * @defgroup Cache Cache
29 */
30
31 /**
32 * interface is intended to be more or less compatible with
33 * the PHP memcached client.
34 *
35 * backends for local hash array and SQL table included:
36 * <code>
37 * $bag = new HashBagOStuff();
38 * $bag = new SqlBagOStuff(); # connect to db first
39 * </code>
40 *
41 * @ingroup Cache
42 */
43 abstract class BagOStuff {
44 private $debugMode = false;
45
46 /**
47 * @param $bool bool
48 */
49 public function setDebug( $bool ) {
50 $this->debugMode = $bool;
51 }
52
53 /* *** THE GUTS OF THE OPERATION *** */
54 /* Override these with functional things in subclasses */
55
56 /**
57 * Get an item with the given key. Returns false if it does not exist.
58 * @param $key string
59 *
60 * @return bool|Object
61 */
62 abstract public function get( $key );
63
64 /**
65 * Get an associative array containing the item for each of the given keys.
66 * Each item will be false if it does not exist.
67 * @param $keys Array List of strings
68 *
69 * @return Array
70 */
71 public function getBatch( array $keys ) {
72 $res = array();
73 foreach ( $keys as $key ) {
74 $res[$key] = $this->get( $key );
75 }
76 return $res;
77 }
78
79 /**
80 * Set an item.
81 * @param $key string
82 * @param $value mixed
83 * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
84 */
85 abstract public function set( $key, $value, $exptime = 0 );
86
87 /**
88 * Delete an item.
89 * @param $key string
90 * @param $time int Amount of time to delay the operation (mostly memcached-specific)
91 */
92 abstract public function delete( $key, $time = 0 );
93
94 public function lock( $key, $timeout = 0 ) {
95 /* stub */
96 return true;
97 }
98
99 public function unlock( $key ) {
100 /* stub */
101 return true;
102 }
103
104 public function keys() {
105 /* stub */
106 return array();
107 }
108
109 /**
110 * Delete all objects expiring before a certain date.
111 * @param $date string The reference date in MW format
112 * @param $progressCallback callback|bool Optional, a function which will be called
113 * regularly during long-running operations with the percentage progress
114 * as the first parameter.
115 *
116 * @return bool on success, false if unimplemented
117 */
118 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
119 // stub
120 return false;
121 }
122
123 /* *** Emulated functions *** */
124
125 public function add( $key, $value, $exptime = 0 ) {
126 if ( !$this->get( $key ) ) {
127 $this->set( $key, $value, $exptime );
128
129 return true;
130 }
131 }
132
133 public function replace( $key, $value, $exptime = 0 ) {
134 if ( $this->get( $key ) !== false ) {
135 $this->set( $key, $value, $exptime );
136 }
137 }
138
139 /**
140 * @param $key String: Key to increase
141 * @param $value Integer: Value to add to $key (Default 1)
142 * @return null if lock is not possible else $key value increased by $value
143 */
144 public function incr( $key, $value = 1 ) {
145 if ( !$this->lock( $key ) ) {
146 return null;
147 }
148
149 $value = intval( $value );
150
151 if ( ( $n = $this->get( $key ) ) !== false ) {
152 $n += $value;
153 $this->set( $key, $n ); // exptime?
154 }
155 $this->unlock( $key );
156
157 return $n;
158 }
159
160 public function decr( $key, $value = 1 ) {
161 return $this->incr( $key, - $value );
162 }
163
164 public function debug( $text ) {
165 if ( $this->debugMode ) {
166 $class = get_class( $this );
167 wfDebug( "$class debug: $text\n" );
168 }
169 }
170
171 /**
172 * Convert an optionally relative time to an absolute time
173 * @return int
174 */
175 protected function convertExpiry( $exptime ) {
176 if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
177 return time() + $exptime;
178 } else {
179 return $exptime;
180 }
181 }
182 }
183
184