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