objectcache: remove redundant merge() definitions
[lhc/web/wiklou.git] / includes / libs / objectcache / APCBagOStuff.php
1 <?php
2 /**
3 * Object caching using PHP's APC 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 APC's shared memory functions
26 *
27 * @ingroup Cache
28 */
29 class APCBagOStuff extends BagOStuff {
30
31 /**
32 * @var bool If true, trust the APC implementation to serialize and
33 * deserialize objects correctly. If false, (de-)serialize in PHP.
34 */
35 protected $nativeSerialize;
36
37 /**
38 * @var string String to append to each APC key. This may be changed
39 * whenever the handling of values is changed, to prevent existing code
40 * from encountering older values which it cannot handle.
41 */
42 const KEY_SUFFIX = ':2';
43
44 /**
45 * Available parameters are:
46 * - nativeSerialize: If true, pass objects to apc_store(), and trust it
47 * to serialize them correctly. If false, serialize
48 * all values in PHP.
49 *
50 * @param array $params
51 */
52 public function __construct( array $params = [] ) {
53 parent::__construct( $params );
54
55 if ( isset( $params['nativeSerialize'] ) ) {
56 $this->nativeSerialize = $params['nativeSerialize'];
57 } elseif ( extension_loaded( 'apcu' ) && ini_get( 'apc.serializer' ) === 'default' ) {
58 // APCu has a memory corruption bug when the serializer is set to 'default'.
59 // See T120267, and upstream bug reports:
60 // - https://github.com/krakjoe/apcu/issues/38
61 // - https://github.com/krakjoe/apcu/issues/35
62 // - https://github.com/krakjoe/apcu/issues/111
63 $this->logger->warning(
64 'The APCu extension is loaded and the apc.serializer INI setting ' .
65 'is set to "default". This can cause memory corruption! ' .
66 'You should change apc.serializer to "php" instead. ' .
67 'See <https://github.com/krakjoe/apcu/issues/38>.'
68 );
69 $this->nativeSerialize = false;
70 } else {
71 $this->nativeSerialize = true;
72 }
73 }
74
75 protected function doGet( $key, $flags = 0 ) {
76 return $this->unserialize( apc_fetch( $key . self::KEY_SUFFIX ) );
77 }
78
79 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
80 $casToken = null;
81
82 $blob = apc_fetch( $key . self::KEY_SUFFIX );
83 $value = $this->unserialize( $blob );
84 if ( $value !== false ) {
85 $casToken = $blob; // don't bother hashing this
86 }
87
88 return $value;
89 }
90
91 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
92 apc_store(
93 $key . self::KEY_SUFFIX,
94 $this->serialize( $value ),
95 $exptime
96 );
97
98 return true;
99 }
100
101 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
102 return apc_add(
103 $key . self::KEY_SUFFIX,
104 $this->serialize( $value ),
105 $exptime
106 );
107 }
108
109 public function delete( $key, $flags = 0 ) {
110 apc_delete( $key . self::KEY_SUFFIX );
111
112 return true;
113 }
114
115 public function incr( $key, $value = 1 ) {
116 return apc_inc( $key . self::KEY_SUFFIX, $value );
117 }
118
119 public function decr( $key, $value = 1 ) {
120 return apc_dec( $key . self::KEY_SUFFIX, $value );
121 }
122
123 protected function serialize( $value ) {
124 if ( !$this->nativeSerialize && !$this->isInteger( $value ) ) {
125 $value = serialize( $value );
126 }
127 return $value;
128 }
129
130 protected function unserialize( $value ) {
131 if ( is_string( $value ) && !$this->nativeSerialize ) {
132 $value = $this->isInteger( $value )
133 ? intval( $value )
134 : unserialize( $value );
135 }
136 return $value;
137 }
138 }