Fix indentation of => added in r77366 to be in step with the rest
[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 yo 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 global $wgDBtype;
244
245 if ( !isset( $this->db ) ) {
246 /* We must keep a separate connection to MySQL in order to avoid deadlocks
247 * However, SQLite has an opposite behaviour.
248 * @todo Investigate behaviour for other databases
249 */
250 if ( $wgDBtype == 'sqlite' ) {
251 $this->db = wfGetDB( DB_MASTER );
252 } else {
253 $this->lb = wfGetLBFactory()->newMainLB();
254 $this->db = $this->lb->getConnection( DB_MASTER );
255 $this->db->clearFlag( DBO_TRX );
256 }
257 }
258
259 return $this->db;
260 }
261
262 public function get( $key ) {
263 # expire old entries if any
264 $this->garbageCollect();
265 $db = $this->getDB();
266 $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
267 array( 'keyname' => $key ), __METHOD__ );
268
269 if ( !$row ) {
270 $this->debug( 'get: no matching rows' );
271 return false;
272 }
273
274 $this->debug( "get: retrieved data; expiry time is " . $row->exptime );
275
276 if ( $this->isExpired( $row->exptime ) ) {
277 $this->debug( "get: key has expired, deleting" );
278 try {
279 $db->begin();
280 # Put the expiry time in the WHERE condition to avoid deleting a
281 # newly-inserted value
282 $db->delete( 'objectcache',
283 array(
284 'keyname' => $key,
285 'exptime' => $row->exptime
286 ), __METHOD__ );
287 $db->commit();
288 } catch ( DBQueryError $e ) {
289 $this->handleWriteError( $e );
290 }
291
292 return false;
293 }
294
295 return $this->unserialize( $db->decodeBlob( $row->value ) );
296 }
297
298 public function set( $key, $value, $exptime = 0 ) {
299 $db = $this->getDB();
300 $exptime = intval( $exptime );
301
302 if ( $exptime < 0 ) {
303 $exptime = 0;
304 }
305
306 if ( $exptime == 0 ) {
307 $encExpiry = $this->getMaxDateTime();
308 } else {
309 if ( $exptime < 3.16e8 ) { # ~10 years
310 $exptime += time();
311 }
312
313 $encExpiry = $db->timestamp( $exptime );
314 }
315 try {
316 $db->begin();
317 // (bug 24425) use a replace if the db supports it instead of
318 // delete/insert to avoid clashes with conflicting keynames
319 $db->replace( 'objectcache', array( 'keyname' ),
320 array(
321 'keyname' => $key,
322 'value' => $db->encodeBlob( $this->serialize( $value ) ),
323 'exptime' => $encExpiry
324 ), __METHOD__ );
325 $db->commit();
326 } catch ( DBQueryError $e ) {
327 $this->handleWriteError( $e );
328
329 return false;
330 }
331
332 return true;
333 }
334
335 public function delete( $key, $time = 0 ) {
336 $db = $this->getDB();
337
338 try {
339 $db->begin();
340 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
341 $db->commit();
342 } catch ( DBQueryError $e ) {
343 $this->handleWriteError( $e );
344
345 return false;
346 }
347
348 return true;
349 }
350
351 public function incr( $key, $step = 1 ) {
352 $db = $this->getDB();
353 $step = intval( $step );
354
355 try {
356 $db->begin();
357 $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
358 array( 'keyname' => $key ), __METHOD__, array( 'FOR UPDATE' ) );
359 if ( $row === false ) {
360 // Missing
361 $db->commit();
362
363 return null;
364 }
365 $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
366 if ( $this->isExpired( $row->exptime ) ) {
367 // Expired, do not reinsert
368 $db->commit();
369
370 return null;
371 }
372
373 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value ) ) );
374 $newValue = $oldValue + $step;
375 $db->insert( 'objectcache',
376 array(
377 'keyname' => $key,
378 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
379 'exptime' => $row->exptime
380 ), __METHOD__ );
381 $db->commit();
382 } catch ( DBQueryError $e ) {
383 $this->handleWriteError( $e );
384
385 return null;
386 }
387
388 return $newValue;
389 }
390
391 public function keys() {
392 $db = $this->getDB();
393 $res = $db->select( 'objectcache', array( 'keyname' ), false, __METHOD__ );
394 $result = array();
395
396 foreach ( $res as $row ) {
397 $result[] = $row->keyname;
398 }
399
400 return $result;
401 }
402
403 protected function isExpired( $exptime ) {
404 return $exptime != $this->getMaxDateTime() && wfTimestamp( TS_UNIX, $exptime ) < time();
405 }
406
407 protected function getMaxDateTime() {
408 if ( time() > 0x7fffffff ) {
409 return $this->getDB()->timestamp( 1 << 62 );
410 } else {
411 return $this->getDB()->timestamp( 0x7fffffff );
412 }
413 }
414
415 protected function garbageCollect() {
416 /* Ignore 99% of requests */
417 if ( !mt_rand( 0, 100 ) ) {
418 $now = time();
419 /* Avoid repeating the delete within a few seconds */
420 if ( $now > ( $this->lastExpireAll + 1 ) ) {
421 $this->lastExpireAll = $now;
422 $this->expireAll();
423 }
424 }
425 }
426
427 public function expireAll() {
428 $db = $this->getDB();
429 $now = $db->timestamp();
430
431 try {
432 $db->begin();
433 $db->delete( 'objectcache', array( 'exptime < ' . $db->addQuotes( $now ) ), __METHOD__ );
434 $db->commit();
435 } catch ( DBQueryError $e ) {
436 $this->handleWriteError( $e );
437 }
438 }
439
440 public function deleteAll() {
441 $db = $this->getDB();
442
443 try {
444 $db->begin();
445 $db->delete( 'objectcache', '*', __METHOD__ );
446 $db->commit();
447 } catch ( DBQueryError $e ) {
448 $this->handleWriteError( $e );
449 }
450 }
451
452 /**
453 * Serialize an object and, if possible, compress the representation.
454 * On typical message and page data, this can provide a 3X decrease
455 * in storage requirements.
456 *
457 * @param $data mixed
458 * @return string
459 */
460 protected function serialize( &$data ) {
461 $serial = serialize( $data );
462
463 if ( function_exists( 'gzdeflate' ) ) {
464 return gzdeflate( $serial );
465 } else {
466 return $serial;
467 }
468 }
469
470 /**
471 * Unserialize and, if necessary, decompress an object.
472 * @param $serial string
473 * @return mixed
474 */
475 protected function unserialize( $serial ) {
476 if ( function_exists( 'gzinflate' ) ) {
477 $decomp = @gzinflate( $serial );
478
479 if ( false !== $decomp ) {
480 $serial = $decomp;
481 }
482 }
483
484 $ret = unserialize( $serial );
485
486 return $ret;
487 }
488
489 /**
490 * Handle a DBQueryError which occurred during a write operation.
491 * Ignore errors which are due to a read-only database, rethrow others.
492 */
493 protected function handleWriteError( $exception ) {
494 $db = $this->getDB();
495
496 if ( !$db->wasReadOnlyError() ) {
497 throw $exception;
498 }
499
500 try {
501 $db->rollback();
502 } catch ( DBQueryError $e ) {
503 }
504
505 wfDebug( __METHOD__ . ": ignoring query error\n" );
506 $db->ignoreErrors( false );
507 }
508 }
509
510 /**
511 * Backwards compatibility alias
512 */
513 class MediaWikiBagOStuff extends SqlBagOStuff { }
514
515 /**
516 * This is a wrapper for APC's shared memory functions
517 *
518 * @ingroup Cache
519 */
520 class APCBagOStuff extends BagOStuff {
521 public function get( $key ) {
522 $val = apc_fetch( $key );
523
524 if ( is_string( $val ) ) {
525 $val = unserialize( $val );
526 }
527
528 return $val;
529 }
530
531 public function set( $key, $value, $exptime = 0 ) {
532 apc_store( $key, serialize( $value ), $exptime );
533
534 return true;
535 }
536
537 public function delete( $key, $time = 0 ) {
538 apc_delete( $key );
539
540 return true;
541 }
542
543 public function keys() {
544 $info = apc_cache_info( 'user' );
545 $list = $info['cache_list'];
546 $keys = array();
547
548 foreach ( $list as $entry ) {
549 $keys[] = $entry['info'];
550 }
551
552 return $keys;
553 }
554 }
555
556 /**
557 * This is a wrapper for eAccelerator's shared memory functions.
558 *
559 * This is basically identical to the deceased Turck MMCache version,
560 * mostly because eAccelerator is based on Turck MMCache.
561 *
562 * @ingroup Cache
563 */
564 class eAccelBagOStuff extends BagOStuff {
565 public function get( $key ) {
566 $val = eaccelerator_get( $key );
567
568 if ( is_string( $val ) ) {
569 $val = unserialize( $val );
570 }
571
572 return $val;
573 }
574
575 public function set( $key, $value, $exptime = 0 ) {
576 eaccelerator_put( $key, serialize( $value ), $exptime );
577
578 return true;
579 }
580
581 public function delete( $key, $time = 0 ) {
582 eaccelerator_rm( $key );
583
584 return true;
585 }
586
587 public function lock( $key, $waitTimeout = 0 ) {
588 eaccelerator_lock( $key );
589
590 return true;
591 }
592
593 public function unlock( $key ) {
594 eaccelerator_unlock( $key );
595
596 return true;
597 }
598 }
599
600 /**
601 * Wrapper for XCache object caching functions; identical interface
602 * to the APC wrapper
603 *
604 * @ingroup Cache
605 */
606 class XCacheBagOStuff extends BagOStuff {
607 /**
608 * Get a value from the XCache object cache
609 *
610 * @param $key String: cache key
611 * @return mixed
612 */
613 public function get( $key ) {
614 $val = xcache_get( $key );
615
616 if ( is_string( $val ) ) {
617 $val = unserialize( $val );
618 }
619
620 return $val;
621 }
622
623 /**
624 * Store a value in the XCache object cache
625 *
626 * @param $key String: cache key
627 * @param $value Mixed: object to store
628 * @param $expire Int: expiration time
629 * @return bool
630 */
631 public function set( $key, $value, $expire = 0 ) {
632 xcache_set( $key, serialize( $value ), $expire );
633
634 return true;
635 }
636
637 /**
638 * Remove a value from the XCache object cache
639 *
640 * @param $key String: cache key
641 * @param $time Int: not used in this implementation
642 * @return bool
643 */
644 public function delete( $key, $time = 0 ) {
645 xcache_unset( $key );
646
647 return true;
648 }
649 }
650
651 /**
652 * Cache that uses DBA as a backend.
653 * Slow due to the need to constantly open and close the file to avoid holding
654 * writer locks. Intended for development use only, as a memcached workalike
655 * for systems that don't have it.
656 *
657 * @ingroup Cache
658 */
659 class DBABagOStuff extends BagOStuff {
660 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
661
662 public function __construct( $dir = false ) {
663 global $wgDBAhandler;
664
665 if ( $dir === false ) {
666 global $wgTmpDirectory;
667 $dir = $wgTmpDirectory;
668 }
669
670 $this->mFile = "$dir/mw-cache-" . wfWikiID();
671 $this->mFile .= '.db';
672 wfDebug( __CLASS__ . ": using cache file {$this->mFile}\n" );
673 $this->mHandler = $wgDBAhandler;
674 }
675
676 /**
677 * Encode value and expiry for storage
678 */
679 function encode( $value, $expiry ) {
680 # Convert to absolute time
681 $expiry = $this->convertExpiry( $expiry );
682
683 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
684 }
685
686 /**
687 * @return list containing value first and expiry second
688 */
689 function decode( $blob ) {
690 if ( !is_string( $blob ) ) {
691 return array( null, 0 );
692 } else {
693 return array(
694 unserialize( substr( $blob, 11 ) ),
695 intval( substr( $blob, 0, 10 ) )
696 );
697 }
698 }
699
700 function getReader() {
701 if ( file_exists( $this->mFile ) ) {
702 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
703 } else {
704 $handle = $this->getWriter();
705 }
706
707 if ( !$handle ) {
708 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
709 }
710
711 return $handle;
712 }
713
714 function getWriter() {
715 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
716
717 if ( !$handle ) {
718 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
719 }
720
721 return $handle;
722 }
723
724 function get( $key ) {
725 wfProfileIn( __METHOD__ );
726 wfDebug( __METHOD__ . "($key)\n" );
727
728 $handle = $this->getReader();
729 if ( !$handle ) {
730 return null;
731 }
732
733 $val = dba_fetch( $key, $handle );
734 list( $val, $expiry ) = $this->decode( $val );
735
736 # Must close ASAP because locks are held
737 dba_close( $handle );
738
739 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
740 # Key is expired, delete it
741 $handle = $this->getWriter();
742 dba_delete( $key, $handle );
743 dba_close( $handle );
744 wfDebug( __METHOD__ . ": $key expired\n" );
745 $val = null;
746 }
747
748 wfProfileOut( __METHOD__ );
749 return $val;
750 }
751
752 function set( $key, $value, $exptime = 0 ) {
753 wfProfileIn( __METHOD__ );
754 wfDebug( __METHOD__ . "($key)\n" );
755
756 $blob = $this->encode( $value, $exptime );
757
758 $handle = $this->getWriter();
759 if ( !$handle ) {
760 return false;
761 }
762
763 $ret = dba_replace( $key, $blob, $handle );
764 dba_close( $handle );
765
766 wfProfileOut( __METHOD__ );
767 return $ret;
768 }
769
770 function delete( $key, $time = 0 ) {
771 wfProfileIn( __METHOD__ );
772 wfDebug( __METHOD__ . "($key)\n" );
773
774 $handle = $this->getWriter();
775 if ( !$handle ) {
776 return false;
777 }
778
779 $ret = dba_delete( $key, $handle );
780 dba_close( $handle );
781
782 wfProfileOut( __METHOD__ );
783 return $ret;
784 }
785
786 function add( $key, $value, $exptime = 0 ) {
787 wfProfileIn( __METHOD__ );
788
789 $blob = $this->encode( $value, $exptime );
790
791 $handle = $this->getWriter();
792
793 if ( !$handle ) {
794 return false;
795 }
796
797 $ret = dba_insert( $key, $blob, $handle );
798
799 # Insert failed, check to see if it failed due to an expired key
800 if ( !$ret ) {
801 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
802
803 if ( $expiry < time() ) {
804 # Yes expired, delete and try again
805 dba_delete( $key, $handle );
806 $ret = dba_insert( $key, $blob, $handle );
807 # This time if it failed then it will be handled by the caller like any other race
808 }
809 }
810
811 dba_close( $handle );
812
813 wfProfileOut( __METHOD__ );
814 return $ret;
815 }
816
817 function keys() {
818 $reader = $this->getReader();
819 $k1 = dba_firstkey( $reader );
820
821 if ( !$k1 ) {
822 return array();
823 }
824
825 $result[] = $k1;
826
827 while ( $key = dba_nextkey( $reader ) ) {
828 $result[] = $key;
829 }
830
831 return $result;
832 }
833 }
834
835 /**
836 * Wrapper for WinCache object caching functions; identical interface
837 * to the APC wrapper
838 *
839 * @ingroup Cache
840 */
841 class WinCacheBagOStuff extends BagOStuff {
842
843 /**
844 * Get a value from the WinCache object cache
845 *
846 * @param $key String: cache key
847 * @return mixed
848 */
849 public function get( $key ) {
850 $val = wincache_ucache_get( $key );
851
852 if ( is_string( $val ) ) {
853 $val = unserialize( $val );
854 }
855
856 return $val;
857 }
858
859 /**
860 * Store a value in the WinCache object cache
861 *
862 * @param $key String: cache key
863 * @param $value Mixed: object to store
864 * @param $expire Int: expiration time
865 * @return bool
866 */
867 public function set( $key, $value, $expire = 0 ) {
868 wincache_ucache_set( $key, serialize( $value ), $expire );
869
870 return true;
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 foreach ( $list as $entry ) {
892 $keys[] = $entry['key_name'];
893 }
894
895 return $keys;
896 }
897 }