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 (wgTmpDirectory).
35 class DBABagOStuff
extends BagOStuff
{
36 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
38 public function __construct( $params ) {
41 if ( !isset( $params['dir'] ) ) {
42 global $wgTmpDirectory;
43 $params['dir'] = $wgTmpDirectory;
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;
53 * Encode value and expiry for storage
59 function encode( $value, $expiry ) {
60 # Convert to absolute time
61 $expiry = $this->convertExpiry( $expiry );
63 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
67 * @return array list containing value first and expiry second
69 function decode( $blob ) {
70 if ( !is_string( $blob ) ) {
71 return array( null, 0 );
74 unserialize( substr( $blob, 11 ) ),
75 intval( substr( $blob, 0, 10 ) )
80 function getReader() {
81 if ( file_exists( $this->mFile
) ) {
82 $handle = dba_open( $this->mFile
, 'rl', $this->mHandler
);
84 $handle = $this->getWriter();
88 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
94 function getWriter() {
95 $handle = dba_open( $this->mFile
, 'cl', $this->mHandler
);
98 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
104 function get( $key ) {
105 wfProfileIn( __METHOD__
);
106 wfDebug( __METHOD__
. "($key)\n" );
108 $handle = $this->getReader();
110 wfProfileOut( __METHOD__
);
114 $val = dba_fetch( $key, $handle );
115 list( $val, $expiry ) = $this->decode( $val );
117 # Must close ASAP because locks are held
118 dba_close( $handle );
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" );
129 wfProfileOut( __METHOD__
);
133 function set( $key, $value, $exptime = 0 ) {
134 wfProfileIn( __METHOD__
);
135 wfDebug( __METHOD__
. "($key)\n" );
137 $blob = $this->encode( $value, $exptime );
139 $handle = $this->getWriter();
141 wfProfileOut( __METHOD__
);
145 $ret = dba_replace( $key, $blob, $handle );
146 dba_close( $handle );
148 wfProfileOut( __METHOD__
);
152 function delete( $key, $time = 0 ) {
153 wfProfileIn( __METHOD__
);
154 wfDebug( __METHOD__
. "($key)\n" );
156 $handle = $this->getWriter();
158 wfProfileOut( __METHOD__
);
162 $ret = dba_delete( $key, $handle );
163 dba_close( $handle );
165 wfProfileOut( __METHOD__
);
169 function add( $key, $value, $exptime = 0 ) {
170 wfProfileIn( __METHOD__
);
172 $blob = $this->encode( $value, $exptime );
174 $handle = $this->getWriter();
177 wfProfileOut( __METHOD__
);
181 $ret = dba_insert( $key, $blob, $handle );
183 # Insert failed, check to see if it failed due to an expired key
185 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
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
195 dba_close( $handle );
197 wfProfileOut( __METHOD__
);
202 $reader = $this->getReader();
203 $k1 = dba_firstkey( $reader );
211 $key = dba_nextkey( $reader );
214 $key = dba_nextkey( $reader );