MediaWikiBagOStuff -> SqlBagOStuff in comment, both are the same since r55079
[lhc/web/wiklou.git] / includes / BagOStuff.php
1 <?php
2 #
3 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
20
21 /**
22 * @defgroup Cache Cache
23 *
24 * @file
25 * @ingroup Cache
26 */
27
28 /**
29 * interface is intended to be more or less compatible with
30 * the PHP memcached client.
31 *
32 * backends for local hash array and SQL table included:
33 * <code>
34 * $bag = new HashBagOStuff();
35 * $bag = new SqlBagOStuff(); # connect to db first
36 * </code>
37 *
38 * @ingroup Cache
39 */
40 abstract class BagOStuff {
41 var $debugMode = false;
42
43 public function set_debug( $bool ) {
44 $this->debugMode = $bool;
45 }
46
47 /* *** THE GUTS OF THE OPERATION *** */
48 /* Override these with functional things in subclasses */
49
50 /**
51 * Get an item with the given key. Returns false if it does not exist.
52 * @param $key string
53 */
54 abstract public function get( $key );
55
56 /**
57 * Set an item.
58 * @param $key string
59 * @param $value mixed
60 * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
61 */
62 abstract public function set( $key, $value, $exptime = 0 );
63
64 /*
65 * Delete an item.
66 * @param $key string
67 * @param $time int Amount of time to delay the operation (mostly memcached-specific)
68 */
69 abstract public function delete( $key, $time = 0 );
70
71 public function lock( $key, $timeout = 0 ) {
72 /* stub */
73 return true;
74 }
75
76 public function unlock( $key ) {
77 /* stub */
78 return true;
79 }
80
81 public function keys() {
82 /* stub */
83 return array();
84 }
85
86 /* *** Emulated functions *** */
87 /* Better performance can likely be got with custom written versions */
88 public function get_multi( $keys ) {
89 $out = array();
90 foreach ( $keys as $key ) {
91 $out[$key] = $this->get( $key );
92 }
93 return $out;
94 }
95
96 public function set_multi( $hash, $exptime = 0 ) {
97 foreach ( $hash as $key => $value ) {
98 $this->set( $key, $value, $exptime );
99 }
100 }
101
102 public function add( $key, $value, $exptime = 0 ) {
103 if ( $this->get( $key ) == false ) {
104 $this->set( $key, $value, $exptime );
105 return true;
106 }
107 }
108
109 public function add_multi( $hash, $exptime = 0 ) {
110 foreach ( $hash as $key => $value ) {
111 $this->add( $key, $value, $exptime );
112 }
113 }
114
115 public function delete_multi( $keys, $time = 0 ) {
116 foreach ( $keys as $key ) {
117 $this->delete( $key, $time );
118 }
119 }
120
121 public function replace( $key, $value, $exptime = 0 ) {
122 if ( $this->get( $key ) !== false ) {
123 $this->set( $key, $value, $exptime );
124 }
125 }
126
127 public function incr( $key, $value = 1 ) {
128 if ( !$this->lock( $key ) ) {
129 return false;
130 }
131 $value = intval( $value );
132
133 $n = false;
134 if ( ( $n = $this->get( $key ) ) !== false ) {
135 $n += $value;
136 $this->set( $key, $n ); // exptime?
137 }
138 $this->unlock( $key );
139 return $n;
140 }
141
142 public function decr( $key, $value = 1 ) {
143 return $this->incr( $key, -$value );
144 }
145
146 public function debug( $text ) {
147 if ( $this->debugMode )
148 wfDebug( "BagOStuff debug: $text\n" );
149 }
150
151 /**
152 * Convert an optionally relative time to an absolute time
153 */
154 protected function convertExpiry( $exptime ) {
155 if ( ( $exptime != 0 ) && ( $exptime < 3600 * 24 * 30 ) ) {
156 return time() + $exptime;
157 } else {
158 return $exptime;
159 }
160 }
161 }
162
163
164 /**
165 * Functional versions!
166 * This is a test of the interface, mainly. It stores things in an associative
167 * array, which is not going to persist between program runs.
168 *
169 * @ingroup Cache
170 */
171 class HashBagOStuff extends BagOStuff {
172 var $bag;
173
174 function __construct() {
175 $this->bag = array();
176 }
177
178 protected function expire( $key ) {
179 $et = $this->bag[$key][1];
180 if ( ( $et == 0 ) || ( $et > time() ) ) {
181 return false;
182 }
183 $this->delete( $key );
184 return true;
185 }
186
187 function get( $key ) {
188 if ( !isset( $this->bag[$key] ) ) {
189 return false;
190 }
191 if ( $this->expire( $key ) ) {
192 return false;
193 }
194 return $this->bag[$key][0];
195 }
196
197 function set( $key, $value, $exptime = 0 ) {
198 $this->bag[$key] = array( $value, $this->convertExpiry( $exptime ) );
199 }
200
201 function delete( $key, $time = 0 ) {
202 if ( !isset( $this->bag[$key] ) ) {
203 return false;
204 }
205 unset( $this->bag[$key] );
206 return true;
207 }
208
209 function keys() {
210 return array_keys( $this->bag );
211 }
212 }
213
214 /**
215 * Class to store objects in the database
216 *
217 * @ingroup Cache
218 */
219 class SqlBagOStuff extends BagOStuff {
220 var $lb, $db;
221 var $lastExpireAll = 0;
222
223 protected function getDB() {
224 global $wgDBtype;
225 if ( !isset( $this->db ) ) {
226 /* We must keep a separate connection to MySQL in order to avoid deadlocks
227 * However, SQLite has an opposite behaviour.
228 * @todo Investigate behaviour for other databases
229 */
230 if ( $wgDBtype == 'sqlite' ) {
231 $this->db = wfGetDB( DB_MASTER );
232 } else {
233 $this->lb = wfGetLBFactory()->newMainLB();
234 $this->db = $this->lb->getConnection( DB_MASTER );
235 $this->db->clearFlag( DBO_TRX );
236 }
237 }
238 return $this->db;
239 }
240
241 public function get( $key ) {
242 # expire old entries if any
243 $this->garbageCollect();
244 $db = $this->getDB();
245 $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
246 array( 'keyname' => $key ), __METHOD__ );
247 if ( !$row ) {
248 $this->debug( 'get: no matching rows' );
249 return false;
250 }
251
252 $this->debug( "get: retrieved data; expiry time is " . $row->exptime );
253 if ( $this->isExpired( $row->exptime ) ) {
254 $this->debug( "get: key has expired, deleting" );
255 try {
256 $db->begin();
257 # Put the expiry time in the WHERE condition to avoid deleting a
258 # newly-inserted value
259 $db->delete( 'objectcache',
260 array(
261 'keyname' => $key,
262 'exptime' => $row->exptime
263 ), __METHOD__ );
264 $db->commit();
265 } catch ( DBQueryError $e ) {
266 $this->handleWriteError( $e );
267 }
268 return false;
269 }
270 return $this->unserialize( $db->decodeBlob( $row->value ) );
271 }
272
273 public function set( $key, $value, $exptime = 0 ) {
274 $db = $this->getDB();
275 $exptime = intval( $exptime );
276 if ( $exptime < 0 ) $exptime = 0;
277 if ( $exptime == 0 ) {
278 $encExpiry = $this->getMaxDateTime();
279 } else {
280 if ( $exptime < 3.16e8 ) # ~10 years
281 $exptime += time();
282 $encExpiry = $db->timestamp( $exptime );
283 }
284 try {
285 $db->begin();
286 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
287 $db->insert( 'objectcache',
288 array(
289 'keyname' => $key,
290 'value' => $db->encodeBlob( $this->serialize( $value ) ),
291 'exptime' => $encExpiry
292 ), __METHOD__ );
293 $db->commit();
294 } catch ( DBQueryError $e ) {
295 $this->handleWriteError( $e );
296 return false;
297 }
298 return true;
299 }
300
301 public function delete( $key, $time = 0 ) {
302 $db = $this->getDB();
303 try {
304 $db->begin();
305 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
306 $db->commit();
307 } catch ( DBQueryError $e ) {
308 $this->handleWriteError( $e );
309 return false;
310 }
311 return true;
312 }
313
314 public function incr( $key, $step = 1 ) {
315 $db = $this->getDB();
316 $step = intval( $step );
317
318 try {
319 $db->begin();
320 $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
321 array( 'keyname' => $key ), __METHOD__, array( 'FOR UPDATE' ) );
322 if ( $row === false ) {
323 // Missing
324 $db->commit();
325 return false;
326 }
327 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
328 if ( $this->isExpired( $row->exptime ) ) {
329 // Expired, do not reinsert
330 $db->commit();
331 return false;
332 }
333
334 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value ) ) );
335 $newValue = $oldValue + $step;
336 $db->insert( 'objectcache',
337 array(
338 'keyname' => $key,
339 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
340 'exptime' => $row->exptime
341 ), __METHOD__ );
342 $db->commit();
343 } catch ( DBQueryError $e ) {
344 $this->handleWriteError( $e );
345 return false;
346 }
347 return $newValue;
348 }
349
350 public function keys() {
351 $db = $this->getDB();
352 $res = $db->select( 'objectcache', array( 'keyname' ), false, __METHOD__ );
353 $result = array();
354 foreach ( $res as $row ) {
355 $result[] = $row->keyname;
356 }
357 return $result;
358 }
359
360 protected function isExpired( $exptime ) {
361 return $exptime != $this->getMaxDateTime() && wfTimestamp( TS_UNIX, $exptime ) < time();
362 }
363
364 protected function getMaxDateTime() {
365 if ( time() > 0x7fffffff ) {
366 return $this->getDB()->timestamp( 1 << 62 );
367 } else {
368 return $this->getDB()->timestamp( 0x7fffffff );
369 }
370 }
371
372 protected function garbageCollect() {
373 /* Ignore 99% of requests */
374 if ( !mt_rand( 0, 100 ) ) {
375 $now = time();
376 /* Avoid repeating the delete within a few seconds */
377 if ( $now > ( $this->lastExpireAll + 1 ) ) {
378 $this->lastExpireAll = $now;
379 $this->expireAll();
380 }
381 }
382 }
383
384 public function expireAll() {
385 $db = $this->getDB();
386 $now = $db->timestamp();
387 try {
388 $db->begin();
389 $db->delete( 'objectcache', array( 'exptime < ' . $db->addQuotes( $now ) ), __METHOD__ );
390 $db->commit();
391 } catch ( DBQueryError $e ) {
392 $this->handleWriteError( $e );
393 }
394 }
395
396 public function deleteAll() {
397 $db = $this->getDB();
398 try {
399 $db->begin();
400 $db->delete( 'objectcache', '*', __METHOD__ );
401 $db->commit();
402 } catch ( DBQueryError $e ) {
403 $this->handleWriteError( $e );
404 }
405 }
406
407 /**
408 * Serialize an object and, if possible, compress the representation.
409 * On typical message and page data, this can provide a 3X decrease
410 * in storage requirements.
411 *
412 * @param $data mixed
413 * @return string
414 */
415 protected function serialize( &$data ) {
416 $serial = serialize( $data );
417 if ( function_exists( 'gzdeflate' ) ) {
418 return gzdeflate( $serial );
419 } else {
420 return $serial;
421 }
422 }
423
424 /**
425 * Unserialize and, if necessary, decompress an object.
426 * @param $serial string
427 * @return mixed
428 */
429 protected function unserialize( $serial ) {
430 if ( function_exists( 'gzinflate' ) ) {
431 $decomp = @gzinflate( $serial );
432 if ( false !== $decomp ) {
433 $serial = $decomp;
434 }
435 }
436 $ret = unserialize( $serial );
437 return $ret;
438 }
439
440 /**
441 * Handle a DBQueryError which occurred during a write operation.
442 * Ignore errors which are due to a read-only database, rethrow others.
443 */
444 protected function handleWriteError( $exception ) {
445 $db = $this->getDB();
446 if ( !$db->wasReadOnlyError() ) {
447 throw $exception;
448 }
449 try {
450 $db->rollback();
451 } catch ( DBQueryError $e ) {
452 }
453 wfDebug( __METHOD__ . ": ignoring query error\n" );
454 $db->ignoreErrors( false );
455 }
456 }
457
458 /**
459 * Backwards compatibility alias
460 */
461 class MediaWikiBagOStuff extends SqlBagOStuff {}
462
463 /**
464 * This is a wrapper for Turck MMCache's shared memory functions.
465 *
466 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
467 * to use a weird custom serializer that randomly segfaults. So we wrap calls
468 * with serialize()/unserialize().
469 *
470 * The thing I noticed about the Turck serialized data was that unlike ordinary
471 * serialize(), it contained the names of methods, and judging by the amount of
472 * binary data, perhaps even the bytecode of the methods themselves. It may be
473 * that Turck's serializer is faster, so a possible future extension would be
474 * to use it for arrays but not for objects.
475 *
476 * @ingroup Cache
477 */
478 class TurckBagOStuff extends BagOStuff {
479 public function get( $key ) {
480 $val = mmcache_get( $key );
481 if ( is_string( $val ) ) {
482 $val = unserialize( $val );
483 }
484 return $val;
485 }
486
487 public function set( $key, $value, $exptime = 0 ) {
488 mmcache_put( $key, serialize( $value ), $exptime );
489 return true;
490 }
491
492 public function delete( $key, $time = 0 ) {
493 mmcache_rm( $key );
494 return true;
495 }
496
497 public function lock( $key, $waitTimeout = 0 ) {
498 mmcache_lock( $key );
499 return true;
500 }
501
502 public function unlock( $key ) {
503 mmcache_unlock( $key );
504 return true;
505 }
506 }
507
508 /**
509 * This is a wrapper for APC's shared memory functions
510 *
511 * @ingroup Cache
512 */
513 class APCBagOStuff extends BagOStuff {
514 public function get( $key ) {
515 $val = apc_fetch( $key );
516 if ( is_string( $val ) ) {
517 $val = unserialize( $val );
518 }
519 return $val;
520 }
521
522 public function set( $key, $value, $exptime = 0 ) {
523 apc_store( $key, serialize( $value ), $exptime );
524 return true;
525 }
526
527 public function delete( $key, $time = 0 ) {
528 apc_delete( $key );
529 return true;
530 }
531 }
532
533
534 /**
535 * This is a wrapper for eAccelerator's shared memory functions.
536 *
537 * This is basically identical to the Turck MMCache version,
538 * mostly because eAccelerator is based on Turck MMCache.
539 *
540 * @ingroup Cache
541 */
542 class eAccelBagOStuff extends BagOStuff {
543 public function get( $key ) {
544 $val = eaccelerator_get( $key );
545 if ( is_string( $val ) ) {
546 $val = unserialize( $val );
547 }
548 return $val;
549 }
550
551 public function set( $key, $value, $exptime = 0 ) {
552 eaccelerator_put( $key, serialize( $value ), $exptime );
553 return true;
554 }
555
556 public function delete( $key, $time = 0 ) {
557 eaccelerator_rm( $key );
558 return true;
559 }
560
561 public function lock( $key, $waitTimeout = 0 ) {
562 eaccelerator_lock( $key );
563 return true;
564 }
565
566 public function unlock( $key ) {
567 eaccelerator_unlock( $key );
568 return true;
569 }
570 }
571
572 /**
573 * Wrapper for XCache object caching functions; identical interface
574 * to the APC wrapper
575 *
576 * @ingroup Cache
577 */
578 class XCacheBagOStuff extends BagOStuff {
579
580 /**
581 * Get a value from the XCache object cache
582 *
583 * @param $key String: cache key
584 * @return mixed
585 */
586 public function get( $key ) {
587 $val = xcache_get( $key );
588 if ( is_string( $val ) )
589 $val = unserialize( $val );
590 return $val;
591 }
592
593 /**
594 * Store a value in the XCache object cache
595 *
596 * @param $key String: cache key
597 * @param $value Mixed: object to store
598 * @param $expire Int: expiration time
599 * @return bool
600 */
601 public function set( $key, $value, $expire = 0 ) {
602 xcache_set( $key, serialize( $value ), $expire );
603 return true;
604 }
605
606 /**
607 * Remove a value from the XCache object cache
608 *
609 * @param $key String: cache key
610 * @param $time Int: not used in this implementation
611 * @return bool
612 */
613 public function delete( $key, $time = 0 ) {
614 xcache_unset( $key );
615 return true;
616 }
617
618 }
619
620 /**
621 * Cache that uses DBA as a backend.
622 * Slow due to the need to constantly open and close the file to avoid holding
623 * writer locks. Intended for development use only, as a memcached workalike
624 * for systems that don't have it.
625 *
626 * @ingroup Cache
627 */
628 class DBABagOStuff extends BagOStuff {
629 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
630
631 public function __construct( $dir = false ) {
632 global $wgDBAhandler;
633 if ( $dir === false ) {
634 global $wgTmpDirectory;
635 $dir = $wgTmpDirectory;
636 }
637 $this->mFile = "$dir/mw-cache-" . wfWikiID();
638 $this->mFile .= '.db';
639 wfDebug( __CLASS__ . ": using cache file {$this->mFile}\n" );
640 $this->mHandler = $wgDBAhandler;
641 }
642
643 /**
644 * Encode value and expiry for storage
645 */
646 function encode( $value, $expiry ) {
647 # Convert to absolute time
648 $expiry = $this->convertExpiry( $expiry );
649 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
650 }
651
652 /**
653 * @return list containing value first and expiry second
654 */
655 function decode( $blob ) {
656 if ( !is_string( $blob ) ) {
657 return array( null, 0 );
658 } else {
659 return array(
660 unserialize( substr( $blob, 11 ) ),
661 intval( substr( $blob, 0, 10 ) )
662 );
663 }
664 }
665
666 function getReader() {
667 if ( file_exists( $this->mFile ) ) {
668 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
669 } else {
670 $handle = $this->getWriter();
671 }
672 if ( !$handle ) {
673 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
674 }
675 return $handle;
676 }
677
678 function getWriter() {
679 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
680 if ( !$handle ) {
681 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
682 }
683 return $handle;
684 }
685
686 function get( $key ) {
687 wfProfileIn( __METHOD__ );
688 wfDebug( __METHOD__ . "($key)\n" );
689 $handle = $this->getReader();
690 if ( !$handle ) {
691 return null;
692 }
693 $val = dba_fetch( $key, $handle );
694 list( $val, $expiry ) = $this->decode( $val );
695 # Must close ASAP because locks are held
696 dba_close( $handle );
697
698 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
699 # Key is expired, delete it
700 $handle = $this->getWriter();
701 dba_delete( $key, $handle );
702 dba_close( $handle );
703 wfDebug( __METHOD__ . ": $key expired\n" );
704 $val = null;
705 }
706 wfProfileOut( __METHOD__ );
707 return $val;
708 }
709
710 function set( $key, $value, $exptime = 0 ) {
711 wfProfileIn( __METHOD__ );
712 wfDebug( __METHOD__ . "($key)\n" );
713 $blob = $this->encode( $value, $exptime );
714 $handle = $this->getWriter();
715 if ( !$handle ) {
716 return false;
717 }
718 $ret = dba_replace( $key, $blob, $handle );
719 dba_close( $handle );
720 wfProfileOut( __METHOD__ );
721 return $ret;
722 }
723
724 function delete( $key, $time = 0 ) {
725 wfProfileIn( __METHOD__ );
726 wfDebug( __METHOD__ . "($key)\n" );
727 $handle = $this->getWriter();
728 if ( !$handle ) {
729 return false;
730 }
731 $ret = dba_delete( $key, $handle );
732 dba_close( $handle );
733 wfProfileOut( __METHOD__ );
734 return $ret;
735 }
736
737 function add( $key, $value, $exptime = 0 ) {
738 wfProfileIn( __METHOD__ );
739 $blob = $this->encode( $value, $exptime );
740 $handle = $this->getWriter();
741 if ( !$handle ) {
742 return false;
743 }
744 $ret = dba_insert( $key, $blob, $handle );
745 # Insert failed, check to see if it failed due to an expired key
746 if ( !$ret ) {
747 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
748 if ( $expiry < time() ) {
749 # Yes expired, delete and try again
750 dba_delete( $key, $handle );
751 $ret = dba_insert( $key, $blob, $handle );
752 # This time if it failed then it will be handled by the caller like any other race
753 }
754 }
755
756 dba_close( $handle );
757 wfProfileOut( __METHOD__ );
758 return $ret;
759 }
760
761 function keys() {
762 $reader = $this->getReader();
763 $k1 = dba_firstkey( $reader );
764 if ( !$k1 ) {
765 return array();
766 }
767 $result[] = $k1;
768 while ( $key = dba_nextkey( $reader ) ) {
769 $result[] = $key;
770 }
771 return $result;
772 }
773 }