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.
11 class DBABagOStuff
extends BagOStuff
{
12 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
14 public function __construct( $dir = false ) {
17 if ( $dir === false ) {
18 global $wgTmpDirectory;
19 $dir = $wgTmpDirectory;
22 $this->mFile
= "$dir/mw-cache-" . wfWikiID();
23 $this->mFile
.= '.db';
24 wfDebug( __CLASS__
. ": using cache file {$this->mFile}\n" );
25 $this->mHandler
= $wgDBAhandler;
29 * Encode value and expiry for storage
35 function encode( $value, $expiry ) {
36 # Convert to absolute time
37 $expiry = $this->convertExpiry( $expiry );
39 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
43 * @return array list containing value first and expiry second
45 function decode( $blob ) {
46 if ( !is_string( $blob ) ) {
47 return array( null, 0 );
50 unserialize( substr( $blob, 11 ) ),
51 intval( substr( $blob, 0, 10 ) )
56 function getReader() {
57 if ( file_exists( $this->mFile
) ) {
58 $handle = dba_open( $this->mFile
, 'rl', $this->mHandler
);
60 $handle = $this->getWriter();
64 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
70 function getWriter() {
71 $handle = dba_open( $this->mFile
, 'cl', $this->mHandler
);
74 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
80 function get( $key ) {
81 wfProfileIn( __METHOD__
);
82 wfDebug( __METHOD__
. "($key)\n" );
84 $handle = $this->getReader();
86 wfProfileOut( __METHOD__
);
90 $val = dba_fetch( $key, $handle );
91 list( $val, $expiry ) = $this->decode( $val );
93 # Must close ASAP because locks are held
96 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
97 # Key is expired, delete it
98 $handle = $this->getWriter();
99 dba_delete( $key, $handle );
100 dba_close( $handle );
101 wfDebug( __METHOD__
. ": $key expired\n" );
105 wfProfileOut( __METHOD__
);
109 function set( $key, $value, $exptime = 0 ) {
110 wfProfileIn( __METHOD__
);
111 wfDebug( __METHOD__
. "($key)\n" );
113 $blob = $this->encode( $value, $exptime );
115 $handle = $this->getWriter();
117 wfProfileOut( __METHOD__
);
121 $ret = dba_replace( $key, $blob, $handle );
122 dba_close( $handle );
124 wfProfileOut( __METHOD__
);
128 function delete( $key, $time = 0 ) {
129 wfProfileIn( __METHOD__
);
130 wfDebug( __METHOD__
. "($key)\n" );
132 $handle = $this->getWriter();
134 wfProfileOut( __METHOD__
);
138 $ret = dba_delete( $key, $handle );
139 dba_close( $handle );
141 wfProfileOut( __METHOD__
);
145 function add( $key, $value, $exptime = 0 ) {
146 wfProfileIn( __METHOD__
);
148 $blob = $this->encode( $value, $exptime );
150 $handle = $this->getWriter();
153 wfProfileOut( __METHOD__
);
157 $ret = dba_insert( $key, $blob, $handle );
159 # Insert failed, check to see if it failed due to an expired key
161 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
163 if ( $expiry < time() ) {
164 # Yes expired, delete and try again
165 dba_delete( $key, $handle );
166 $ret = dba_insert( $key, $blob, $handle );
167 # This time if it failed then it will be handled by the caller like any other race
171 dba_close( $handle );
173 wfProfileOut( __METHOD__
);
178 $reader = $this->getReader();
179 $k1 = dba_firstkey( $reader );
187 while ( $key = dba_nextkey( $reader ) ) {