Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[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, &$casToken = null ) {
62 $casToken = null;
63
64 if ( !$this->hasKey( $key ) || $this->expire( $key ) ) {
65 return false;
66 }
67
68 // Refresh key position for maxCacheKeys eviction
69 $temp = $this->bag[$key];
70 unset( $this->bag[$key] );
71 $this->bag[$key] = $temp;
72
73 $casToken = $this->bag[$key][self::KEY_CAS];
74
75 return $this->bag[$key][self::KEY_VAL];
76 }
77
78 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
79 // Refresh key position for maxCacheKeys eviction
80 unset( $this->bag[$key] );
81 $this->bag[$key] = [
82 self::KEY_VAL => $value,
83 self::KEY_EXP => $this->convertToExpiry( $exptime ),
84 self::KEY_CAS => $this->token . ':' . ++self::$casCounter
85 ];
86
87 if ( count( $this->bag ) > $this->maxCacheKeys ) {
88 reset( $this->bag );
89 $evictKey = key( $this->bag );
90 unset( $this->bag[$evictKey] );
91 }
92
93 return true;
94 }
95
96 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
97 if ( $this->get( $key ) === false ) {
98 return $this->set( $key, $value, $exptime, $flags );
99 }
100
101 return false; // key already set
102 }
103
104 public function delete( $key, $flags = 0 ) {
105 unset( $this->bag[$key] );
106
107 return true;
108 }
109
110 public function incr( $key, $value = 1 ) {
111 $n = $this->get( $key );
112 if ( $this->isInteger( $n ) ) {
113 $n = max( $n + intval( $value ), 0 );
114 $this->bag[$key][self::KEY_VAL] = $n;
115
116 return $n;
117 }
118
119 return false;
120 }
121
122 /**
123 * Clear all values in cache
124 */
125 public function clear() {
126 $this->bag = [];
127 }
128
129 /**
130 * @param string $key
131 * @return bool
132 */
133 protected function expire( $key ) {
134 $et = $this->bag[$key][self::KEY_EXP];
135 if ( $et == self::TTL_INDEFINITE || $et > $this->getCurrentTime() ) {
136 return false;
137 }
138
139 $this->delete( $key );
140
141 return true;
142 }
143
144 /**
145 * Does this bag have a non-null value for the given key?
146 *
147 * @param string $key
148 * @return bool
149 * @since 1.27
150 */
151 protected function hasKey( $key ) {
152 return isset( $this->bag[$key] );
153 }
154 }