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
31 function encode( $value, $expiry ) {
32 # Convert to absolute time
33 $expiry = $this->convertExpiry( $expiry );
35 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
39 * @return list containing value first and expiry second
41 function decode( $blob ) {
42 if ( !is_string( $blob ) ) {
43 return array( null, 0 );
46 unserialize( substr( $blob, 11 ) ),
47 intval( substr( $blob, 0, 10 ) )
52 function getReader() {
53 if ( file_exists( $this->mFile
) ) {
54 $handle = dba_open( $this->mFile
, 'rl', $this->mHandler
);
56 $handle = $this->getWriter();
60 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
66 function getWriter() {
67 $handle = dba_open( $this->mFile
, 'cl', $this->mHandler
);
70 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
76 function get( $key ) {
77 wfProfileIn( __METHOD__
);
78 wfDebug( __METHOD__
. "($key)\n" );
80 $handle = $this->getReader();
82 wfProfileOut( __METHOD__
);
86 $val = dba_fetch( $key, $handle );
87 list( $val, $expiry ) = $this->decode( $val );
89 # Must close ASAP because locks are held
92 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
93 # Key is expired, delete it
94 $handle = $this->getWriter();
95 dba_delete( $key, $handle );
97 wfDebug( __METHOD__
. ": $key expired\n" );
101 wfProfileOut( __METHOD__
);
105 function set( $key, $value, $exptime = 0 ) {
106 wfProfileIn( __METHOD__
);
107 wfDebug( __METHOD__
. "($key)\n" );
109 $blob = $this->encode( $value, $exptime );
111 $handle = $this->getWriter();
113 wfProfileOut( __METHOD__
);
117 $ret = dba_replace( $key, $blob, $handle );
118 dba_close( $handle );
120 wfProfileOut( __METHOD__
);
124 function delete( $key, $time = 0 ) {
125 wfProfileIn( __METHOD__
);
126 wfDebug( __METHOD__
. "($key)\n" );
128 $handle = $this->getWriter();
130 wfProfileOut( __METHOD__
);
134 $ret = dba_delete( $key, $handle );
135 dba_close( $handle );
137 wfProfileOut( __METHOD__
);
141 function add( $key, $value, $exptime = 0 ) {
142 wfProfileIn( __METHOD__
);
144 $blob = $this->encode( $value, $exptime );
146 $handle = $this->getWriter();
149 wfProfileOut( __METHOD__
);
153 $ret = dba_insert( $key, $blob, $handle );
155 # Insert failed, check to see if it failed due to an expired key
157 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
159 if ( $expiry < time() ) {
160 # Yes expired, delete and try again
161 dba_delete( $key, $handle );
162 $ret = dba_insert( $key, $blob, $handle );
163 # This time if it failed then it will be handled by the caller like any other race
167 dba_close( $handle );
169 wfProfileOut( __METHOD__
);
174 $reader = $this->getReader();
175 $k1 = dba_firstkey( $reader );
183 while ( $key = dba_nextkey( $reader ) ) {