Switch some HTMLForms in special pages to OOUI
[lhc/web/wiklou.git] / includes / libs / ProcessCacheLRU.php
1 <?php
2 /**
3 * Per-process memory cache for storing items.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23 use Wikimedia\Assert\Assert;
24
25 /**
26 * Handles per process caching of items
27 * @ingroup Cache
28 */
29 class ProcessCacheLRU {
30 /** @var Array */
31 protected $cache = array(); // (key => prop => value)
32
33 /** @var Array */
34 protected $cacheTimes = array(); // (key => prop => UNIX timestamp)
35
36 protected $maxCacheKeys; // integer; max entries
37
38 /**
39 * @param int $maxKeys Maximum number of entries allowed (min 1).
40 * @throws UnexpectedValueException When $maxCacheKeys is not an int or =< 0.
41 */
42 public function __construct( $maxKeys ) {
43 $this->resize( $maxKeys );
44 }
45
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.
50 *
51 * @param string $key
52 * @param string $prop
53 * @param mixed $value
54 * @return void
55 */
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] );
64 }
65 $this->cache[$key][$prop] = $value;
66 $this->cacheTimes[$key][$prop] = microtime( true );
67 }
68
69 /**
70 * Check if a property field exists for a cache entry.
71 *
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
76 */
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
81 );
82 }
83
84 return false;
85 }
86
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.
91 *
92 * @param string $key
93 * @param string $prop
94 * @return mixed
95 */
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;
103 }
104 }
105
106 /**
107 * Clear one or several cache entries, or all cache entries.
108 *
109 * @param string|array $keys
110 * @return void
111 */
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] );
120 }
121 }
122 }
123
124 /**
125 * Resize the maximum number of cache entries, removing older entries as needed
126 *
127 * @param int $maxKeys
128 * @return void
129 * @throws UnexpectedValueException
130 */
131 public function resize( $maxKeys ) {
132 Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
133 Assert::parameter( $maxKeys >= 1, '$maxKeys', 'must be >= 1' );
134
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] );
141 }
142 }
143
144 /**
145 * Push an entry to the top of the cache
146 *
147 * @param string $key
148 */
149 protected function ping( $key ) {
150 $item = $this->cache[$key];
151 unset( $this->cache[$key] );
152 $this->cache[$key] = $item;
153 }
154 }