Merge "(bug 37090) Remove Spanish gender aliases."
[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 (wgTmpDirectory).
32 *
33 * @ingroup Cache
34 */
35 class DBABagOStuff extends BagOStuff {
36 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
37
38 public function __construct( $params ) {
39 global $wgDBAhandler;
40
41 if ( !isset( $params['dir'] ) ) {
42 global $wgTmpDirectory;
43 $params['dir'] = $wgTmpDirectory;
44 }
45
46 $this->mFile = $params['dir']."/mw-cache-" . wfWikiID();
47 $this->mFile .= '.db';
48 wfDebug( __CLASS__ . ": using cache file {$this->mFile}\n" );
49 $this->mHandler = $wgDBAhandler;
50 }
51
52 /**
53 * Encode value and expiry for storage
54 * @param $value
55 * @param $expiry
56 *
57 * @return string
58 */
59 function encode( $value, $expiry ) {
60 # Convert to absolute time
61 $expiry = $this->convertExpiry( $expiry );
62
63 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
64 }
65
66 /**
67 * @return array list containing value first and expiry second
68 */
69 function decode( $blob ) {
70 if ( !is_string( $blob ) ) {
71 return array( null, 0 );
72 } else {
73 return array(
74 unserialize( substr( $blob, 11 ) ),
75 intval( substr( $blob, 0, 10 ) )
76 );
77 }
78 }
79
80 function getReader() {
81 if ( file_exists( $this->mFile ) ) {
82 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
83 } else {
84 $handle = $this->getWriter();
85 }
86
87 if ( !$handle ) {
88 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
89 }
90
91 return $handle;
92 }
93
94 function getWriter() {
95 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
96
97 if ( !$handle ) {
98 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
99 }
100
101 return $handle;
102 }
103
104 function get( $key ) {
105 wfProfileIn( __METHOD__ );
106 wfDebug( __METHOD__ . "($key)\n" );
107
108 $handle = $this->getReader();
109 if ( !$handle ) {
110 wfProfileOut( __METHOD__ );
111 return null;
112 }
113
114 $val = dba_fetch( $key, $handle );
115 list( $val, $expiry ) = $this->decode( $val );
116
117 # Must close ASAP because locks are held
118 dba_close( $handle );
119
120 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
121 # Key is expired, delete it
122 $handle = $this->getWriter();
123 dba_delete( $key, $handle );
124 dba_close( $handle );
125 wfDebug( __METHOD__ . ": $key expired\n" );
126 $val = null;
127 }
128
129 wfProfileOut( __METHOD__ );
130 return $val;
131 }
132
133 function set( $key, $value, $exptime = 0 ) {
134 wfProfileIn( __METHOD__ );
135 wfDebug( __METHOD__ . "($key)\n" );
136
137 $blob = $this->encode( $value, $exptime );
138
139 $handle = $this->getWriter();
140 if ( !$handle ) {
141 wfProfileOut( __METHOD__ );
142 return false;
143 }
144
145 $ret = dba_replace( $key, $blob, $handle );
146 dba_close( $handle );
147
148 wfProfileOut( __METHOD__ );
149 return $ret;
150 }
151
152 function delete( $key, $time = 0 ) {
153 wfProfileIn( __METHOD__ );
154 wfDebug( __METHOD__ . "($key)\n" );
155
156 $handle = $this->getWriter();
157 if ( !$handle ) {
158 wfProfileOut( __METHOD__ );
159 return false;
160 }
161
162 $ret = dba_delete( $key, $handle );
163 dba_close( $handle );
164
165 wfProfileOut( __METHOD__ );
166 return $ret;
167 }
168
169 function add( $key, $value, $exptime = 0 ) {
170 wfProfileIn( __METHOD__ );
171
172 $blob = $this->encode( $value, $exptime );
173
174 $handle = $this->getWriter();
175
176 if ( !$handle ) {
177 wfProfileOut( __METHOD__ );
178 return false;
179 }
180
181 $ret = dba_insert( $key, $blob, $handle );
182
183 # Insert failed, check to see if it failed due to an expired key
184 if ( !$ret ) {
185 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
186
187 if ( $expiry < time() ) {
188 # Yes expired, delete and try again
189 dba_delete( $key, $handle );
190 $ret = dba_insert( $key, $blob, $handle );
191 # This time if it failed then it will be handled by the caller like any other race
192 }
193 }
194
195 dba_close( $handle );
196
197 wfProfileOut( __METHOD__ );
198 return $ret;
199 }
200
201 function keys() {
202 $reader = $this->getReader();
203 $k1 = dba_firstkey( $reader );
204
205 if ( !$k1 ) {
206 return array();
207 }
208
209 $result[] = $k1;
210
211 $key = dba_nextkey( $reader );
212 while ( $key ) {
213 $result[] = $key;
214 $key = dba_nextkey( $reader );
215 }
216
217 return $result;
218 }
219 }
220