Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / libs / objectcache / HashBagOStuff.php
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 * @ingroup Cache
30 */
31 class HashBagOStuff extends MediumSpecificBagOStuff {
32 /** @var mixed[] */
33 protected $bag = [];
34 /** @var int Max entries allowed */
35 protected $maxCacheKeys;
36
37 /** @var string CAS token prefix for this instance */
38 private $token;
39
40 /** @var int CAS token counter */
41 private static $casCounter = 0;
42
43 const KEY_VAL = 0;
44 const KEY_EXP = 1;
45 const KEY_CAS = 2;
46
47 /**
48 * @param array $params Additional parameters include:
49 * - maxKeys : only allow this many keys (using oldest-first eviction)
50 * @codingStandardsIgnoreStart
51 * @phan-param array{logger?:Psr\Log\LoggerInterface,asyncHandler?:callable,keyspace?:string,reportDupes?:bool,syncTimeout?:int,segmentationSize?:int,segmentedValueMaxSize?:int,maxKeys?:int} $params
52 * @codingStandardsIgnoreEnd
53 */
54 function __construct( $params = [] ) {
55 $params['segmentationSize'] = $params['segmentationSize'] ?? INF;
56 parent::__construct( $params );
57
58 $this->token = microtime( true ) . ':' . mt_rand();
59 $this->maxCacheKeys = $params['maxKeys'] ?? INF;
60 if ( $this->maxCacheKeys <= 0 ) {
61 throw new InvalidArgumentException( '$maxKeys parameter must be above zero' );
62 }
63 }
64
65 protected function doGet( $key, $flags = 0, &$casToken = null ) {
66 $casToken = null;
67
68 if ( !$this->hasKey( $key ) || $this->expire( $key ) ) {
69 return false;
70 }
71
72 // Refresh key position for maxCacheKeys eviction
73 $temp = $this->bag[$key];
74 unset( $this->bag[$key] );
75 $this->bag[$key] = $temp;
76
77 $casToken = $this->bag[$key][self::KEY_CAS];
78
79 return $this->bag[$key][self::KEY_VAL];
80 }
81
82 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
83 // Refresh key position for maxCacheKeys eviction
84 unset( $this->bag[$key] );
85 $this->bag[$key] = [
86 self::KEY_VAL => $value,
87 self::KEY_EXP => $this->getExpirationAsTimestamp( $exptime ),
88 self::KEY_CAS => $this->token . ':' . ++self::$casCounter
89 ];
90
91 if ( count( $this->bag ) > $this->maxCacheKeys ) {
92 reset( $this->bag );
93 $evictKey = key( $this->bag );
94 unset( $this->bag[$evictKey] );
95 }
96
97 return true;
98 }
99
100 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
101 if ( $this->hasKey( $key ) && !$this->expire( $key ) ) {
102 return false; // key already set
103 }
104
105 return $this->doSet( $key, $value, $exptime, $flags );
106 }
107
108 protected function doDelete( $key, $flags = 0 ) {
109 unset( $this->bag[$key] );
110
111 return true;
112 }
113
114 public function incr( $key, $value = 1, $flags = 0 ) {
115 $n = $this->get( $key );
116 if ( $this->isInteger( $n ) ) {
117 $n = max( $n + (int)$value, 0 );
118 $this->bag[$key][self::KEY_VAL] = $n;
119
120 return $n;
121 }
122
123 return false;
124 }
125
126 public function decr( $key, $value = 1, $flags = 0 ) {
127 return $this->incr( $key, -$value, $flags );
128 }
129
130 /**
131 * Clear all values in cache
132 */
133 public function clear() {
134 $this->bag = [];
135 }
136
137 /**
138 * @param string $key
139 * @return bool
140 */
141 protected function expire( $key ) {
142 $et = $this->bag[$key][self::KEY_EXP];
143 if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) {
144 return false;
145 }
146
147 $this->doDelete( $key );
148
149 return true;
150 }
151
152 /**
153 * Does this bag have a non-null value for the given key?
154 *
155 * @param string $key
156 * @return bool
157 * @since 1.27
158 */
159 public function hasKey( $key ) {
160 return isset( $this->bag[$key] );
161 }
162 }