Merge "Added MediaWikiTestCase::assertTypeOrValue to facilitate common type checks"
[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 * @return mixed
115 */
116 public function get( $key ) {
117 wfProfileIn( __METHOD__ );
118 wfDebug( __METHOD__ . "($key)\n" );
119
120 $handle = $this->getReader();
121 if ( !$handle ) {
122 wfProfileOut( __METHOD__ );
123 return false;
124 }
125
126 $val = dba_fetch( $key, $handle );
127 list( $val, $expiry ) = $this->decode( $val );
128
129 # Must close ASAP because locks are held
130 dba_close( $handle );
131
132 if ( $val !== false && $expiry && $expiry < time() ) {
133 # Key is expired, delete it
134 $handle = $this->getWriter();
135 dba_delete( $key, $handle );
136 dba_close( $handle );
137 wfDebug( __METHOD__ . ": $key expired\n" );
138 $val = false;
139 }
140
141 wfProfileOut( __METHOD__ );
142 return $val;
143 }
144
145 /**
146 * @param $key string
147 * @param $value mixed
148 * @param $exptime int
149 * @return bool
150 */
151 public function set( $key, $value, $exptime = 0 ) {
152 wfProfileIn( __METHOD__ );
153 wfDebug( __METHOD__ . "($key)\n" );
154
155 $blob = $this->encode( $value, $exptime );
156
157 $handle = $this->getWriter();
158 if ( !$handle ) {
159 wfProfileOut( __METHOD__ );
160 return false;
161 }
162
163 $ret = dba_replace( $key, $blob, $handle );
164 dba_close( $handle );
165
166 wfProfileOut( __METHOD__ );
167 return $ret;
168 }
169
170 /**
171 * @param $key string
172 * @param $time int
173 * @return bool
174 */
175 public function delete( $key, $time = 0 ) {
176 wfProfileIn( __METHOD__ );
177 wfDebug( __METHOD__ . "($key)\n" );
178
179 $handle = $this->getWriter();
180 if ( !$handle ) {
181 wfProfileOut( __METHOD__ );
182 return false;
183 }
184
185 $ret = !dba_exists( $key, $handle ) || dba_delete( $key, $handle );
186 dba_close( $handle );
187
188 wfProfileOut( __METHOD__ );
189 return $ret;
190 }
191
192 /**
193 * @param $key string
194 * @param $value mixed
195 * @param $exptime int
196 * @return bool
197 */
198 public function add( $key, $value, $exptime = 0 ) {
199 wfProfileIn( __METHOD__ );
200
201 $blob = $this->encode( $value, $exptime );
202
203 $handle = $this->getWriter();
204
205 if ( !$handle ) {
206 wfProfileOut( __METHOD__ );
207 return false;
208 }
209
210 $ret = dba_insert( $key, $blob, $handle );
211
212 # Insert failed, check to see if it failed due to an expired key
213 if ( !$ret ) {
214 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
215
216 if ( $expiry && $expiry < time() ) {
217 # Yes expired, delete and try again
218 dba_delete( $key, $handle );
219 $ret = dba_insert( $key, $blob, $handle );
220 # This time if it failed then it will be handled by the caller like any other race
221 }
222 }
223
224 dba_close( $handle );
225
226 wfProfileOut( __METHOD__ );
227 return $ret;
228 }
229
230 /**
231 * @param $key string
232 * @param $step integer
233 * @return integer|bool
234 */
235 public function incr( $key, $step = 1 ) {
236 wfProfileIn( __METHOD__ );
237
238 $handle = $this->getWriter();
239
240 if ( !$handle ) {
241 wfProfileOut( __METHOD__ );
242 return false;
243 }
244
245 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
246 if ( $value !== false ) {
247 if ( $expiry && $expiry < time() ) {
248 # Key is expired, delete it
249 dba_delete( $key, $handle );
250 wfDebug( __METHOD__ . ": $key expired\n" );
251 $value = false;
252 } else {
253 $value += $step;
254 $blob = $this->encode( $value, $expiry );
255
256 $ret = dba_replace( $key, $blob, $handle );
257 $value = $ret ? $value : false;
258 }
259 }
260
261 dba_close( $handle );
262
263 wfProfileOut( __METHOD__ );
264
265 return ( $value === false ) ? false : (int)$value;
266 }
267
268 function keys() {
269 $reader = $this->getReader();
270 $k1 = dba_firstkey( $reader );
271
272 if ( !$k1 ) {
273 return array();
274 }
275
276 $result[] = $k1;
277
278 $key = dba_nextkey( $reader );
279 while ( $key ) {
280 $result[] = $key;
281 $key = dba_nextkey( $reader );
282 }
283
284 return $result;
285 }
286 }