58c434c58a7ebd43dbe204c5c194bbbfdc0b2760
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
33 function encode( $value, $expiry ) {
34 # Convert to absolute time
35 $expiry = $this->convertExpiry( $expiry );
37 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
41 * @return array list containing value first and expiry second
43 function decode( $blob ) {
44 if ( !is_string( $blob ) ) {
45 return array( null, 0 );
48 unserialize( substr( $blob, 11 ) ),
49 intval( substr( $blob, 0, 10 ) )
54 function getReader() {
55 if ( file_exists( $this->mFile
) ) {
56 $handle = dba_open( $this->mFile
, 'rl', $this->mHandler
);
58 $handle = $this->getWriter();
62 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
68 function getWriter() {
69 $handle = dba_open( $this->mFile
, 'cl', $this->mHandler
);
72 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
78 function get( $key ) {
79 wfProfileIn( __METHOD__
);
80 wfDebug( __METHOD__
. "($key)\n" );
82 $handle = $this->getReader();
84 wfProfileOut( __METHOD__
);
88 $val = dba_fetch( $key, $handle );
89 list( $val, $expiry ) = $this->decode( $val );
91 # Must close ASAP because locks are held
94 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
95 # Key is expired, delete it
96 $handle = $this->getWriter();
97 dba_delete( $key, $handle );
99 wfDebug( __METHOD__
. ": $key expired\n" );
103 wfProfileOut( __METHOD__
);
107 function set( $key, $value, $exptime = 0 ) {
108 wfProfileIn( __METHOD__
);
109 wfDebug( __METHOD__
. "($key)\n" );
111 $blob = $this->encode( $value, $exptime );
113 $handle = $this->getWriter();
115 wfProfileOut( __METHOD__
);
119 $ret = dba_replace( $key, $blob, $handle );
120 dba_close( $handle );
122 wfProfileOut( __METHOD__
);
126 function delete( $key, $time = 0 ) {
127 wfProfileIn( __METHOD__
);
128 wfDebug( __METHOD__
. "($key)\n" );
130 $handle = $this->getWriter();
132 wfProfileOut( __METHOD__
);
136 $ret = dba_delete( $key, $handle );
137 dba_close( $handle );
139 wfProfileOut( __METHOD__
);
143 function add( $key, $value, $exptime = 0 ) {
144 wfProfileIn( __METHOD__
);
146 $blob = $this->encode( $value, $exptime );
148 $handle = $this->getWriter();
151 wfProfileOut( __METHOD__
);
155 $ret = dba_insert( $key, $blob, $handle );
157 # Insert failed, check to see if it failed due to an expired key
159 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
161 if ( $expiry < time() ) {
162 # Yes expired, delete and try again
163 dba_delete( $key, $handle );
164 $ret = dba_insert( $key, $blob, $handle );
165 # This time if it failed then it will be handled by the caller like any other race
169 dba_close( $handle );
171 wfProfileOut( __METHOD__
);
176 $reader = $this->getReader();
177 $k1 = dba_firstkey( $reader );
185 while ( $key = dba_nextkey( $reader ) ) {