Bump RL filter version to 4 to keep it in sync with the cluster. WMF-centrism, I...
[lhc/web/wiklou.git] / includes / objectcache / SqlBagOStuff.php
1 <?php
2
3 /**
4 * Class to store objects in the database
5 *
6 * @ingroup Cache
7 */
8 class SqlBagOStuff extends BagOStuff {
9 var $lb, $db, $serverInfo;
10 var $lastExpireAll = 0;
11
12 /**
13 * Constructor. Parameters are:
14 * - server: A server info structure in the format required by each
15 * element in $wgDBServers.
16 */
17 public function __construct( $params ) {
18 if ( isset( $params['server'] ) ) {
19 $this->serverInfo = $params['server'];
20 $this->serverInfo['load'] = 1;
21 }
22 }
23
24 protected function getDB() {
25 if ( !isset( $this->db ) ) {
26 # If server connection info was given, use that
27 if ( $this->serverInfo ) {
28 $this->lb = new LoadBalancer( array(
29 'servers' => array( $this->serverInfo ) ) );
30 $this->db = $this->lb->getConnection( DB_MASTER );
31 $this->db->clearFlag( DBO_TRX );
32 } else {
33 # We must keep a separate connection to MySQL in order to avoid deadlocks
34 # However, SQLite has an opposite behaviour.
35 # @todo Investigate behaviour for other databases
36 if ( wfGetDB( DB_MASTER )->getType() == 'sqlite' ) {
37 $this->db = wfGetDB( DB_MASTER );
38 } else {
39 $this->lb = wfGetLBFactory()->newMainLB();
40 $this->db = $this->lb->getConnection( DB_MASTER );
41 $this->db->clearFlag( DBO_TRX );
42 }
43 }
44 }
45
46 return $this->db;
47 }
48
49 public function get( $key ) {
50 # expire old entries if any
51 $this->garbageCollect();
52 $db = $this->getDB();
53 $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
54 array( 'keyname' => $key ), __METHOD__ );
55
56 if ( !$row ) {
57 $this->debug( 'get: no matching rows' );
58 return false;
59 }
60
61 $this->debug( "get: retrieved data; expiry time is " . $row->exptime );
62
63 if ( $this->isExpired( $row->exptime ) ) {
64 $this->debug( "get: key has expired, deleting" );
65 try {
66 $db->begin();
67 # Put the expiry time in the WHERE condition to avoid deleting a
68 # newly-inserted value
69 $db->delete( 'objectcache',
70 array(
71 'keyname' => $key,
72 'exptime' => $row->exptime
73 ), __METHOD__ );
74 $db->commit();
75 } catch ( DBQueryError $e ) {
76 $this->handleWriteError( $e );
77 }
78
79 return false;
80 }
81
82 return $this->unserialize( $db->decodeBlob( $row->value ) );
83 }
84
85 public function set( $key, $value, $exptime = 0 ) {
86 $db = $this->getDB();
87 $exptime = intval( $exptime );
88
89 if ( $exptime < 0 ) {
90 $exptime = 0;
91 }
92
93 if ( $exptime == 0 ) {
94 $encExpiry = $this->getMaxDateTime();
95 } else {
96 if ( $exptime < 3.16e8 ) { # ~10 years
97 $exptime += time();
98 }
99
100 $encExpiry = $db->timestamp( $exptime );
101 }
102 try {
103 $db->begin();
104 // (bug 24425) use a replace if the db supports it instead of
105 // delete/insert to avoid clashes with conflicting keynames
106 $db->replace( 'objectcache', array( 'keyname' ),
107 array(
108 'keyname' => $key,
109 'value' => $db->encodeBlob( $this->serialize( $value ) ),
110 'exptime' => $encExpiry
111 ), __METHOD__ );
112 $db->commit();
113 } catch ( DBQueryError $e ) {
114 $this->handleWriteError( $e );
115
116 return false;
117 }
118
119 return true;
120 }
121
122 public function delete( $key, $time = 0 ) {
123 $db = $this->getDB();
124
125 try {
126 $db->begin();
127 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
128 $db->commit();
129 } catch ( DBQueryError $e ) {
130 $this->handleWriteError( $e );
131
132 return false;
133 }
134
135 return true;
136 }
137
138 public function incr( $key, $step = 1 ) {
139 $db = $this->getDB();
140 $step = intval( $step );
141
142 try {
143 $db->begin();
144 $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
145 array( 'keyname' => $key ), __METHOD__, array( 'FOR UPDATE' ) );
146 if ( $row === false ) {
147 // Missing
148 $db->commit();
149
150 return null;
151 }
152 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
153 if ( $this->isExpired( $row->exptime ) ) {
154 // Expired, do not reinsert
155 $db->commit();
156
157 return null;
158 }
159
160 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value ) ) );
161 $newValue = $oldValue + $step;
162 $db->insert( 'objectcache',
163 array(
164 'keyname' => $key,
165 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
166 'exptime' => $row->exptime
167 ), __METHOD__ );
168 $db->commit();
169 } catch ( DBQueryError $e ) {
170 $this->handleWriteError( $e );
171
172 return null;
173 }
174
175 return $newValue;
176 }
177
178 public function keys() {
179 $db = $this->getDB();
180 $res = $db->select( 'objectcache', array( 'keyname' ), false, __METHOD__ );
181 $result = array();
182
183 foreach ( $res as $row ) {
184 $result[] = $row->keyname;
185 }
186
187 return $result;
188 }
189
190 protected function isExpired( $exptime ) {
191 return $exptime != $this->getMaxDateTime() && wfTimestamp( TS_UNIX, $exptime ) < time();
192 }
193
194 protected function getMaxDateTime() {
195 if ( time() > 0x7fffffff ) {
196 return $this->getDB()->timestamp( 1 << 62 );
197 } else {
198 return $this->getDB()->timestamp( 0x7fffffff );
199 }
200 }
201
202 protected function garbageCollect() {
203 /* Ignore 99% of requests */
204 if ( !mt_rand( 0, 100 ) ) {
205 $now = time();
206 /* Avoid repeating the delete within a few seconds */
207 if ( $now > ( $this->lastExpireAll + 1 ) ) {
208 $this->lastExpireAll = $now;
209 $this->expireAll();
210 }
211 }
212 }
213
214 public function expireAll() {
215 $db = $this->getDB();
216 $now = $db->timestamp();
217
218 try {
219 $db->begin();
220 $db->delete( 'objectcache', array( 'exptime < ' . $db->addQuotes( $now ) ), __METHOD__ );
221 $db->commit();
222 } catch ( DBQueryError $e ) {
223 $this->handleWriteError( $e );
224 }
225 }
226
227 public function deleteAll() {
228 $db = $this->getDB();
229
230 try {
231 $db->begin();
232 $db->delete( 'objectcache', '*', __METHOD__ );
233 $db->commit();
234 } catch ( DBQueryError $e ) {
235 $this->handleWriteError( $e );
236 }
237 }
238
239 /**
240 * Serialize an object and, if possible, compress the representation.
241 * On typical message and page data, this can provide a 3X decrease
242 * in storage requirements.
243 *
244 * @param $data mixed
245 * @return string
246 */
247 protected function serialize( &$data ) {
248 $serial = serialize( $data );
249
250 if ( function_exists( 'gzdeflate' ) ) {
251 return gzdeflate( $serial );
252 } else {
253 return $serial;
254 }
255 }
256
257 /**
258 * Unserialize and, if necessary, decompress an object.
259 * @param $serial string
260 * @return mixed
261 */
262 protected function unserialize( $serial ) {
263 if ( function_exists( 'gzinflate' ) ) {
264 $decomp = @gzinflate( $serial );
265
266 if ( false !== $decomp ) {
267 $serial = $decomp;
268 }
269 }
270
271 $ret = unserialize( $serial );
272
273 return $ret;
274 }
275
276 /**
277 * Handle a DBQueryError which occurred during a write operation.
278 * Ignore errors which are due to a read-only database, rethrow others.
279 */
280 protected function handleWriteError( $exception ) {
281 $db = $this->getDB();
282
283 if ( !$db->wasReadOnlyError() ) {
284 throw $exception;
285 }
286
287 try {
288 $db->rollback();
289 } catch ( DBQueryError $e ) {
290 }
291
292 wfDebug( __METHOD__ . ": ignoring query error\n" );
293 $db->ignoreErrors( false );
294 }
295 }
296
297 /**
298 * Backwards compatibility alias
299 */
300 class MediaWikiBagOStuff extends SqlBagOStuff { }
301