Remove collateOnly() from ProfilerStandard and subclasses
[mediawiki.git] / includes / libs / ProcessCacheLRU.php
blobce97142d77f394591c3247b9f178281defad73d9
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
24 /**
25 * Handles per process caching of items
26 * @ingroup Cache
28 class ProcessCacheLRU {
29 /** @var Array */
30 protected $cache = array(); // (key => prop => value)
31 /** @var Array */
32 protected $cacheTimes = array(); // (key => prop => UNIX timestamp)
34 protected $maxCacheKeys; // integer; max entries
36 /**
37 * @param $maxKeys integer Maximum number of entries allowed (min 1).
38 * @throws UnexpectedValueException When $maxCacheKeys is not an int or =< 0.
40 public function __construct( $maxKeys ) {
41 $this->resize( $maxKeys );
44 /**
45 * Set a property field for a cache entry.
46 * This will prune the cache if it gets too large based on LRU.
47 * If the item is already set, it will be pushed to the top of the cache.
49 * @param $key string
50 * @param $prop string
51 * @param $value mixed
52 * @return void
54 public function set( $key, $prop, $value ) {
55 if ( isset( $this->cache[$key] ) ) {
56 $this->ping( $key ); // push to top
57 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
58 reset( $this->cache );
59 $evictKey = key( $this->cache );
60 unset( $this->cache[$evictKey] );
61 unset( $this->cacheTimes[$evictKey] );
63 $this->cache[$key][$prop] = $value;
64 $this->cacheTimes[$key][$prop] = microtime( true );
67 /**
68 * Check if a property field exists for a cache entry.
70 * @param $key string
71 * @param $prop string
72 * @param $maxAge float Ignore items older than this many seconds (since 1.21)
73 * @return bool
75 public function has( $key, $prop, $maxAge = 0.0 ) {
76 if ( isset( $this->cache[$key][$prop] ) ) {
77 return ( $maxAge <= 0 ||
78 ( microtime( true ) - $this->cacheTimes[$key][$prop] ) <= $maxAge );
81 return false;
84 /**
85 * Get a property field for a cache entry.
86 * This returns null if the property is not set.
87 * If the item is already set, it will be pushed to the top of the cache.
89 * @param $key string
90 * @param $prop string
91 * @return mixed
93 public function get( $key, $prop ) {
94 if ( isset( $this->cache[$key][$prop] ) ) {
95 $this->ping( $key ); // push to top
96 return $this->cache[$key][$prop];
97 } else {
98 return null;
103 * Clear one or several cache entries, or all cache entries
105 * @param $keys string|Array
106 * @return void
108 public function clear( $keys = null ) {
109 if ( $keys === null ) {
110 $this->cache = array();
111 $this->cacheTimes = array();
112 } else {
113 foreach ( (array)$keys as $key ) {
114 unset( $this->cache[$key] );
115 unset( $this->cacheTimes[$key] );
121 * Resize the maximum number of cache entries, removing older entries as needed
123 * @param $maxKeys integer
124 * @return void
125 * @throws UnexpectedValueException
127 public function resize( $maxKeys ) {
128 if ( !is_int( $maxKeys ) || $maxKeys < 1 ) {
129 throw new UnexpectedValueException( __METHOD__ . " must be given an integer >= 1" );
131 $this->maxCacheKeys = $maxKeys;
132 while ( count( $this->cache ) > $this->maxCacheKeys ) {
133 reset( $this->cache );
134 $evictKey = key( $this->cache );
135 unset( $this->cache[$evictKey] );
136 unset( $this->cacheTimes[$evictKey] );
141 * Push an entry to the top of the cache
143 * @param $key string
145 protected function ping( $key ) {
146 $item = $this->cache[$key];
147 unset( $this->cache[$key] );
148 $this->cache[$key] = $item;