Merge "Convert Special:DeletedContributions to use OOUI."
[lhc/web/wiklou.git] / includes / libs / ProcessCacheLRU.php
index b55ff9d..03e23ed 100644 (file)
@@ -28,10 +28,10 @@ use Wikimedia\Assert\Assert;
  */
 class ProcessCacheLRU {
        /** @var Array */
-       protected $cache = array(); // (key => prop => value)
+       protected $cache = []; // (key => prop => value)
 
        /** @var Array */
-       protected $cacheTimes = array(); // (key => prop => UNIX timestamp)
+       protected $cacheTimes = []; // (key => prop => UNIX timestamp)
 
        protected $maxCacheKeys; // integer; max entries
 
@@ -55,7 +55,7 @@ class ProcessCacheLRU {
         */
        public function set( $key, $prop, $value ) {
                if ( isset( $this->cache[$key] ) ) {
-                       $this->ping( $key ); // push to top
+                       $this->ping( $key );
                } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
                        reset( $this->cache );
                        $evictKey = key( $this->cache );
@@ -94,13 +94,11 @@ class ProcessCacheLRU {
         * @return mixed
         */
        public function get( $key, $prop ) {
-               if ( isset( $this->cache[$key][$prop] ) ) {
-                       // push to top
-                       $this->ping( $key );
-                       return $this->cache[$key][$prop];
-               } else {
+               if ( !isset( $this->cache[$key][$prop] ) ) {
                        return null;
                }
+               $this->ping( $key );
+               return $this->cache[$key][$prop];
        }
 
        /**
@@ -111,8 +109,8 @@ class ProcessCacheLRU {
         */
        public function clear( $keys = null ) {
                if ( $keys === null ) {
-                       $this->cache = array();
-                       $this->cacheTimes = array();
+                       $this->cache = [];
+                       $this->cacheTimes = [];
                } else {
                        foreach ( (array)$keys as $key ) {
                                unset( $this->cache[$key] );
@@ -130,7 +128,7 @@ class ProcessCacheLRU {
         */
        public function resize( $maxKeys ) {
                Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
-               Assert::parameter( $maxKeys >= 1, '$maxKeys', 'must be >= 1' );
+               Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
 
                $this->maxCacheKeys = $maxKeys;
                while ( count( $this->cache ) > $this->maxCacheKeys ) {
@@ -151,4 +149,12 @@ class ProcessCacheLRU {
                unset( $this->cache[$key] );
                $this->cache[$key] = $item;
        }
+
+       /**
+        * Get cache size
+        * @return int
+        */
+       public function getSize() {
+               return $this->maxCacheKeys;
+       }
 }