Only store currently-existing categories in the categories table
[mediawiki.git] / includes / libs / objectcache / APCBagOStuff.php
blob8f70fc7a771cd245e2f40bbdf23dcd7bd8b04951
1 <?php
2 /**
3 * Object caching using PHP's APC accelerator.
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.
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.
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
20 * @file
21 * @ingroup Cache
24 /**
25 * This is a wrapper for APC's shared memory functions
27 * @ingroup Cache
29 class APCBagOStuff extends BagOStuff {
31 /**
32 * @var bool If true, trust the APC implementation to serialize and
33 * deserialize objects correctly. If false, (de-)serialize in PHP.
35 protected $nativeSerialize;
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.
42 const KEY_SUFFIX = ':2';
44 /**
45 * Constructor
47 * Available parameters are:
48 * - nativeSerialize: If true, pass objects to apc_store(), and trust it
49 * to serialize them correctly. If false, serialize
50 * all values in PHP.
52 * @param array $params
54 public function __construct( array $params = [] ) {
55 parent::__construct( $params );
57 if ( isset( $params['nativeSerialize'] ) ) {
58 $this->nativeSerialize = $params['nativeSerialize'];
59 } elseif ( extension_loaded( 'apcu' ) && ini_get( 'apc.serializer' ) === 'default' ) {
60 // APCu has a memory corruption bug when the serializer is set to 'default'.
61 // See T120267, and upstream bug reports:
62 // - https://github.com/krakjoe/apcu/issues/38
63 // - https://github.com/krakjoe/apcu/issues/35
64 // - https://github.com/krakjoe/apcu/issues/111
65 $this->logger->warning(
66 'The APCu extension is loaded and the apc.serializer INI setting ' .
67 'is set to "default". This can cause memory corruption! ' .
68 'You should change apc.serializer to "php" instead. ' .
69 'See <https://github.com/krakjoe/apcu/issues/38>.'
71 $this->nativeSerialize = false;
72 } else {
73 $this->nativeSerialize = true;
77 protected function doGet( $key, $flags = 0 ) {
78 $val = apc_fetch( $key . self::KEY_SUFFIX );
80 if ( is_string( $val ) && !$this->nativeSerialize ) {
81 $val = $this->isInteger( $val )
82 ? intval( $val )
83 : unserialize( $val );
86 return $val;
89 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
90 if ( !$this->nativeSerialize && !$this->isInteger( $value ) ) {
91 $value = serialize( $value );
94 apc_store( $key . self::KEY_SUFFIX, $value, $exptime );
96 return true;
99 public function delete( $key ) {
100 apc_delete( $key . self::KEY_SUFFIX );
102 return true;
105 public function incr( $key, $value = 1 ) {
106 return apc_inc( $key . self::KEY_SUFFIX, $value );
109 public function decr( $key, $value = 1 ) {
110 return apc_dec( $key . self::KEY_SUFFIX, $value );