Merge "Allow HTML5 <rtc> tag (ruby support for East Asian typography)."
[lhc/web/wiklou.git] / includes / libs / ProcessCacheLRU.php
index f2d9f42..f988207 100644 (file)
@@ -38,10 +38,7 @@ class ProcessCacheLRU {
         * @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 );
        }
 
        /**
@@ -119,6 +116,25 @@ class ProcessCacheLRU {
                }
        }
 
+       /**
+        * Resize the maximum number of cache entries, removing older entries as needed
+        *
+        * @param $maxKeys integer
+        * @return void
+        */
+       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
         *