Ehcache more like mehcache
[lhc/web/wiklou.git] / includes / objectcache / DBABagOStuff.php
1 <?php
2 /**
3 * Object caching using DBA backend.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Cache that uses DBA as a backend.
26 * Slow due to the need to constantly open and close the file to avoid holding
27 * writer locks. Intended for development use only, as a memcached workalike
28 * for systems that don't have it.
29 *
30 * On construction you can pass array( 'dir' => '/some/path' ); as a parameter
31 * to override the default DBA files directory (wfTempDir()).
32 *
33 * @ingroup Cache
34 */
35 class DBABagOStuff extends BagOStuff {
36 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
37
38 /**
39 * @param $params array
40 */
41 public function __construct( $params ) {
42 global $wgDBAhandler;
43
44 if ( !isset( $params['dir'] ) ) {
45 $params['dir'] = wfTempDir();
46 }
47
48 $this->mFile = $params['dir'] . '/mw-cache-' . wfWikiID() . '.db';
49 wfDebug( __CLASS__ . ": using cache file {$this->mFile}\n" );
50 $this->mHandler = $wgDBAhandler;
51 }
52
53 /**
54 * Encode value and expiry for storage
55 * @param $value
56 * @param $expiry
57 *
58 * @return string
59 */
60 protected function encode( $value, $expiry ) {
61 # Convert to absolute time
62 $expiry = $this->convertExpiry( $expiry );
63
64 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
65 }
66
67 /**
68 * @param $blob string
69 * @return array list containing value first and expiry second
70 */
71 protected function decode( $blob ) {
72 if ( !is_string( $blob ) ) {
73 return array( false, 0 );
74 } else {
75 return array(
76 unserialize( substr( $blob, 11 ) ),
77 intval( substr( $blob, 0, 10 ) )
78 );
79 }
80 }
81
82 /**
83 * @return resource
84 */
85 protected function getReader() {
86 if ( file_exists( $this->mFile ) ) {
87 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
88 } else {
89 $handle = $this->getWriter();
90 }
91
92 if ( !$handle ) {
93 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
94 }
95
96 return $handle;
97 }
98
99 /**
100 * @return resource
101 */
102 protected function getWriter() {
103 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
104
105 if ( !$handle ) {
106 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
107 }
108
109 return $handle;
110 }
111
112 /**
113 * @param $key string
114 * @param $casToken[optional] mixed
115 * @return mixed
116 */
117 public function get( $key, &$casToken = null ) {
118 wfProfileIn( __METHOD__ );
119 wfDebug( __METHOD__ . "($key)\n" );
120
121 $handle = $this->getReader();
122 if ( !$handle ) {
123 wfProfileOut( __METHOD__ );
124 return false;
125 }
126
127 $val = dba_fetch( $key, $handle );
128 $casToken = $val;
129 list( $val, $expiry ) = $this->decode( $val );
130
131 # Must close ASAP because locks are held
132 dba_close( $handle );
133
134 if ( $val !== false && $expiry && $expiry < time() ) {
135 # Key is expired, delete it
136 $handle = $this->getWriter();
137 dba_delete( $key, $handle );
138 dba_close( $handle );
139 wfDebug( __METHOD__ . ": $key expired\n" );
140 $val = false;
141 }
142
143 wfProfileOut( __METHOD__ );
144
145 return $val;
146 }
147
148 /**
149 * @param $key string
150 * @param $value mixed
151 * @param $exptime int
152 * @return bool
153 */
154 public function set( $key, $value, $exptime = 0 ) {
155 wfProfileIn( __METHOD__ );
156 wfDebug( __METHOD__ . "($key)\n" );
157
158 $blob = $this->encode( $value, $exptime );
159
160 $handle = $this->getWriter();
161 if ( !$handle ) {
162 wfProfileOut( __METHOD__ );
163 return false;
164 }
165
166 $ret = dba_replace( $key, $blob, $handle );
167 dba_close( $handle );
168
169 wfProfileOut( __METHOD__ );
170 return $ret;
171 }
172
173 /**
174 * @param $casToken mixed
175 * @param $key string
176 * @param $value mixed
177 * @param $exptime int
178 * @return bool
179 */
180 public function cas( $casToken, $key, $value, $exptime = 0 ) {
181 wfProfileIn( __METHOD__ );
182 wfDebug( __METHOD__ . "($key)\n" );
183
184 $blob = $this->encode( $value, $exptime );
185
186 $handle = $this->getWriter();
187 if ( !$handle ) {
188 wfProfileOut( __METHOD__ );
189 return false;
190 }
191
192 // DBA is locked to any other write connection, so we can safely
193 // compare the current & previous value before saving new value
194 $val = dba_fetch( $key, $handle );
195 if ( $casToken !== $val ) {
196 dba_close( $handle );
197 wfProfileOut( __METHOD__ );
198 return false;
199 }
200
201 $ret = dba_replace( $key, $blob, $handle );
202 dba_close( $handle );
203
204 wfProfileOut( __METHOD__ );
205 return $ret;
206 }
207
208 /**
209 * @param $key string
210 * @param $time int
211 * @return bool
212 */
213 public function delete( $key, $time = 0 ) {
214 wfProfileIn( __METHOD__ );
215 wfDebug( __METHOD__ . "($key)\n" );
216
217 $handle = $this->getWriter();
218 if ( !$handle ) {
219 wfProfileOut( __METHOD__ );
220 return false;
221 }
222
223 $ret = !dba_exists( $key, $handle ) || dba_delete( $key, $handle );
224 dba_close( $handle );
225
226 wfProfileOut( __METHOD__ );
227 return $ret;
228 }
229
230 /**
231 * @param $key string
232 * @param $value mixed
233 * @param $exptime int
234 * @return bool
235 */
236 public function add( $key, $value, $exptime = 0 ) {
237 wfProfileIn( __METHOD__ );
238
239 $blob = $this->encode( $value, $exptime );
240
241 $handle = $this->getWriter();
242
243 if ( !$handle ) {
244 wfProfileOut( __METHOD__ );
245 return false;
246 }
247
248 $ret = dba_insert( $key, $blob, $handle );
249
250 # Insert failed, check to see if it failed due to an expired key
251 if ( !$ret ) {
252 list( , $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
253
254 if ( $expiry && $expiry < time() ) {
255 # Yes expired, delete and try again
256 dba_delete( $key, $handle );
257 $ret = dba_insert( $key, $blob, $handle );
258 # This time if it failed then it will be handled by the caller like any other race
259 }
260 }
261
262 dba_close( $handle );
263
264 wfProfileOut( __METHOD__ );
265 return $ret;
266 }
267
268 /**
269 * @param $key string
270 * @param $step integer
271 * @return integer|bool
272 */
273 public function incr( $key, $step = 1 ) {
274 wfProfileIn( __METHOD__ );
275
276 $handle = $this->getWriter();
277
278 if ( !$handle ) {
279 wfProfileOut( __METHOD__ );
280 return false;
281 }
282
283 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
284 if ( $value !== false ) {
285 if ( $expiry && $expiry < time() ) {
286 # Key is expired, delete it
287 dba_delete( $key, $handle );
288 wfDebug( __METHOD__ . ": $key expired\n" );
289 $value = false;
290 } else {
291 $value += $step;
292 $blob = $this->encode( $value, $expiry );
293
294 $ret = dba_replace( $key, $blob, $handle );
295 $value = $ret ? $value : false;
296 }
297 }
298
299 dba_close( $handle );
300
301 wfProfileOut( __METHOD__ );
302
303 return ( $value === false ) ? false : (int)$value;
304 }
305 }