Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / includes / libs / MapCacheLRU.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 use Wikimedia\Assert\Assert;
24
25 /**
26 * Handles a simple LRU key/value map with a maximum number of entries
27 *
28 * Use ProcessCacheLRU if hierarchical purging is needed or objects can become stale
29 *
30 * @see ProcessCacheLRU
31 * @ingroup Cache
32 * @since 1.23
33 */
34 class MapCacheLRU {
35 /** @var array */
36 protected $cache = []; // (key => value)
37
38 protected $maxCacheKeys; // integer; max entries
39
40 /**
41 * @param int $maxKeys Maximum number of entries allowed (min 1).
42 * @throws Exception When $maxCacheKeys is not an int or not above zero.
43 */
44 public function __construct( $maxKeys ) {
45 Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
46 Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
47
48 $this->maxCacheKeys = $maxKeys;
49 }
50
51 /**
52 * Set a key/value pair.
53 * This will prune the cache if it gets too large based on LRU.
54 * If the item is already set, it will be pushed to the top of the cache.
55 *
56 * @param string $key
57 * @param mixed $value
58 * @return void
59 */
60 public function set( $key, $value ) {
61 if ( $this->has( $key ) ) {
62 $this->ping( $key );
63 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
64 reset( $this->cache );
65 $evictKey = key( $this->cache );
66 unset( $this->cache[$evictKey] );
67 }
68 $this->cache[$key] = $value;
69 }
70
71 /**
72 * Check if a key exists
73 *
74 * @param string $key
75 * @return bool
76 */
77 public function has( $key ) {
78 if ( !is_int( $key ) && !is_string( $key ) ) {
79 throw new MWException( __METHOD__ . ' called with invalid key. Must be string or integer.' );
80 }
81 return array_key_exists( $key, $this->cache );
82 }
83
84 /**
85 * Get the value for a key.
86 * This returns null if the key is not set.
87 * If the item is already set, it will be pushed to the top of the cache.
88 *
89 * @param string $key
90 * @return mixed Returns null if the key was not found
91 */
92 public function get( $key ) {
93 if ( !$this->has( $key ) ) {
94 return null;
95 }
96
97 $this->ping( $key );
98
99 return $this->cache[$key];
100 }
101
102 /**
103 * @return array
104 * @since 1.25
105 */
106 public function getAllKeys() {
107 return array_keys( $this->cache );
108 }
109
110 /**
111 * Get an item with the given key, producing and setting it if not found.
112 *
113 * If the callback returns false, then nothing is stored.
114 *
115 * @since 1.28
116 * @param string $key
117 * @param callable $callback Callback that will produce the value
118 * @return mixed The cached value if found or the result of $callback otherwise
119 */
120 public function getWithSetCallback( $key, callable $callback ) {
121 if ( $this->has( $key ) ) {
122 $value = $this->get( $key );
123 } else {
124 $value = call_user_func( $callback );
125 if ( $value !== false ) {
126 $this->set( $key, $value );
127 }
128 }
129
130 return $value;
131 }
132
133 /**
134 * Clear one or several cache entries, or all cache entries
135 *
136 * @param string|array $keys
137 * @return void
138 */
139 public function clear( $keys = null ) {
140 if ( $keys === null ) {
141 $this->cache = [];
142 } else {
143 foreach ( (array)$keys as $key ) {
144 unset( $this->cache[$key] );
145 }
146 }
147 }
148
149 /**
150 * Push an entry to the top of the cache
151 *
152 * @param string $key
153 */
154 protected function ping( $key ) {
155 $item = $this->cache[$key];
156 unset( $this->cache[$key] );
157 $this->cache[$key] = $item;
158 }
159 }