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 per process caching of items
29 class ProcessCacheLRU
{
31 protected $cache = []; // (key => prop => value)
34 protected $cacheTimes = []; // (key => prop => UNIX timestamp)
36 protected $maxCacheKeys; // integer; max entries
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 );
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.
56 public function set( $key, $prop, $value ) {
57 if ( isset( $this->cache
[$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 );
70 * Check if a property field exists for a cache entry.
74 * @param float $maxAge Ignore items older than this many seconds (since 1.21)
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
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.
96 public function get( $key, $prop ) {
97 if ( !isset( $this->cache
[$key][$prop] ) ) {
101 return $this->cache
[$key][$prop];
105 * Clear one or several cache entries, or all cache entries.
107 * @param string|array $keys
110 public function clear( $keys = null ) {
111 if ( $keys === null ) {
113 $this->cacheTimes
= [];
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
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
147 protected function ping( $key ) {
148 $item = $this->cache
[$key];
149 unset( $this->cache
[$key] );
150 $this->cache
[$key] = $item;