Make ProcessCacheLRU wrap MapCacheLRU
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 27 Jun 2018 14:00:10 +0000 (15:00 +0100)
committerKrinkle <krinklemail@gmail.com>
Thu, 28 Jun 2018 20:23:37 +0000 (20:23 +0000)
Change-Id: I190c824af471aee798e2f111b902f38532b8ac99

includes/libs/ProcessCacheLRU.php
tests/phpunit/includes/libs/ProcessCacheLRUTest.php

index b374259..eb32d98 100644 (file)
@@ -20,7 +20,6 @@
  * @file
  * @ingroup Cache
  */
-use Wikimedia\Assert\Assert;
 
 /**
  * Class for process caching individual properties of expiring items
@@ -28,22 +27,18 @@ use Wikimedia\Assert\Assert;
  * When the key for an entire item is deleted, all properties for it are deleted
  *
  * @ingroup Cache
+ * @deprecated Since 1.32 Use MapCacheLRU instead
  */
 class ProcessCacheLRU {
-       /** @var array */
-       protected $cache = []; // (key => prop => value)
-
-       /** @var array */
-       protected $cacheTimes = []; // (key => prop => UNIX timestamp)
-
-       protected $maxCacheKeys; // integer; max entries
+       /** @var MapCacheLRU */
+       protected $cache;
 
        /**
         * @param int $maxKeys Maximum number of entries allowed (min 1).
         * @throws UnexpectedValueException When $maxCacheKeys is not an int or =< 0.
         */
        public function __construct( $maxKeys ) {
-               $this->resize( $maxKeys );
+               $this->cache = new MapCacheLRU( $maxKeys );
        }
 
        /**
@@ -57,16 +52,7 @@ class ProcessCacheLRU {
         * @return void
         */
        public function set( $key, $prop, $value ) {
-               if ( isset( $this->cache[$key] ) ) {
-                       $this->ping( $key );
-               } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
-                       reset( $this->cache );
-                       $evictKey = key( $this->cache );
-                       unset( $this->cache[$evictKey] );
-                       unset( $this->cacheTimes[$evictKey] );
-               }
-               $this->cache[$key][$prop] = $value;
-               $this->cacheTimes[$key][$prop] = microtime( true );
+               $this->cache->setField( $key, $prop, $value );
        }
 
        /**
@@ -78,13 +64,7 @@ class ProcessCacheLRU {
         * @return bool
         */
        public function has( $key, $prop, $maxAge = 0.0 ) {
-               if ( isset( $this->cache[$key][$prop] ) ) {
-                       return ( $maxAge <= 0 ||
-                               ( microtime( true ) - $this->cacheTimes[$key][$prop] ) <= $maxAge
-                       );
-               }
-
-               return false;
+               return $this->cache->hasField( $key, $prop, $maxAge );
        }
 
        /**
@@ -97,11 +77,7 @@ class ProcessCacheLRU {
         * @return mixed
         */
        public function get( $key, $prop ) {
-               if ( !isset( $this->cache[$key][$prop] ) ) {
-                       return null;
-               }
-               $this->ping( $key );
-               return $this->cache[$key][$prop];
+               return $this->cache->getField( $key, $prop );
        }
 
        /**
@@ -111,15 +87,7 @@ class ProcessCacheLRU {
         * @return void
         */
        public function clear( $keys = null ) {
-               if ( $keys === null ) {
-                       $this->cache = [];
-                       $this->cacheTimes = [];
-               } else {
-                       foreach ( (array)$keys as $key ) {
-                               unset( $this->cache[$key] );
-                               unset( $this->cacheTimes[$key] );
-                       }
-               }
+               $this->cache->clear( $keys );
        }
 
        /**
@@ -130,27 +98,7 @@ class ProcessCacheLRU {
         * @throws UnexpectedValueException
         */
        public function resize( $maxKeys ) {
-               Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
-               Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
-
-               $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 string $key
-        */
-       protected function ping( $key ) {
-               $item = $this->cache[$key];
-               unset( $this->cache[$key] );
-               $this->cache[$key] = $item;
+               $this->cache->setMaxSize( $maxKeys );
        }
 
        /**
@@ -158,6 +106,6 @@ class ProcessCacheLRU {
         * @return int
         */
        public function getSize() {
-               return $this->maxCacheKeys;
+               return $this->cache->getMaxSize();
        }
 }
index c8940e5..c9fa320 100644 (file)
@@ -18,7 +18,7 @@ class ProcessCacheLRUTest extends PHPUnit\Framework\TestCase {
         * Compare against an array so we get the cache content difference.
         */
        protected function assertCacheEmpty( $cache, $msg = 'Cache should be empty' ) {
-               $this->assertAttributeEquals( [], 'cache', $cache, $msg );
+               $this->assertEquals( 0, $cache->getEntriesCount(), $msg );
        }
 
        /**
@@ -256,13 +256,11 @@ class ProcessCacheLRUTest extends PHPUnit\Framework\TestCase {
  * Overrides some ProcessCacheLRU methods and properties accessibility.
  */
 class ProcessCacheLRUTestable extends ProcessCacheLRU {
-       public $cache = [];
-
        public function getCache() {
-               return $this->cache;
+               return $this->cache->toArray();
        }
 
        public function getEntriesCount() {
-               return count( $this->cache );
+               return count( $this->cache->toArray() );
        }
 }