Merge "Remove hardcoded quotes on integer"
[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 list( $val, $expiry ) = $this->decode( $val );
129
130 # Must close ASAP because locks are held
131 dba_close( $handle );
132
133 if ( $val !== false && $expiry && $expiry < time() ) {
134 # Key is expired, delete it
135 $handle = $this->getWriter();
136 dba_delete( $key, $handle );
137 dba_close( $handle );
138 wfDebug( __METHOD__ . ": $key expired\n" );
139 $val = false;
140 }
141
142 $casToken = $val;
143
144 wfProfileOut( __METHOD__ );
145
146 return $val;
147 }
148
149 /**
150 * @param $key string
151 * @param $value mixed
152 * @param $exptime int
153 * @return bool
154 */
155 public function set( $key, $value, $exptime = 0 ) {
156 wfProfileIn( __METHOD__ );
157 wfDebug( __METHOD__ . "($key)\n" );
158
159 $blob = $this->encode( $value, $exptime );
160
161 $handle = $this->getWriter();
162 if ( !$handle ) {
163 wfProfileOut( __METHOD__ );
164 return false;
165 }
166
167 $ret = dba_replace( $key, $blob, $handle );
168 dba_close( $handle );
169
170 wfProfileOut( __METHOD__ );
171 return $ret;
172 }
173
174 /**
175 * @param $casToken mixed
176 * @param $key string
177 * @param $value mixed
178 * @param $exptime int
179 * @return bool
180 */
181 public function cas( $casToken, $key, $value, $exptime = 0 ) {
182 wfProfileIn( __METHOD__ );
183 wfDebug( __METHOD__ . "($key)\n" );
184
185 $blob = $this->encode( $value, $exptime );
186
187 $handle = $this->getWriter();
188 if ( !$handle ) {
189 wfProfileOut( __METHOD__ );
190 return false;
191 }
192
193 // DBA is locked to any other write connection, so we can safely
194 // compare the current & previous value before saving new value
195 $val = dba_fetch( $key, $handle );
196 list( $val, $exptime ) = $this->decode( $val );
197 if ( $casToken !== $val ) {
198 dba_close( $handle );
199 wfProfileOut( __METHOD__ );
200 return false;
201 }
202
203 $ret = dba_replace( $key, $blob, $handle );
204 dba_close( $handle );
205
206 wfProfileOut( __METHOD__ );
207 return $ret;
208 }
209
210 /**
211 * @param $key string
212 * @param $time int
213 * @return bool
214 */
215 public function delete( $key, $time = 0 ) {
216 wfProfileIn( __METHOD__ );
217 wfDebug( __METHOD__ . "($key)\n" );
218
219 $handle = $this->getWriter();
220 if ( !$handle ) {
221 wfProfileOut( __METHOD__ );
222 return false;
223 }
224
225 $ret = !dba_exists( $key, $handle ) || dba_delete( $key, $handle );
226 dba_close( $handle );
227
228 wfProfileOut( __METHOD__ );
229 return $ret;
230 }
231
232 /**
233 * @param $key string
234 * @param $value mixed
235 * @param $exptime int
236 * @return bool
237 */
238 public function add( $key, $value, $exptime = 0 ) {
239 wfProfileIn( __METHOD__ );
240
241 $blob = $this->encode( $value, $exptime );
242
243 $handle = $this->getWriter();
244
245 if ( !$handle ) {
246 wfProfileOut( __METHOD__ );
247 return false;
248 }
249
250 $ret = dba_insert( $key, $blob, $handle );
251
252 # Insert failed, check to see if it failed due to an expired key
253 if ( !$ret ) {
254 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
255
256 if ( $expiry && $expiry < time() ) {
257 # Yes expired, delete and try again
258 dba_delete( $key, $handle );
259 $ret = dba_insert( $key, $blob, $handle );
260 # This time if it failed then it will be handled by the caller like any other race
261 }
262 }
263
264 dba_close( $handle );
265
266 wfProfileOut( __METHOD__ );
267 return $ret;
268 }
269
270 /**
271 * @param $key string
272 * @param $step integer
273 * @return integer|bool
274 */
275 public function incr( $key, $step = 1 ) {
276 wfProfileIn( __METHOD__ );
277
278 $handle = $this->getWriter();
279
280 if ( !$handle ) {
281 wfProfileOut( __METHOD__ );
282 return false;
283 }
284
285 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
286 if ( $value !== false ) {
287 if ( $expiry && $expiry < time() ) {
288 # Key is expired, delete it
289 dba_delete( $key, $handle );
290 wfDebug( __METHOD__ . ": $key expired\n" );
291 $value = false;
292 } else {
293 $value += $step;
294 $blob = $this->encode( $value, $expiry );
295
296 $ret = dba_replace( $key, $blob, $handle );
297 $value = $ret ? $value : false;
298 }
299 }
300
301 dba_close( $handle );
302
303 wfProfileOut( __METHOD__ );
304
305 return ( $value === false ) ? false : (int)$value;
306 }
307 }