Merge "Correct the plural forms for Manx (Gaelg)"
[lhc/web/wiklou.git] / includes / cache / MapCacheLRU.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 a simple LRU key/value map with a maximum number of entries
26 *
27 * Use ProcessCacheLRU if hierarchical purging is needed or objects can become stale
28 *
29 * @see ProcessCacheLRU
30 * @ingroup Cache
31 * @since 1.23
32 */
33 class MapCacheLRU {
34 /** @var Array */
35 protected $cache = array(); // (key => value)
36
37 protected $maxCacheKeys; // integer; max entries
38
39 /**
40 * @param $maxKeys integer Maximum number of entries allowed (min 1).
41 * @throws MWException When $maxCacheKeys is not an int or =< 0.
42 */
43 public function __construct( $maxKeys ) {
44 if ( !is_int( $maxKeys ) || $maxKeys < 1 ) {
45 throw new MWException( __METHOD__ . " must be given an integer and >= 1" );
46 }
47 $this->maxCacheKeys = $maxKeys;
48 }
49
50 /**
51 * Set a key/value pair.
52 * This will prune the cache if it gets too large based on LRU.
53 * If the item is already set, it will be pushed to the top of the cache.
54 *
55 * @param $key string
56 * @param $value mixed
57 * @return void
58 */
59 public function set( $key, $value ) {
60 if ( isset( $this->cache[$key] ) ) {
61 $this->ping( $key ); // push to top
62 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
63 reset( $this->cache );
64 $evictKey = key( $this->cache );
65 unset( $this->cache[$evictKey] );
66 }
67 $this->cache[$key] = $value;
68 }
69
70 /**
71 * Get the value for a key.
72 * This returns null if the key is not set.
73 * If the item is already set, it will be pushed to the top of the cache.
74 *
75 * @param $key string
76 * @param $prop string
77 * @return mixed
78 */
79 public function get( $key ) {
80 if ( isset( $this->cache[$key] ) ) {
81 $this->ping( $key ); // push to top
82 return $this->cache[$key];
83 } else {
84 return null;
85 }
86 }
87
88 /**
89 * Clear one or several cache entries, or all cache entries
90 *
91 * @param $keys string|Array
92 * @return void
93 */
94 public function clear( $keys = null ) {
95 if ( $keys === null ) {
96 $this->cache = array();
97 } else {
98 foreach ( (array)$keys as $key ) {
99 unset( $this->cache[$key] );
100 }
101 }
102 }
103
104 /**
105 * Push an entry to the top of the cache
106 *
107 * @param $key string
108 */
109 protected function ping( $key ) {
110 $item = $this->cache[$key];
111 unset( $this->cache[$key] );
112 $this->cache[$key] = $item;
113 }
114 }