Enable configuration to supply options for Special:Search form
[lhc/web/wiklou.git] / includes / libs / objectcache / APCUBagOStuff.php
1 <?php
2 /**
3 * Object caching using PHP's APCU accelerator.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * This is a wrapper for APCU's shared memory functions
26 *
27 * Use PHP serialization to avoid bugs and easily create CAS tokens.
28 * APCu has a memory corruption bug when the serializer is set to 'default'.
29 * See T120267, and upstream bug reports:
30 * - https://github.com/krakjoe/apcu/issues/38
31 * - https://github.com/krakjoe/apcu/issues/35
32 * - https://github.com/krakjoe/apcu/issues/111
33 *
34 * @ingroup Cache
35 */
36 class APCUBagOStuff extends BagOStuff {
37 /** @var bool Whether to trust the APC implementation to serialization */
38 private $nativeSerialize;
39
40 /**
41 * @var string String to append to each APC key. This may be changed
42 * whenever the handling of values is changed, to prevent existing code
43 * from encountering older values which it cannot handle.
44 */
45 const KEY_SUFFIX = ':4';
46
47 public function __construct( array $params = [] ) {
48 parent::__construct( $params );
49 // The extension serialize is still buggy, unlike "php" and "igbinary"
50 $this->nativeSerialize = ( ini_get( 'apc.serializer' ) !== 'default' );
51 }
52
53 protected function doGet( $key, $flags = 0, &$casToken = null ) {
54 $casToken = null;
55
56 $blob = apcu_fetch( $key . self::KEY_SUFFIX );
57 $value = $this->unserialize( $blob );
58 if ( $value !== false ) {
59 $casToken = $blob; // don't bother hashing this
60 }
61
62 return $value;
63 }
64
65 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
66 return apcu_store(
67 $key . self::KEY_SUFFIX,
68 $this->serialize( $value ),
69 $exptime
70 );
71 }
72
73 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
74 return apcu_add(
75 $key . self::KEY_SUFFIX,
76 $this->serialize( $value ),
77 $exptime
78 );
79 }
80
81 public function delete( $key, $flags = 0 ) {
82 apcu_delete( $key . self::KEY_SUFFIX );
83
84 return true;
85 }
86
87 public function incr( $key, $value = 1 ) {
88 // https://github.com/krakjoe/apcu/issues/166
89 if ( apcu_exists( $key . self::KEY_SUFFIX ) ) {
90 return apcu_inc( $key . self::KEY_SUFFIX, $value );
91 } else {
92 return false;
93 }
94 }
95
96 public function decr( $key, $value = 1 ) {
97 // https://github.com/krakjoe/apcu/issues/166
98 if ( apcu_exists( $key . self::KEY_SUFFIX ) ) {
99 return apcu_dec( $key . self::KEY_SUFFIX, $value );
100 } else {
101 return false;
102 }
103 }
104
105 protected function serialize( $value ) {
106 if ( $this->nativeSerialize ) {
107 return $value;
108 }
109
110 return $this->isInteger( $value ) ? (int)$value : serialize( $value );
111 }
112
113 protected function unserialize( $value ) {
114 if ( $this->nativeSerialize ) {
115 return $value;
116 }
117
118 return $this->isInteger( $value ) ? (int)$value : unserialize( $value );
119 }
120 }