Merge "Support more coupled DBs in AtomicSectionUpdate/AutoCommitUpdate"
[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 * @ingroup Cache
28 */
29 class APCUBagOStuff extends APCBagOStuff {
30 /**
31 * Available parameters are:
32 * - nativeSerialize: If true, pass objects to apcu_store(), and trust it
33 * to serialize them correctly. If false, serialize
34 * all values in PHP.
35 *
36 * @param array $params
37 */
38 public function __construct( array $params = [] ) {
39 parent::__construct( $params );
40 }
41
42 protected function doGet( $key, $flags = 0 ) {
43 return $this->unserialize( apcu_fetch( $key . self::KEY_SUFFIX ) );
44 }
45
46 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
47 $casToken = null;
48
49 $blob = apcu_fetch( $key . self::KEY_SUFFIX );
50 $value = $this->unserialize( $blob );
51 if ( $value !== false ) {
52 $casToken = $blob; // don't bother hashing this
53 }
54
55 return $value;
56 }
57
58 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
59 apcu_store(
60 $key . self::KEY_SUFFIX,
61 $this->serialize( $value ),
62 $exptime
63 );
64
65 return true;
66 }
67
68 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
69 return apcu_add(
70 $key . self::KEY_SUFFIX,
71 $this->serialize( $value ),
72 $exptime
73 );
74 }
75
76 public function delete( $key, $flags = 0 ) {
77 apcu_delete( $key . self::KEY_SUFFIX );
78
79 return true;
80 }
81
82 public function incr( $key, $value = 1 ) {
83 /**
84 * @todo When we only support php 7 or higher remove this hack
85 *
86 * https://github.com/krakjoe/apcu/issues/166
87 */
88 if ( apcu_exists( $key . self::KEY_SUFFIX ) ) {
89 return apcu_inc( $key . self::KEY_SUFFIX, $value );
90 } else {
91 return false;
92 }
93 }
94
95 public function decr( $key, $value = 1 ) {
96 /**
97 * @todo When we only support php 7 or higher remove this hack
98 *
99 * https://github.com/krakjoe/apcu/issues/166
100 */
101 if ( apcu_exists( $key . self::KEY_SUFFIX ) ) {
102 return apcu_dec( $key . self::KEY_SUFFIX, $value );
103 } else {
104 return false;
105 }
106 }
107
108 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
109 return $this->mergeViaCas( $key, $callback, $exptime, $attempts, $flags );
110 }
111 }