3 * Per-process memory cache for storing items.
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.
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.
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
25 * Handles a simple LRU key/value map with a maximum number of entries
27 * Use ProcessCacheLRU if hierarchical purging is needed or objects can become stale
29 * @see ProcessCacheLRU
35 protected $cache = array(); // (key => value)
37 protected $maxCacheKeys; // integer; max entries
40 * @param int $maxKeys Maximum number of entries allowed (min 1).
41 * @throws Exception When $maxCacheKeys is not an int or =< 0.
43 public function __construct( $maxKeys ) {
44 if ( !is_int( $maxKeys ) ||
$maxKeys < 1 ) {
45 throw new Exception( __METHOD__
. " must be given an integer and >= 1" );
47 $this->maxCacheKeys
= $maxKeys;
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.
59 public function set( $key, $value ) {
60 if ( array_key_exists( $key, $this->cache
) ) {
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] );
67 $this->cache
[$key] = $value;
71 * Check if a key exists
76 public function has( $key ) {
77 if ( is_string( $key ) ||
is_integer( $key ) ) {
78 return array_key_exists( $key, $this->cache
);
80 wfWarn( __METHOD__
. ": Key passed isn't a string or an integer.", 2 );
85 * Get the value for a key.
86 * This returns null if the key is not set.
87 * If the item is already set, it will be pushed to the top of the cache.
92 public function get( $key ) {
93 if ( array_key_exists( $key, $this->cache
) ) {
94 $this->ping( $key ); // push to top
95 return $this->cache
[$key];
105 public function getAllKeys() {
106 return array_keys( $this->cache
);
110 * Clear one or several cache entries, or all cache entries
112 * @param string|array $keys
115 public function clear( $keys = null ) {
116 if ( $keys === null ) {
117 $this->cache
= array();
119 foreach ( (array)$keys as $key ) {
120 unset( $this->cache
[$key] );
126 * Push an entry to the top of the cache
130 protected function ping( $key ) {
131 $item = $this->cache
[$key];
132 unset( $this->cache
[$key] );
133 $this->cache
[$key] = $item;