4 * Cache that uses DBA as a backend.
5 * Slow due to the need to constantly open and close the file to avoid holding
6 * writer locks. Intended for development use only, as a memcached workalike
7 * for systems that don't have it.
9 * On construction you can pass array( 'dir' => '/some/path' ); as a parameter
10 * to override the default DBA files directory (wgTmpDirectory).
14 class DBABagOStuff
extends BagOStuff
{
15 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
17 public function __construct( $params ) {
20 if ( !isset( $params['dir'] ) ) {
21 global $wgTmpDirectory;
22 $params['dir'] = $wgTmpDirectory;
25 $this->mFile
= $params['dir']."/mw-cache-" . wfWikiID();
26 $this->mFile
.= '.db';
27 wfDebug( __CLASS__
. ": using cache file {$this->mFile}\n" );
28 $this->mHandler
= $wgDBAhandler;
32 * Encode value and expiry for storage
38 function encode( $value, $expiry ) {
39 # Convert to absolute time
40 $expiry = $this->convertExpiry( $expiry );
42 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
46 * @return array list containing value first and expiry second
48 function decode( $blob ) {
49 if ( !is_string( $blob ) ) {
50 return array( null, 0 );
53 unserialize( substr( $blob, 11 ) ),
54 intval( substr( $blob, 0, 10 ) )
59 function getReader() {
60 if ( file_exists( $this->mFile
) ) {
61 $handle = dba_open( $this->mFile
, 'rl', $this->mHandler
);
63 $handle = $this->getWriter();
67 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
73 function getWriter() {
74 $handle = dba_open( $this->mFile
, 'cl', $this->mHandler
);
77 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
83 function get( $key ) {
84 wfProfileIn( __METHOD__
);
85 wfDebug( __METHOD__
. "($key)\n" );
87 $handle = $this->getReader();
89 wfProfileOut( __METHOD__
);
93 $val = dba_fetch( $key, $handle );
94 list( $val, $expiry ) = $this->decode( $val );
96 # Must close ASAP because locks are held
99 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
100 # Key is expired, delete it
101 $handle = $this->getWriter();
102 dba_delete( $key, $handle );
103 dba_close( $handle );
104 wfDebug( __METHOD__
. ": $key expired\n" );
108 wfProfileOut( __METHOD__
);
112 function set( $key, $value, $exptime = 0 ) {
113 wfProfileIn( __METHOD__
);
114 wfDebug( __METHOD__
. "($key)\n" );
116 $blob = $this->encode( $value, $exptime );
118 $handle = $this->getWriter();
120 wfProfileOut( __METHOD__
);
124 $ret = dba_replace( $key, $blob, $handle );
125 dba_close( $handle );
127 wfProfileOut( __METHOD__
);
131 function delete( $key, $time = 0 ) {
132 wfProfileIn( __METHOD__
);
133 wfDebug( __METHOD__
. "($key)\n" );
135 $handle = $this->getWriter();
137 wfProfileOut( __METHOD__
);
141 $ret = dba_delete( $key, $handle );
142 dba_close( $handle );
144 wfProfileOut( __METHOD__
);
148 function add( $key, $value, $exptime = 0 ) {
149 wfProfileIn( __METHOD__
);
151 $blob = $this->encode( $value, $exptime );
153 $handle = $this->getWriter();
156 wfProfileOut( __METHOD__
);
160 $ret = dba_insert( $key, $blob, $handle );
162 # Insert failed, check to see if it failed due to an expired key
164 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
166 if ( $expiry < time() ) {
167 # Yes expired, delete and try again
168 dba_delete( $key, $handle );
169 $ret = dba_insert( $key, $blob, $handle );
170 # This time if it failed then it will be handled by the caller like any other race
174 dba_close( $handle );
176 wfProfileOut( __METHOD__
);
181 $reader = $this->getReader();
182 $k1 = dba_firstkey( $reader );
190 $key = dba_nextkey( $reader );
193 $key = dba_nextkey( $reader );