3 * Per-process memory cache for storing items.
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.
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.
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
23 use Wikimedia\Assert\Assert
;
26 * Handles a simple LRU key/value map with a maximum number of entries
28 * Use ProcessCacheLRU if hierarchical purging is needed or objects can become stale
30 * @see ProcessCacheLRU
36 protected $cache = []; // (key => value)
38 protected $maxCacheKeys; // integer; max entries
41 * @param int $maxKeys Maximum number of entries allowed (min 1).
42 * @throws Exception When $maxCacheKeys is not an int or not above zero.
44 public function __construct( $maxKeys ) {
45 Assert
::parameterType( 'integer', $maxKeys, '$maxKeys' );
46 Assert
::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
48 $this->maxCacheKeys
= $maxKeys;
52 * @param array $values Key/value map in order of increasingly recent access
57 public static function newFromArray( array $values, $maxKeys ) {
58 $mapCache = new self( $maxKeys );
59 $mapCache->cache
= ( count( $values ) > $maxKeys )
60 ?
array_slice( $values, -$maxKeys, null, true )
67 * @return array Key/value map in order of increasingly recent access
70 public function toArray() {
75 * Set a key/value pair.
76 * This will prune the cache if it gets too large based on LRU.
77 * If the item is already set, it will be pushed to the top of the cache.
79 * To reduce evictions due to one-off use of many new keys, $rank can be
80 * set to have keys start at the top of a bottom fraction of the list. A value
81 * of 3/8 means values start at the top of the bottom 3/8s of the list and are
82 * moved to the top of the list when accessed a second time.
86 * @param float $rank Bottom fraction of the list where keys start off [Default: 1.0]
89 public function set( $key, $value, $rank = 1.0 ) {
90 if ( $this->has( $key ) ) {
92 } elseif ( count( $this->cache
) >= $this->maxCacheKeys
) {
93 reset( $this->cache
);
94 $evictKey = key( $this->cache
);
95 unset( $this->cache
[$evictKey] );
98 if ( $rank < 1.0 && $rank > 0 ) {
99 $offset = intval( $rank * count( $this->cache
) );
100 $this->cache
= array_slice( $this->cache
, 0, $offset, true )
102 +
array_slice( $this->cache
, $offset, null, true );
104 $this->cache
[$key] = $value;
109 * Check if a key exists
114 public function has( $key ) {
115 if ( !is_int( $key ) && !is_string( $key ) ) {
116 throw new UnexpectedValueException(
117 __METHOD__
. ' called with invalid key. Must be string or integer.' );
119 return array_key_exists( $key, $this->cache
);
123 * Get the value for a key.
124 * This returns null if the key is not set.
125 * If the item is already set, it will be pushed to the top of the cache.
128 * @return mixed Returns null if the key was not found
130 public function get( $key ) {
131 if ( !$this->has( $key ) ) {
137 return $this->cache
[$key];
144 public function getAllKeys() {
145 return array_keys( $this->cache
);
149 * Get an item with the given key, producing and setting it if not found.
151 * If the callback returns false, then nothing is stored.
155 * @param callable $callback Callback that will produce the value
156 * @param float $rank Bottom fraction of the list where keys start off [Default: 1.0]
157 * @return mixed The cached value if found or the result of $callback otherwise
159 public function getWithSetCallback( $key, callable
$callback, $rank = 1.0 ) {
160 if ( $this->has( $key ) ) {
161 $value = $this->get( $key );
163 $value = call_user_func( $callback );
164 if ( $value !== false ) {
165 $this->set( $key, $value, $rank );
173 * Clear one or several cache entries, or all cache entries
175 * @param string|array $keys
178 public function clear( $keys = null ) {
179 if ( $keys === null ) {
182 foreach ( (array)$keys as $key ) {
183 unset( $this->cache
[$key] );
189 * Push an entry to the top of the cache
193 protected function ping( $key ) {
194 $item = $this->cache
[$key];
195 unset( $this->cache
[$key] );
196 $this->cache
[$key] = $item;