Merge "objectcache: Add some newlines to WANObjectCache docs"
[mediawiki.git] / includes / libs / ProcessCacheLRU.php
blobb55ff9da8b9dfab4c900989547c18e8084d58649
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 = array(); // (key => prop => value)
33 /** @var Array */
34 protected $cacheTimes = array(); // (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 ); // push to top
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 // push to top
99 $this->ping( $key );
100 return $this->cache[$key][$prop];
101 } else {
102 return null;
107 * Clear one or several cache entries, or all cache entries.
109 * @param string|array $keys
110 * @return void
112 public function clear( $keys = null ) {
113 if ( $keys === null ) {
114 $this->cache = array();
115 $this->cacheTimes = array();
116 } else {
117 foreach ( (array)$keys as $key ) {
118 unset( $this->cache[$key] );
119 unset( $this->cacheTimes[$key] );
125 * Resize the maximum number of cache entries, removing older entries as needed
127 * @param int $maxKeys
128 * @return void
129 * @throws UnexpectedValueException
131 public function resize( $maxKeys ) {
132 Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
133 Assert::parameter( $maxKeys >= 1, '$maxKeys', 'must be >= 1' );
135 $this->maxCacheKeys = $maxKeys;
136 while ( count( $this->cache ) > $this->maxCacheKeys ) {
137 reset( $this->cache );
138 $evictKey = key( $this->cache );
139 unset( $this->cache[$evictKey] );
140 unset( $this->cacheTimes[$evictKey] );
145 * Push an entry to the top of the cache
147 * @param string $key
149 protected function ping( $key ) {
150 $item = $this->cache[$key];
151 unset( $this->cache[$key] );
152 $this->cache[$key] = $item;