Merge "Don't trigger MessageBlobStore during tests"
[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
24 /**
25 * Handles per process caching of items
26 * @ingroup Cache
27 */
28 class ProcessCacheLRU {
29 /** @var Array */
30 protected $cache = array(); // (key => prop => value)
31
32 /** @var Array */
33 protected $cacheTimes = array(); // (key => prop => UNIX timestamp)
34
35 protected $maxCacheKeys; // integer; max entries
36
37 /**
38 * @param int $maxKeys Maximum number of entries allowed (min 1).
39 * @throws UnexpectedValueException When $maxCacheKeys is not an int or =< 0.
40 */
41 public function __construct( $maxKeys ) {
42 $this->resize( $maxKeys );
43 }
44
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.
49 *
50 * @param string $key
51 * @param string $prop
52 * @param mixed $value
53 * @return void
54 */
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] );
63 }
64 $this->cache[$key][$prop] = $value;
65 $this->cacheTimes[$key][$prop] = microtime( true );
66 }
67
68 /**
69 * Check if a property field exists for a cache entry.
70 *
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
75 */
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
80 );
81 }
82
83 return false;
84 }
85
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.
90 *
91 * @param string $key
92 * @param string $prop
93 * @return mixed
94 */
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;
102 }
103 }
104
105 /**
106 * Clear one or several cache entries, or all cache entries.
107 *
108 * @param string|array $keys
109 * @return void
110 */
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] );
119 }
120 }
121 }
122
123 /**
124 * Resize the maximum number of cache entries, removing older entries as needed
125 *
126 * @param int $maxKeys
127 * @return void
128 * @throws UnexpectedValueException
129 */
130 public function resize( $maxKeys ) {
131 if ( !is_int( $maxKeys ) || $maxKeys < 1 ) {
132 throw new UnexpectedValueException( __METHOD__ . " must be given an integer >= 1" );
133 }
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] );
140 }
141 }
142
143 /**
144 * Push an entry to the top of the cache
145 *
146 * @param string $key
147 */
148 protected function ping( $key ) {
149 $item = $this->cache[$key];
150 unset( $this->cache[$key] );
151 $this->cache[$key] = $item;
152 }
153 }