|
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
|
|
|
* Constructor |
|
32
|
|
|
* |
|
33
|
|
|
* Available parameters are: |
|
34
|
|
|
* - nativeSerialize: If true, pass objects to apcu_store(), and trust it |
|
35
|
|
|
* to serialize them correctly. If false, serialize |
|
36
|
|
|
* all values in PHP. |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $params |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct( array $params = [] ) { |
|
41
|
|
|
parent::__construct( $params ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function doGet( $key, $flags = 0 ) { |
|
45
|
|
|
return $this->getUnserialize( |
|
46
|
|
|
apcu_fetch( $key . self::KEY_SUFFIX ) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function set( $key, $value, $exptime = 0, $flags = 0 ) { |
|
51
|
|
|
apcu_store( |
|
52
|
|
|
$key . self::KEY_SUFFIX, |
|
53
|
|
|
$this->setSerialize( $value ), |
|
54
|
|
|
$exptime |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function delete( $key ) { |
|
61
|
|
|
apcu_delete( $key . self::KEY_SUFFIX ); |
|
62
|
|
|
|
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
View Code Duplication |
public function incr( $key, $value = 1 ) { |
|
67
|
|
|
/** |
|
68
|
|
|
* @todo When we only support php 7 or higher remove this hack |
|
69
|
|
|
* |
|
70
|
|
|
* https://github.com/krakjoe/apcu/issues/166 |
|
71
|
|
|
*/ |
|
72
|
|
|
if ( apcu_exists( $key . self::KEY_SUFFIX ) ) { |
|
73
|
|
|
return apcu_inc( $key . self::KEY_SUFFIX, $value ); |
|
74
|
|
|
} else { |
|
75
|
|
|
return apcu_set( $key . self::KEY_SUFFIX, $value ); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
View Code Duplication |
public function decr( $key, $value = 1 ) { |
|
80
|
|
|
/** |
|
81
|
|
|
* @todo When we only support php 7 or higher remove this hack |
|
82
|
|
|
* |
|
83
|
|
|
* https://github.com/krakjoe/apcu/issues/166 |
|
84
|
|
|
*/ |
|
85
|
|
|
if ( apcu_exists( $key . self::KEY_SUFFIX ) ) { |
|
86
|
|
|
return apcu_dec( $key . self::KEY_SUFFIX, $value ); |
|
87
|
|
|
} else { |
|
88
|
|
|
return apcu_set( $key . self::KEY_SUFFIX, -$value ); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|