3 * Object caching using DBA backend.
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.
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.
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
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.
30 * On construction you can pass array( 'dir' => '/some/path' ); as a parameter
31 * to override the default DBA files directory (wfTempDir()).
35 class DBABagOStuff
extends BagOStuff
{
36 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
39 * @param $params array
41 public function __construct( $params ) {
44 if ( !isset( $params['dir'] ) ) {
45 $params['dir'] = wfTempDir();
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;
55 * Encode value and expiry for storage
61 function encode( $value, $expiry ) {
62 # Convert to absolute time
63 $expiry = $this->convertExpiry( $expiry );
65 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
70 * @return array list containing value first and expiry second
72 function decode( $blob ) {
73 if ( !is_string( $blob ) ) {
74 return array( null, 0 );
77 unserialize( substr( $blob, 11 ) ),
78 intval( substr( $blob, 0, 10 ) )
86 function getReader() {
87 if ( file_exists( $this->mFile
) ) {
88 $handle = dba_open( $this->mFile
, 'rl', $this->mHandler
);
90 $handle = $this->getWriter();
94 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
103 function getWriter() {
104 $handle = dba_open( $this->mFile
, 'cl', $this->mHandler
);
107 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
115 * @return mixed|null|string
117 function get( $key ) {
118 wfProfileIn( __METHOD__
);
119 wfDebug( __METHOD__
. "($key)\n" );
121 $handle = $this->getReader();
123 wfProfileOut( __METHOD__
);
127 $val = dba_fetch( $key, $handle );
128 list( $val, $expiry ) = $this->decode( $val );
130 # Must close ASAP because locks are held
131 dba_close( $handle );
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" );
142 wfProfileOut( __METHOD__
);
148 * @param $value mixed
149 * @param $exptime int
152 function set( $key, $value, $exptime = 0 ) {
153 wfProfileIn( __METHOD__
);
154 wfDebug( __METHOD__
. "($key)\n" );
156 $blob = $this->encode( $value, $exptime );
158 $handle = $this->getWriter();
160 wfProfileOut( __METHOD__
);
164 $ret = dba_replace( $key, $blob, $handle );
165 dba_close( $handle );
167 wfProfileOut( __METHOD__
);
176 function delete( $key, $time = 0 ) {
177 wfProfileIn( __METHOD__
);
178 wfDebug( __METHOD__
. "($key)\n" );
180 $handle = $this->getWriter();
182 wfProfileOut( __METHOD__
);
186 $ret = dba_delete( $key, $handle );
187 dba_close( $handle );
189 wfProfileOut( __METHOD__
);
195 * @param $value mixed
196 * @param $exptime int
199 function add( $key, $value, $exptime = 0 ) {
200 wfProfileIn( __METHOD__
);
202 $blob = $this->encode( $value, $exptime );
204 $handle = $this->getWriter();
207 wfProfileOut( __METHOD__
);
211 $ret = dba_insert( $key, $blob, $handle );
213 # Insert failed, check to see if it failed due to an expired key
215 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
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
225 dba_close( $handle );
227 wfProfileOut( __METHOD__
);
235 $reader = $this->getReader();
236 $k1 = dba_firstkey( $reader );
244 $key = dba_nextkey( $reader );
247 $key = dba_nextkey( $reader );