Merge "Fix separated login link so that create account and login are always next...
[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();
49 $this->mFile .= '.db';
50 wfDebug( __CLASS__ . ": using cache file {$this->mFile}\n" );
51 $this->mHandler = $wgDBAhandler;
52 }
53
54 /**
55 * Encode value and expiry for storage
56 * @param $value
57 * @param $expiry
58 *
59 * @return string
60 */
61 function encode( $value, $expiry ) {
62 # Convert to absolute time
63 $expiry = $this->convertExpiry( $expiry );
64
65 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
66 }
67
68 /**
69 * @param $blob string
70 * @return array list containing value first and expiry second
71 */
72 function decode( $blob ) {
73 if ( !is_string( $blob ) ) {
74 return array( null, 0 );
75 } else {
76 return array(
77 unserialize( substr( $blob, 11 ) ),
78 intval( substr( $blob, 0, 10 ) )
79 );
80 }
81 }
82
83 /**
84 * @return resource
85 */
86 function getReader() {
87 if ( file_exists( $this->mFile ) ) {
88 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
89 } else {
90 $handle = $this->getWriter();
91 }
92
93 if ( !$handle ) {
94 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
95 }
96
97 return $handle;
98 }
99
100 /**
101 * @return resource
102 */
103 function getWriter() {
104 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
105
106 if ( !$handle ) {
107 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
108 }
109
110 return $handle;
111 }
112
113 /**
114 * @param $key string
115 * @return mixed|null|string
116 */
117 function get( $key ) {
118 wfProfileIn( __METHOD__ );
119 wfDebug( __METHOD__ . "($key)\n" );
120
121 $handle = $this->getReader();
122 if ( !$handle ) {
123 wfProfileOut( __METHOD__ );
124 return null;
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 ( !is_null( $val ) && $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 = null;
140 }
141
142 wfProfileOut( __METHOD__ );
143 return $val;
144 }
145
146 /**
147 * @param $key string
148 * @param $value mixed
149 * @param $exptime int
150 * @return bool
151 */
152 function set( $key, $value, $exptime = 0 ) {
153 wfProfileIn( __METHOD__ );
154 wfDebug( __METHOD__ . "($key)\n" );
155
156 $blob = $this->encode( $value, $exptime );
157
158 $handle = $this->getWriter();
159 if ( !$handle ) {
160 wfProfileOut( __METHOD__ );
161 return false;
162 }
163
164 $ret = dba_replace( $key, $blob, $handle );
165 dba_close( $handle );
166
167 wfProfileOut( __METHOD__ );
168 return $ret;
169 }
170
171 /**
172 * @param $key string
173 * @param $time int
174 * @return bool
175 */
176 function delete( $key, $time = 0 ) {
177 wfProfileIn( __METHOD__ );
178 wfDebug( __METHOD__ . "($key)\n" );
179
180 $handle = $this->getWriter();
181 if ( !$handle ) {
182 wfProfileOut( __METHOD__ );
183 return false;
184 }
185
186 $ret = dba_delete( $key, $handle );
187 dba_close( $handle );
188
189 wfProfileOut( __METHOD__ );
190 return $ret;
191 }
192
193 /**
194 * @param $key string
195 * @param $value mixed
196 * @param $exptime int
197 * @return bool
198 */
199 function add( $key, $value, $exptime = 0 ) {
200 wfProfileIn( __METHOD__ );
201
202 $blob = $this->encode( $value, $exptime );
203
204 $handle = $this->getWriter();
205
206 if ( !$handle ) {
207 wfProfileOut( __METHOD__ );
208 return false;
209 }
210
211 $ret = dba_insert( $key, $blob, $handle );
212
213 # Insert failed, check to see if it failed due to an expired key
214 if ( !$ret ) {
215 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
216
217 if ( $expiry < time() ) {
218 # Yes expired, delete and try again
219 dba_delete( $key, $handle );
220 $ret = dba_insert( $key, $blob, $handle );
221 # This time if it failed then it will be handled by the caller like any other race
222 }
223 }
224
225 dba_close( $handle );
226
227 wfProfileOut( __METHOD__ );
228 return $ret;
229 }
230
231 /**
232 * @return Array
233 */
234 function keys() {
235 $reader = $this->getReader();
236 $k1 = dba_firstkey( $reader );
237
238 if ( !$k1 ) {
239 return array();
240 }
241
242 $result[] = $k1;
243
244 $key = dba_nextkey( $reader );
245 while ( $key ) {
246 $result[] = $key;
247 $key = dba_nextkey( $reader );
248 }
249
250 return $result;
251 }
252 }
253