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