From: Aaron Schulz Date: Wed, 27 Jun 2018 14:00:10 +0000 (+0100) Subject: Make ProcessCacheLRU wrap MapCacheLRU X-Git-Tag: 1.34.0-rc.0~4926^2 X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dmes_infos.php?a=commitdiff_plain;h=6afa7bb86a38d0097f6a98238b4e9e60205ffd0a;p=lhc%2Fweb%2Fwiklou.git Make ProcessCacheLRU wrap MapCacheLRU Change-Id: I190c824af471aee798e2f111b902f38532b8ac99 --- diff --git a/includes/libs/ProcessCacheLRU.php b/includes/libs/ProcessCacheLRU.php index b374259fdc..eb32d985a5 100644 --- a/includes/libs/ProcessCacheLRU.php +++ b/includes/libs/ProcessCacheLRU.php @@ -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(); } } diff --git a/tests/phpunit/includes/libs/ProcessCacheLRUTest.php b/tests/phpunit/includes/libs/ProcessCacheLRUTest.php index c8940e5f2b..c9fa3205fe 100644 --- a/tests/phpunit/includes/libs/ProcessCacheLRUTest.php +++ b/tests/phpunit/includes/libs/ProcessCacheLRUTest.php @@ -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() ); } }