Merge "mediawiki.skinning: Merge toccolours into css rule for toc and mw-warning"
[lhc/web/wiklou.git] / includes / libs / ProcessCacheLRU.php
index f2d9f42..8d80eb3 100644 (file)
 class ProcessCacheLRU {
        /** @var Array */
        protected $cache = array(); // (key => prop => value)
+
        /** @var Array */
        protected $cacheTimes = array(); // (key => prop => UNIX timestamp)
 
        protected $maxCacheKeys; // integer; max entries
 
        /**
-        * @param $maxKeys integer Maximum number of entries allowed (min 1).
+        * @param int $maxKeys Maximum number of entries allowed (min 1).
         * @throws UnexpectedValueException When $maxCacheKeys is not an int or =< 0.
         */
        public function __construct( $maxKeys ) {
-               if ( !is_int( $maxKeys ) || $maxKeys < 1 ) {
-                       throw new UnexpectedValueException( __METHOD__ . " must be given an integer >= 1" );
-               }
-               $this->maxCacheKeys = $maxKeys;
+               $this->resize( $maxKeys );
        }
 
        /**
@@ -49,9 +47,9 @@ class ProcessCacheLRU {
         * 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 $prop string
-        * @param $value mixed
+        * @param string $key
+        * @param string $prop
+        * @param mixed $value
         * @return void
         */
        public function set( $key, $prop, $value ) {
@@ -64,20 +62,22 @@ class ProcessCacheLRU {
                        unset( $this->cacheTimes[$evictKey] );
                }
                $this->cache[$key][$prop] = $value;
-               $this->cacheTimes[$key][$prop] = time();
+               $this->cacheTimes[$key][$prop] = microtime( true );
        }
 
        /**
         * Check if a property field exists for a cache entry.
         *
-        * @param $key string
-        * @param $prop string
-        * @param $maxAge integer Ignore items older than this many seconds (since 1.21)
+        * @param string $key
+        * @param string $prop
+        * @param float $maxAge Ignore items older than this many seconds (since 1.21)
         * @return bool
         */
-       public function has( $key, $prop, $maxAge = 0 ) {
+       public function has( $key, $prop, $maxAge = 0.0 ) {
                if ( isset( $this->cache[$key][$prop] ) ) {
-                       return ( $maxAge <= 0 || ( time() - $this->cacheTimes[$key][$prop] ) <= $maxAge );
+                       return ( $maxAge <= 0 ||
+                               ( microtime( true ) - $this->cacheTimes[$key][$prop] ) <= $maxAge
+                       );
                }
 
                return false;
@@ -88,13 +88,14 @@ class ProcessCacheLRU {
         * This returns null if the property 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
+        * @param string $key
+        * @param string $prop
         * @return mixed
         */
        public function get( $key, $prop ) {
                if ( isset( $this->cache[$key][$prop] ) ) {
-                       $this->ping( $key ); // push to top
+                       // push to top
+                       $this->ping( $key );
                        return $this->cache[$key][$prop];
                } else {
                        return null;
@@ -102,9 +103,9 @@ class ProcessCacheLRU {
        }
 
        /**
-        * Clear one or several cache entries, or all cache entries
+        * Clear one or several cache entries, or all cache entries.
         *
-        * @param $keys string|Array
+        * @param string|array $keys
         * @return void
         */
        public function clear( $keys = null ) {
@@ -119,10 +120,30 @@ class ProcessCacheLRU {
                }
        }
 
+       /**
+        * Resize the maximum number of cache entries, removing older entries as needed
+        *
+        * @param int $maxKeys
+        * @return void
+        * @throws UnexpectedValueException
+        */
+       public function resize( $maxKeys ) {
+               if ( !is_int( $maxKeys ) || $maxKeys < 1 ) {
+                       throw new UnexpectedValueException( __METHOD__ . " must be given an integer >= 1" );
+               }
+               $this->maxCacheKeys = $maxKeys;
+               while ( count( $this->cache ) > $this->maxCacheKeys ) {
+                       reset( $this->cache );
+                       $evictKey = key( $this->cache );
+                       unset( $this->cache[$evictKey] );
+                       unset( $this->cacheTimes[$evictKey] );
+               }
+       }
+
        /**
         * Push an entry to the top of the cache
         *
-        * @param $key string
+        * @param string $key
         */
        protected function ping( $key ) {
                $item = $this->cache[$key];