]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/libs/ProcessCacheLRU.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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 = []; // (key => prop => value)
32
33         /** @var Array */
34         protected $cacheTimes = []; // (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 );
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                         return null;
99                 }
100                 $this->ping( $key );
101                 return $this->cache[$key][$prop];
102         }
103
104         /**
105          * Clear one or several cache entries, or all cache entries.
106          *
107          * @param string|array $keys
108          * @return void
109          */
110         public function clear( $keys = null ) {
111                 if ( $keys === null ) {
112                         $this->cache = [];
113                         $this->cacheTimes = [];
114                 } else {
115                         foreach ( (array)$keys as $key ) {
116                                 unset( $this->cache[$key] );
117                                 unset( $this->cacheTimes[$key] );
118                         }
119                 }
120         }
121
122         /**
123          * Resize the maximum number of cache entries, removing older entries as needed
124          *
125          * @param int $maxKeys
126          * @return void
127          * @throws UnexpectedValueException
128          */
129         public function resize( $maxKeys ) {
130                 Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
131                 Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
132
133                 $this->maxCacheKeys = $maxKeys;
134                 while ( count( $this->cache ) > $this->maxCacheKeys ) {
135                         reset( $this->cache );
136                         $evictKey = key( $this->cache );
137                         unset( $this->cache[$evictKey] );
138                         unset( $this->cacheTimes[$evictKey] );
139                 }
140         }
141
142         /**
143          * Push an entry to the top of the cache
144          *
145          * @param string $key
146          */
147         protected function ping( $key ) {
148                 $item = $this->cache[$key];
149                 unset( $this->cache[$key] );
150                 $this->cache[$key] = $item;
151         }
152
153         /**
154          * Get cache size
155          * @return int
156          */
157         public function getSize() {
158                 return $this->maxCacheKeys;
159         }
160 }