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