Merge "Add attributes parameter to ShowSearchHitTitle"
[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 * Class for process caching individual properties of expiring items
27 *
28 * When the key for an entire item is deleted, all properties for it are deleted
29 *
30 * @ingroup Cache
31 */
32 class ProcessCacheLRU {
33 /** @var array */
34 protected $cache = []; // (key => prop => value)
35
36 /** @var array */
37 protected $cacheTimes = []; // (key => prop => UNIX timestamp)
38
39 protected $maxCacheKeys; // integer; max entries
40
41 /**
42 * @param int $maxKeys Maximum number of entries allowed (min 1).
43 * @throws UnexpectedValueException When $maxCacheKeys is not an int or =< 0.
44 */
45 public function __construct( $maxKeys ) {
46 $this->resize( $maxKeys );
47 }
48
49 /**
50 * Set a property field for a cache entry.
51 * This will prune the cache if it gets too large based on LRU.
52 * If the item is already set, it will be pushed to the top of the cache.
53 *
54 * @param string $key
55 * @param string $prop
56 * @param mixed $value
57 * @return void
58 */
59 public function set( $key, $prop, $value ) {
60 if ( isset( $this->cache[$key] ) ) {
61 $this->ping( $key );
62 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
63 reset( $this->cache );
64 $evictKey = key( $this->cache );
65 unset( $this->cache[$evictKey] );
66 unset( $this->cacheTimes[$evictKey] );
67 }
68 $this->cache[$key][$prop] = $value;
69 $this->cacheTimes[$key][$prop] = microtime( true );
70 }
71
72 /**
73 * Check if a property field exists for a cache entry.
74 *
75 * @param string $key
76 * @param string $prop
77 * @param float $maxAge Ignore items older than this many seconds (since 1.21)
78 * @return bool
79 */
80 public function has( $key, $prop, $maxAge = 0.0 ) {
81 if ( isset( $this->cache[$key][$prop] ) ) {
82 return ( $maxAge <= 0 ||
83 ( microtime( true ) - $this->cacheTimes[$key][$prop] ) <= $maxAge
84 );
85 }
86
87 return false;
88 }
89
90 /**
91 * Get a property field for a cache entry.
92 * This returns null if the property is not set.
93 * If the item is already set, it will be pushed to the top of the cache.
94 *
95 * @param string $key
96 * @param string $prop
97 * @return mixed
98 */
99 public function get( $key, $prop ) {
100 if ( !isset( $this->cache[$key][$prop] ) ) {
101 return null;
102 }
103 $this->ping( $key );
104 return $this->cache[$key][$prop];
105 }
106
107 /**
108 * Clear one or several cache entries, or all cache entries.
109 *
110 * @param string|array $keys
111 * @return void
112 */
113 public function clear( $keys = null ) {
114 if ( $keys === null ) {
115 $this->cache = [];
116 $this->cacheTimes = [];
117 } else {
118 foreach ( (array)$keys as $key ) {
119 unset( $this->cache[$key] );
120 unset( $this->cacheTimes[$key] );
121 }
122 }
123 }
124
125 /**
126 * Resize the maximum number of cache entries, removing older entries as needed
127 *
128 * @param int $maxKeys
129 * @return void
130 * @throws UnexpectedValueException
131 */
132 public function resize( $maxKeys ) {
133 Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
134 Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
135
136 $this->maxCacheKeys = $maxKeys;
137 while ( count( $this->cache ) > $this->maxCacheKeys ) {
138 reset( $this->cache );
139 $evictKey = key( $this->cache );
140 unset( $this->cache[$evictKey] );
141 unset( $this->cacheTimes[$evictKey] );
142 }
143 }
144
145 /**
146 * Push an entry to the top of the cache
147 *
148 * @param string $key
149 */
150 protected function ping( $key ) {
151 $item = $this->cache[$key];
152 unset( $this->cache[$key] );
153 $this->cache[$key] = $item;
154 }
155
156 /**
157 * Get cache size
158 * @return int
159 */
160 public function getSize() {
161 return $this->maxCacheKeys;
162 }
163 }