Merge "Update OOUI to v0.31.1"
[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 BagOStuff {
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 */
51 function __construct( $params = [] ) {
52 parent::__construct( $params );
53
54 $this->token = microtime( true ) . ':' . mt_rand();
55 $this->maxCacheKeys = $params['maxKeys'] ?? INF;
56 if ( $this->maxCacheKeys <= 0 ) {
57 throw new InvalidArgumentException( '$maxKeys parameter must be above zero' );
58 }
59 }
60
61 protected function doGet( $key, $flags = 0 ) {
62 if ( !$this->hasKey( $key ) ) {
63 return false;
64 }
65
66 if ( $this->expire( $key ) ) {
67 return false;
68 }
69
70 // Refresh key position for maxCacheKeys eviction
71 $temp = $this->bag[$key];
72 unset( $this->bag[$key] );
73 $this->bag[$key] = $temp;
74
75 return $this->bag[$key][self::KEY_VAL];
76 }
77
78 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
79 $casToken = null;
80
81 $value = $this->doGet( $key );
82 if ( $value !== false ) {
83 $casToken = $this->bag[$key][self::KEY_CAS];
84 }
85
86 return $value;
87 }
88
89 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
90 // Refresh key position for maxCacheKeys eviction
91 unset( $this->bag[$key] );
92 $this->bag[$key] = [
93 self::KEY_VAL => $value,
94 self::KEY_EXP => $this->convertExpiry( $exptime ),
95 self::KEY_CAS => $this->token . ':' . ++self::$casCounter
96 ];
97
98 if ( count( $this->bag ) > $this->maxCacheKeys ) {
99 reset( $this->bag );
100 $evictKey = key( $this->bag );
101 unset( $this->bag[$evictKey] );
102 }
103
104 return true;
105 }
106
107 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
108 if ( $this->get( $key ) === false ) {
109 return $this->set( $key, $value, $exptime, $flags );
110 }
111
112 return false; // key already set
113 }
114
115 public function delete( $key, $flags = 0 ) {
116 unset( $this->bag[$key] );
117
118 return true;
119 }
120
121 public function incr( $key, $value = 1 ) {
122 $n = $this->get( $key );
123 if ( $this->isInteger( $n ) ) {
124 $n = max( $n + intval( $value ), 0 );
125 $this->bag[$key][self::KEY_VAL] = $n;
126
127 return $n;
128 }
129
130 return false;
131 }
132
133 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
134 return $this->mergeViaCas( $key, $callback, $exptime, $attempts, $flags );
135 }
136
137 /**
138 * Clear all values in cache
139 */
140 public function clear() {
141 $this->bag = [];
142 }
143
144 /**
145 * @param string $key
146 * @return bool
147 */
148 protected function expire( $key ) {
149 $et = $this->bag[$key][self::KEY_EXP];
150 if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) {
151 return false;
152 }
153
154 $this->delete( $key );
155
156 return true;
157 }
158
159 /**
160 * Does this bag have a non-null value for the given key?
161 *
162 * @param string $key
163 * @return bool
164 * @since 1.27
165 */
166 protected function hasKey( $key ) {
167 return isset( $this->bag[$key] );
168 }
169 }