Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.96% |
48 / 49 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
HashBagOStuff | |
97.96% |
48 / 49 |
|
88.89% |
8 / 9 |
24 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
doGet | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
doSet | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
doAdd | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
doDelete | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
doIncrWithInit | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
4.03 | |||
clear | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
expire | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
hasKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Per-process memory cache for storing items. |
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 | * Simple store for keeping values in an associative array for the current process. |
26 | * |
27 | * Data will not persist and is not shared with other processes. |
28 | * |
29 | * @newable |
30 | * @ingroup Cache |
31 | */ |
32 | class HashBagOStuff extends MediumSpecificBagOStuff { |
33 | /** @var mixed[] */ |
34 | protected $bag = []; |
35 | /** @var int|double Max entries allowed, INF for unlimited */ |
36 | protected $maxCacheKeys; |
37 | |
38 | /** @var string CAS token prefix for this instance */ |
39 | private $token; |
40 | |
41 | /** @var int CAS token counter */ |
42 | private static $casCounter = 0; |
43 | |
44 | public const KEY_VAL = 0; |
45 | public const KEY_EXP = 1; |
46 | public const KEY_CAS = 2; |
47 | |
48 | /** |
49 | * @stable to call |
50 | * @param array $params Additional parameters include: |
51 | * - maxKeys : only allow this many keys (using oldest-first eviction) |
52 | * @phpcs:ignore Generic.Files.LineLength |
53 | * @phan-param array{logger?:Psr\Log\LoggerInterface,asyncHandler?:callable,keyspace?:string,reportDupes?:bool,segmentationSize?:int,segmentedValueMaxSize?:int,maxKeys?:int} $params |
54 | */ |
55 | public function __construct( $params = [] ) { |
56 | $params['segmentationSize'] ??= INF; |
57 | parent::__construct( $params ); |
58 | |
59 | $this->token = microtime( true ) . ':' . mt_rand(); |
60 | $maxKeys = $params['maxKeys'] ?? INF; |
61 | if ( $maxKeys !== INF && ( !is_int( $maxKeys ) || $maxKeys <= 0 ) ) { |
62 | throw new InvalidArgumentException( '$maxKeys parameter must be above zero' ); |
63 | } |
64 | $this->maxCacheKeys = $maxKeys; |
65 | |
66 | $this->attrMap[self::ATTR_DURABILITY] = self::QOS_DURABILITY_SCRIPT; |
67 | } |
68 | |
69 | protected function doGet( $key, $flags = 0, &$casToken = null ) { |
70 | $getToken = ( $casToken === self::PASS_BY_REF ); |
71 | $casToken = null; |
72 | |
73 | if ( !$this->hasKey( $key ) || $this->expire( $key ) ) { |
74 | return false; |
75 | } |
76 | |
77 | // Refresh key position for maxCacheKeys eviction |
78 | $temp = $this->bag[$key]; |
79 | unset( $this->bag[$key] ); |
80 | $this->bag[$key] = $temp; |
81 | |
82 | $value = $this->bag[$key][self::KEY_VAL]; |
83 | if ( $getToken && $value !== false ) { |
84 | $casToken = $this->bag[$key][self::KEY_CAS]; |
85 | } |
86 | |
87 | return $value; |
88 | } |
89 | |
90 | protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) { |
91 | // Refresh key position for maxCacheKeys eviction |
92 | unset( $this->bag[$key] ); |
93 | $this->bag[$key] = [ |
94 | self::KEY_VAL => $value, |
95 | self::KEY_EXP => $this->getExpirationAsTimestamp( $exptime ), |
96 | self::KEY_CAS => $this->token . ':' . ++self::$casCounter |
97 | ]; |
98 | |
99 | if ( count( $this->bag ) > $this->maxCacheKeys ) { |
100 | $evictKey = array_key_first( $this->bag ); |
101 | unset( $this->bag[$evictKey] ); |
102 | } |
103 | |
104 | return true; |
105 | } |
106 | |
107 | protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) { |
108 | if ( $this->hasKey( $key ) && !$this->expire( $key ) ) { |
109 | // key already set |
110 | return false; |
111 | } |
112 | |
113 | return $this->doSet( $key, $value, $exptime, $flags ); |
114 | } |
115 | |
116 | protected function doDelete( $key, $flags = 0 ) { |
117 | unset( $this->bag[$key] ); |
118 | |
119 | return true; |
120 | } |
121 | |
122 | protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) { |
123 | $curValue = $this->doGet( $key ); |
124 | if ( $curValue === false ) { |
125 | $newValue = $this->doSet( $key, $init, $exptime ) ? $init : false; |
126 | } elseif ( $this->isInteger( $curValue ) ) { |
127 | $newValue = max( $curValue + $step, 0 ); |
128 | $this->bag[$key][self::KEY_VAL] = $newValue; |
129 | } else { |
130 | $newValue = false; |
131 | } |
132 | |
133 | return $newValue; |
134 | } |
135 | |
136 | /** |
137 | * Clear all values in cache |
138 | */ |
139 | public function clear() { |
140 | $this->bag = []; |
141 | } |
142 | |
143 | /** |
144 | * @param string $key |
145 | * @return bool |
146 | */ |
147 | protected function expire( $key ) { |
148 | $et = $this->bag[$key][self::KEY_EXP]; |
149 | if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) { |
150 | return false; |
151 | } |
152 | |
153 | $this->doDelete( $key ); |
154 | |
155 | return true; |
156 | } |
157 | |
158 | /** |
159 | * Does this bag have a non-null value for the given key? |
160 | * |
161 | * @param string $key |
162 | * @return bool |
163 | * @since 1.27 |
164 | */ |
165 | public function hasKey( $key ) { |
166 | return isset( $this->bag[$key] ); |
167 | } |
168 | } |