Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / libs / ProcessCacheLRU.php
blob8d80eb38671fab8b2f153c6e34508b8e757b3fff
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)
32 /** @var Array */
33 protected $cacheTimes = array(); // (key => prop => UNIX timestamp)
35 protected $maxCacheKeys; // integer; max entries
37 /**
38 * @param int $maxKeys Maximum number of entries allowed (min 1).
39 * @throws UnexpectedValueException When $maxCacheKeys is not an int or =< 0.
41 public function __construct( $maxKeys ) {
42 $this->resize( $maxKeys );
45 /**
46 * Set a property field for a cache entry.
47 * This will prune the cache if it gets too large based on LRU.
48 * If the item is already set, it will be pushed to the top of the cache.
50 * @param string $key
51 * @param string $prop
52 * @param mixed $value
53 * @return void
55 public function set( $key, $prop, $value ) {
56 if ( isset( $this->cache[$key] ) ) {
57 $this->ping( $key ); // push to top
58 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
59 reset( $this->cache );
60 $evictKey = key( $this->cache );
61 unset( $this->cache[$evictKey] );
62 unset( $this->cacheTimes[$evictKey] );
64 $this->cache[$key][$prop] = $value;
65 $this->cacheTimes[$key][$prop] = microtime( true );
68 /**
69 * Check if a property field exists for a cache entry.
71 * @param string $key
72 * @param string $prop
73 * @param float $maxAge Ignore items older than this many seconds (since 1.21)
74 * @return bool
76 public function has( $key, $prop, $maxAge = 0.0 ) {
77 if ( isset( $this->cache[$key][$prop] ) ) {
78 return ( $maxAge <= 0 ||
79 ( microtime( true ) - $this->cacheTimes[$key][$prop] ) <= $maxAge
83 return false;
86 /**
87 * Get a property field for a cache entry.
88 * This returns null if the property is not set.
89 * If the item is already set, it will be pushed to the top of the cache.
91 * @param string $key
92 * @param string $prop
93 * @return mixed
95 public function get( $key, $prop ) {
96 if ( isset( $this->cache[$key][$prop] ) ) {
97 // push to top
98 $this->ping( $key );
99 return $this->cache[$key][$prop];
100 } else {
101 return null;
106 * Clear one or several cache entries, or all cache entries.
108 * @param string|array $keys
109 * @return void
111 public function clear( $keys = null ) {
112 if ( $keys === null ) {
113 $this->cache = array();
114 $this->cacheTimes = array();
115 } else {
116 foreach ( (array)$keys as $key ) {
117 unset( $this->cache[$key] );
118 unset( $this->cacheTimes[$key] );
124 * Resize the maximum number of cache entries, removing older entries as needed
126 * @param int $maxKeys
127 * @return void
128 * @throws UnexpectedValueException
130 public function resize( $maxKeys ) {
131 if ( !is_int( $maxKeys ) || $maxKeys < 1 ) {
132 throw new UnexpectedValueException( __METHOD__ . " must be given an integer >= 1" );
134 $this->maxCacheKeys = $maxKeys;
135 while ( count( $this->cache ) > $this->maxCacheKeys ) {
136 reset( $this->cache );
137 $evictKey = key( $this->cache );
138 unset( $this->cache[$evictKey] );
139 unset( $this->cacheTimes[$evictKey] );
144 * Push an entry to the top of the cache
146 * @param string $key
148 protected function ping( $key ) {
149 $item = $this->cache[$key];
150 unset( $this->cache[$key] );
151 $this->cache[$key] = $item;