Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / libs / ProcessCacheLRU.php
blob5a933914c61f83088bd3995fcb11c42e2f393efb
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 per process caching of items
27 * @ingroup Cache
29 class ProcessCacheLRU {
30 /** @var Array */
31 protected $cache = []; // (key => prop => value)
33 /** @var Array */
34 protected $cacheTimes = []; // (key => prop => UNIX timestamp)
36 protected $maxCacheKeys; // integer; max entries
38 /**
39 * @param int $maxKeys Maximum number of entries allowed (min 1).
40 * @throws UnexpectedValueException When $maxCacheKeys is not an int or =< 0.
42 public function __construct( $maxKeys ) {
43 $this->resize( $maxKeys );
46 /**
47 * Set a property field for a cache entry.
48 * This will prune the cache if it gets too large based on LRU.
49 * If the item is already set, it will be pushed to the top of the cache.
51 * @param string $key
52 * @param string $prop
53 * @param mixed $value
54 * @return void
56 public function set( $key, $prop, $value ) {
57 if ( isset( $this->cache[$key] ) ) {
58 $this->ping( $key );
59 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
60 reset( $this->cache );
61 $evictKey = key( $this->cache );
62 unset( $this->cache[$evictKey] );
63 unset( $this->cacheTimes[$evictKey] );
65 $this->cache[$key][$prop] = $value;
66 $this->cacheTimes[$key][$prop] = microtime( true );
69 /**
70 * Check if a property field exists for a cache entry.
72 * @param string $key
73 * @param string $prop
74 * @param float $maxAge Ignore items older than this many seconds (since 1.21)
75 * @return bool
77 public function has( $key, $prop, $maxAge = 0.0 ) {
78 if ( isset( $this->cache[$key][$prop] ) ) {
79 return ( $maxAge <= 0 ||
80 ( microtime( true ) - $this->cacheTimes[$key][$prop] ) <= $maxAge
84 return false;
87 /**
88 * Get a property field for a cache entry.
89 * This returns null if the property is not set.
90 * If the item is already set, it will be pushed to the top of the cache.
92 * @param string $key
93 * @param string $prop
94 * @return mixed
96 public function get( $key, $prop ) {
97 if ( !isset( $this->cache[$key][$prop] ) ) {
98 return null;
100 $this->ping( $key );
101 return $this->cache[$key][$prop];
105 * Clear one or several cache entries, or all cache entries.
107 * @param string|array $keys
108 * @return void
110 public function clear( $keys = null ) {
111 if ( $keys === null ) {
112 $this->cache = [];
113 $this->cacheTimes = [];
114 } else {
115 foreach ( (array)$keys as $key ) {
116 unset( $this->cache[$key] );
117 unset( $this->cacheTimes[$key] );
123 * Resize the maximum number of cache entries, removing older entries as needed
125 * @param int $maxKeys
126 * @return void
127 * @throws UnexpectedValueException
129 public function resize( $maxKeys ) {
130 Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
131 Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
133 $this->maxCacheKeys = $maxKeys;
134 while ( count( $this->cache ) > $this->maxCacheKeys ) {
135 reset( $this->cache );
136 $evictKey = key( $this->cache );
137 unset( $this->cache[$evictKey] );
138 unset( $this->cacheTimes[$evictKey] );
143 * Push an entry to the top of the cache
145 * @param string $key
147 protected function ping( $key ) {
148 $item = $this->cache[$key];
149 unset( $this->cache[$key] );
150 $this->cache[$key] = $item;