Made Title cache use MapCacheLRU
[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 */
32 class MapCacheLRU {
33 /** @var Array */
34 protected $cache = array(); // (key => value)
35
36 protected $maxCacheKeys; // integer; max entries
37
38 /**
39 * @param $maxKeys integer Maximum number of entries allowed (min 1).
40 * @throws MWException When $maxCacheKeys is not an int or =< 0.
41 */
42 public function __construct( $maxKeys ) {
43 if ( !is_int( $maxKeys ) || $maxKeys < 1 ) {
44 throw new MWException( __METHOD__ . " must be given an integer and >= 1" );
45 }
46 $this->maxCacheKeys = $maxKeys;
47 }
48
49 /**
50 * Set a key/value pair.
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 $key string
55 * @param $value mixed
56 * @return void
57 */
58 public function set( $key, $value ) {
59 if ( isset( $this->cache[$key] ) ) {
60 $this->ping( $key ); // push to top
61 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
62 reset( $this->cache );
63 $evictKey = key( $this->cache );
64 unset( $this->cache[$evictKey] );
65 }
66 $this->cache[$key] = $value;
67 }
68
69 /**
70 * Check if a key exists
71 *
72 * @param $key string
73 * @return bool
74 */
75 public function has( $key ) {
76 return isset( $this->cache[$key] );
77 }
78
79 /**
80 * Get the value for a key.
81 * This returns null if the key is not set.
82 * If the item is already set, it will be pushed to the top of the cache.
83 *
84 * @param $key string
85 * @return mixed
86 */
87 public function get( $key ) {
88 if ( isset( $this->cache[$key] ) ) {
89 $this->ping( $key ); // push to top
90 return $this->cache[$key];
91 } else {
92 return null;
93 }
94 }
95
96 /**
97 * Clear one or several cache entries, or all cache entries
98 *
99 * @param $keys string|Array
100 * @return void
101 */
102 public function clear( $keys = null ) {
103 if ( $keys === null ) {
104 $this->cache = array();
105 } else {
106 foreach ( (array)$keys as $key ) {
107 unset( $this->cache[$key] );
108 }
109 }
110 }
111
112 /**
113 * Push an entry to the top of the cache
114 *
115 * @param $key string
116 */
117 protected function ping( $key ) {
118 $item = $this->cache[$key];
119 unset( $this->cache[$key] );
120 $this->cache[$key] = $item;
121 }
122 }