3 * Object caching using PHP's APCU 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
25 * This is a wrapper for APCU's shared memory functions
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
36 class APCUBagOStuff
extends BagOStuff
{
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
= ':3';
44 protected function doGet( $key, $flags = 0, &$casToken = null ) {
47 $blob = apcu_fetch( $key . self
::KEY_SUFFIX
);
48 $value = $this->unserialize( $blob );
49 if ( $value !== false ) {
50 $casToken = $blob; // don't bother hashing this
56 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
58 $key . self
::KEY_SUFFIX
,
59 $this->serialize( $value ),
64 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
66 $key . self
::KEY_SUFFIX
,
67 $this->serialize( $value ),
72 public function delete( $key, $flags = 0 ) {
73 apcu_delete( $key . self
::KEY_SUFFIX
);
78 public function incr( $key, $value = 1 ) {
80 * @todo When we only support php 7 or higher remove this hack
82 * https://github.com/krakjoe/apcu/issues/166
84 if ( apcu_exists( $key . self
::KEY_SUFFIX
) ) {
85 return apcu_inc( $key . self
::KEY_SUFFIX
, $value );
91 public function decr( $key, $value = 1 ) {
93 * @todo When we only support php 7 or higher remove this hack
95 * https://github.com/krakjoe/apcu/issues/166
97 if ( apcu_exists( $key . self
::KEY_SUFFIX
) ) {
98 return apcu_dec( $key . self
::KEY_SUFFIX
, $value );
104 protected function serialize( $value ) {
105 return $this->isInteger( $value ) ?
(int)$value : serialize( $value );
108 protected function unserialize( $value ) {
109 return $this->isInteger( $value ) ?
(int)$value : unserialize( $value );