Bug 35034 - moved autocomment-prefix between the prefix and the arrow. Follow up...
[lhc/web/wiklou.git] / includes / objectcache / DBABagOStuff.php
1 <?php
2
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.
8 *
9 * On construction you can pass array( 'dir' => '/some/path' ); as a parameter
10 * to override the default DBA files directory (wgTmpDirectory).
11 *
12 * @ingroup Cache
13 */
14 class DBABagOStuff extends BagOStuff {
15 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
16
17 public function __construct( $params ) {
18 global $wgDBAhandler;
19
20 if ( !isset( $params['dir'] ) ) {
21 global $wgTmpDirectory;
22 $params['dir'] = $wgTmpDirectory;
23 }
24
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;
29 }
30
31 /**
32 * Encode value and expiry for storage
33 * @param $value
34 * @param $expiry
35 *
36 * @return string
37 */
38 function encode( $value, $expiry ) {
39 # Convert to absolute time
40 $expiry = $this->convertExpiry( $expiry );
41
42 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
43 }
44
45 /**
46 * @return array list containing value first and expiry second
47 */
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 ) )
55 );
56 }
57 }
58
59 function getReader() {
60 if ( file_exists( $this->mFile ) ) {
61 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
62 } else {
63 $handle = $this->getWriter();
64 }
65
66 if ( !$handle ) {
67 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
68 }
69
70 return $handle;
71 }
72
73 function getWriter() {
74 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
75
76 if ( !$handle ) {
77 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
78 }
79
80 return $handle;
81 }
82
83 function get( $key ) {
84 wfProfileIn( __METHOD__ );
85 wfDebug( __METHOD__ . "($key)\n" );
86
87 $handle = $this->getReader();
88 if ( !$handle ) {
89 wfProfileOut( __METHOD__ );
90 return null;
91 }
92
93 $val = dba_fetch( $key, $handle );
94 list( $val, $expiry ) = $this->decode( $val );
95
96 # Must close ASAP because locks are held
97 dba_close( $handle );
98
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;
106 }
107
108 wfProfileOut( __METHOD__ );
109 return $val;
110 }
111
112 function set( $key, $value, $exptime = 0 ) {
113 wfProfileIn( __METHOD__ );
114 wfDebug( __METHOD__ . "($key)\n" );
115
116 $blob = $this->encode( $value, $exptime );
117
118 $handle = $this->getWriter();
119 if ( !$handle ) {
120 wfProfileOut( __METHOD__ );
121 return false;
122 }
123
124 $ret = dba_replace( $key, $blob, $handle );
125 dba_close( $handle );
126
127 wfProfileOut( __METHOD__ );
128 return $ret;
129 }
130
131 function delete( $key, $time = 0 ) {
132 wfProfileIn( __METHOD__ );
133 wfDebug( __METHOD__ . "($key)\n" );
134
135 $handle = $this->getWriter();
136 if ( !$handle ) {
137 wfProfileOut( __METHOD__ );
138 return false;
139 }
140
141 $ret = dba_delete( $key, $handle );
142 dba_close( $handle );
143
144 wfProfileOut( __METHOD__ );
145 return $ret;
146 }
147
148 function add( $key, $value, $exptime = 0 ) {
149 wfProfileIn( __METHOD__ );
150
151 $blob = $this->encode( $value, $exptime );
152
153 $handle = $this->getWriter();
154
155 if ( !$handle ) {
156 wfProfileOut( __METHOD__ );
157 return false;
158 }
159
160 $ret = dba_insert( $key, $blob, $handle );
161
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 ) );
165
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
171 }
172 }
173
174 dba_close( $handle );
175
176 wfProfileOut( __METHOD__ );
177 return $ret;
178 }
179
180 function keys() {
181 $reader = $this->getReader();
182 $k1 = dba_firstkey( $reader );
183
184 if ( !$k1 ) {
185 return array();
186 }
187
188 $result[] = $k1;
189
190 $key = dba_nextkey( $reader );
191 while ( $key ) {
192 $result[] = $key;
193 $key = dba_nextkey( $reader );
194 }
195
196 return $result;
197 }
198 }
199