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;
38 public function __construct( $params ) {
41 if ( !isset( $params['dir'] ) ) {
42 $params['dir'] = wfTempDir();
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;
52 * Encode value and expiry for storage
58 function encode( $value, $expiry ) {
59 # Convert to absolute time
60 $expiry = $this->convertExpiry( $expiry );
62 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
66 * @return array list containing value first and expiry second
68 function decode( $blob ) {
69 if ( !is_string( $blob ) ) {
70 return array( null, 0 );
73 unserialize( substr( $blob, 11 ) ),
74 intval( substr( $blob, 0, 10 ) )
79 function getReader() {
80 if ( file_exists( $this->mFile
) ) {
81 $handle = dba_open( $this->mFile
, 'rl', $this->mHandler
);
83 $handle = $this->getWriter();
87 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
93 function getWriter() {
94 $handle = dba_open( $this->mFile
, 'cl', $this->mHandler
);
97 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
103 function get( $key ) {
104 wfProfileIn( __METHOD__
);
105 wfDebug( __METHOD__
. "($key)\n" );
107 $handle = $this->getReader();
109 wfProfileOut( __METHOD__
);
113 $val = dba_fetch( $key, $handle );
114 list( $val, $expiry ) = $this->decode( $val );
116 # Must close ASAP because locks are held
117 dba_close( $handle );
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" );
128 wfProfileOut( __METHOD__
);
132 function set( $key, $value, $exptime = 0 ) {
133 wfProfileIn( __METHOD__
);
134 wfDebug( __METHOD__
. "($key)\n" );
136 $blob = $this->encode( $value, $exptime );
138 $handle = $this->getWriter();
140 wfProfileOut( __METHOD__
);
144 $ret = dba_replace( $key, $blob, $handle );
145 dba_close( $handle );
147 wfProfileOut( __METHOD__
);
151 function delete( $key, $time = 0 ) {
152 wfProfileIn( __METHOD__
);
153 wfDebug( __METHOD__
. "($key)\n" );
155 $handle = $this->getWriter();
157 wfProfileOut( __METHOD__
);
161 $ret = dba_delete( $key, $handle );
162 dba_close( $handle );
164 wfProfileOut( __METHOD__
);
168 function add( $key, $value, $exptime = 0 ) {
169 wfProfileIn( __METHOD__
);
171 $blob = $this->encode( $value, $exptime );
173 $handle = $this->getWriter();
176 wfProfileOut( __METHOD__
);
180 $ret = dba_insert( $key, $blob, $handle );
182 # Insert failed, check to see if it failed due to an expired key
184 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
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
194 dba_close( $handle );
196 wfProfileOut( __METHOD__
);
201 $reader = $this->getReader();
202 $k1 = dba_firstkey( $reader );
210 $key = dba_nextkey( $reader );
213 $key = dba_nextkey( $reader );