DatabaseMysqlBase visibility cleanups
[mediawiki.git] / includes / libs / MapCacheLRU.php
blob2f5a454f5ddbe3880b7d86d4715351d9ed115314
1 <?php
2 /**
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
20 * @file
21 * @ingroup Cache
23 use Wikimedia\Assert\Assert;
25 /**
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
31 * @ingroup Cache
32 * @since 1.23
34 class MapCacheLRU {
35 /** @var array */
36 protected $cache = []; // (key => value)
38 protected $maxCacheKeys; // integer; max entries
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.
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;
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.
56 * @param string $key
57 * @param mixed $value
58 * @return void
60 public function set( $key, $value ) {
61 if ( array_key_exists( $key, $this->cache ) ) {
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] );
68 $this->cache[$key] = $value;
71 /**
72 * Check if a key exists
74 * @param string $key
75 * @return bool
77 public function has( $key ) {
78 return array_key_exists( $key, $this->cache );
81 /**
82 * Get the value for a key.
83 * This returns null if the key is not set.
84 * If the item is already set, it will be pushed to the top of the cache.
86 * @param string $key
87 * @return mixed Returns null if the key was not found
89 public function get( $key ) {
90 if ( !array_key_exists( $key, $this->cache ) ) {
91 return null;
94 $this->ping( $key );
96 return $this->cache[$key];
99 /**
100 * @return array
101 * @since 1.25
103 public function getAllKeys() {
104 return array_keys( $this->cache );
108 * Get an item with the given key, producing and setting it if not found.
110 * If the callback returns false, then nothing is stored.
112 * @since 1.28
113 * @param string $key
114 * @param callable $callback Callback that will produce the value
115 * @return mixed The cached value if found or the result of $callback otherwise
117 public function getWithSetCallback( $key, callable $callback ) {
118 if ( $this->has( $key ) ) {
119 $value = $this->get( $key );
120 } else {
121 $value = call_user_func( $callback );
122 if ( $value !== false ) {
123 $this->set( $key, $value );
127 return $value;
131 * Clear one or several cache entries, or all cache entries
133 * @param string|array $keys
134 * @return void
136 public function clear( $keys = null ) {
137 if ( $keys === null ) {
138 $this->cache = [];
139 } else {
140 foreach ( (array)$keys as $key ) {
141 unset( $this->cache[$key] );
147 * Push an entry to the top of the cache
149 * @param string $key
151 protected function ping( $key ) {
152 $item = $this->cache[$key];
153 unset( $this->cache[$key] );
154 $this->cache[$key] = $item;