From 77f7e9ba654058617f97e84563d2fa0061dc72a1 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 4 Oct 2013 20:47:33 -0700 Subject: [PATCH] Added MapCacheLRU class, a simpler cousin to ProcessCacheLRU Change-Id: Ibe1b44c8e168a086afb3481b4bb7660dc7d31ae7 --- includes/AutoLoader.php | 1 + includes/cache/MapCacheLRU.php | 113 +++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 includes/cache/MapCacheLRU.php diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 60ee2a866b..6ee62e0424 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -390,6 +390,7 @@ $wgAutoloadLocalClasses = array( 'HTMLFileCache' => 'includes/cache/HTMLFileCache.php', 'LinkBatch' => 'includes/cache/LinkBatch.php', 'LinkCache' => 'includes/cache/LinkCache.php', + 'MapCacheLRU' => 'includes/cache/MapCacheLRU.php', 'MessageCache' => 'includes/cache/MessageCache.php', 'ObjectFileCache' => 'includes/cache/ObjectFileCache.php', 'ProcessCacheLRU' => 'includes/cache/ProcessCacheLRU.php', diff --git a/includes/cache/MapCacheLRU.php b/includes/cache/MapCacheLRU.php new file mode 100644 index 0000000000..3539d8fca2 --- /dev/null +++ b/includes/cache/MapCacheLRU.php @@ -0,0 +1,113 @@ + value) + + protected $maxCacheKeys; // integer; max entries + + /** + * @param $maxKeys integer Maximum number of entries allowed (min 1). + * @throws MWException When $maxCacheKeys is not an int or =< 0. + */ + public function __construct( $maxKeys ) { + if ( !is_int( $maxKeys ) || $maxKeys < 1 ) { + throw new MWException( __METHOD__ . " must be given an integer and >= 1" ); + } + $this->maxCacheKeys = $maxKeys; + } + + /** + * Set a key/value pair. + * This will prune the cache if it gets too large based on LRU. + * If the item is already set, it will be pushed to the top of the cache. + * + * @param $key string + * @param $value mixed + * @return void + */ + public function set( $key, $value ) { + if ( isset( $this->cache[$key] ) ) { + $this->ping( $key ); // push to top + } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) { + reset( $this->cache ); + $evictKey = key( $this->cache ); + unset( $this->cache[$evictKey] ); + } + $this->cache[$key] = $value; + } + + /** + * Get the value for a key. + * This returns null if the key is not set. + * If the item is already set, it will be pushed to the top of the cache. + * + * @param $key string + * @param $prop string + * @return mixed + */ + public function get( $key ) { + if ( isset( $this->cache[$key] ) ) { + $this->ping( $key ); // push to top + return $this->cache[$key]; + } else { + return null; + } + } + + /** + * Clear one or several cache entries, or all cache entries + * + * @param $keys string|Array + * @return void + */ + public function clear( $keys = null ) { + if ( $keys === null ) { + $this->cache = array(); + } else { + foreach ( (array)$keys as $key ) { + unset( $this->cache[$key] ); + } + } + } + + /** + * Push an entry to the top of the cache + * + * @param $key string + */ + protected function ping( $key ) { + $item = $this->cache[$key]; + unset( $this->cache[$key] ); + $this->cache[$key] = $item; + } +} -- 2.20.1