* Merged WikiPage::updateRestrictions() and Title::updateTitleProtection() into WikiP...
[mediawiki.git] / includes / objectcache / DBABagOStuff.php
blobade8c0a9c73b33d0edbc62e713e80c4a7b3eec2e
1 <?php
3 /**
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).
12 * @ingroup Cache
14 class DBABagOStuff extends BagOStuff {
15 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
17 public function __construct( $params ) {
18 global $wgDBAhandler;
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;
31 /**
32 * Encode value and expiry for storage
33 * @param $value
34 * @param $expiry
36 * @return string
38 function encode( $value, $expiry ) {
39 # Convert to absolute time
40 $expiry = $this->convertExpiry( $expiry );
42 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
45 /**
46 * @return array list containing value first and expiry second
48 function decode( $blob ) {
49 if ( !is_string( $blob ) ) {
50 return array( null, 0 );
51 } else {
52 return array(
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 );
62 } else {
63 $handle = $this->getWriter();
66 if ( !$handle ) {
67 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
70 return $handle;
73 function getWriter() {
74 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
76 if ( !$handle ) {
77 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
80 return $handle;
83 function get( $key ) {
84 wfProfileIn( __METHOD__ );
85 wfDebug( __METHOD__ . "($key)\n" );
87 $handle = $this->getReader();
88 if ( !$handle ) {
89 wfProfileOut( __METHOD__ );
90 return null;
93 $val = dba_fetch( $key, $handle );
94 list( $val, $expiry ) = $this->decode( $val );
96 # Must close ASAP because locks are held
97 dba_close( $handle );
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" );
105 $val = null;
108 wfProfileOut( __METHOD__ );
109 return $val;
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();
119 if ( !$handle ) {
120 wfProfileOut( __METHOD__ );
121 return false;
124 $ret = dba_replace( $key, $blob, $handle );
125 dba_close( $handle );
127 wfProfileOut( __METHOD__ );
128 return $ret;
131 function delete( $key, $time = 0 ) {
132 wfProfileIn( __METHOD__ );
133 wfDebug( __METHOD__ . "($key)\n" );
135 $handle = $this->getWriter();
136 if ( !$handle ) {
137 wfProfileOut( __METHOD__ );
138 return false;
141 $ret = dba_delete( $key, $handle );
142 dba_close( $handle );
144 wfProfileOut( __METHOD__ );
145 return $ret;
148 function add( $key, $value, $exptime = 0 ) {
149 wfProfileIn( __METHOD__ );
151 $blob = $this->encode( $value, $exptime );
153 $handle = $this->getWriter();
155 if ( !$handle ) {
156 wfProfileOut( __METHOD__ );
157 return false;
160 $ret = dba_insert( $key, $blob, $handle );
162 # Insert failed, check to see if it failed due to an expired key
163 if ( !$ret ) {
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__ );
177 return $ret;
180 function keys() {
181 $reader = $this->getReader();
182 $k1 = dba_firstkey( $reader );
184 if ( !$k1 ) {
185 return array();
188 $result[] = $k1;
190 $key = dba_nextkey( $reader );
191 while ( $key ) {
192 $result[] = $key;
193 $key = dba_nextkey( $reader );
196 return $result;