aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Schulz <aschulz@wikimedia.org>2018-06-27 15:00:10 +0100
committerKrinkle <krinklemail@gmail.com>2018-06-28 20:23:37 +0000
commit6afa7bb86a38d0097f6a98238b4e9e60205ffd0a (patch)
treef57e90568a89d57ce47b6d6b4f2b6fd1ba8115d4
parent42ae4a4d82632e035e341ceb27fc23b880861972 (diff)
downloadmediawikicore-6afa7bb86a38d0097f6a98238b4e9e60205ffd0a.tar.gz
mediawikicore-6afa7bb86a38d0097f6a98238b4e9e60205ffd0a.zip
Make ProcessCacheLRU wrap MapCacheLRU
Change-Id: I190c824af471aee798e2f111b902f38532b8ac99
-rw-r--r--includes/libs/ProcessCacheLRU.php72
-rw-r--r--tests/phpunit/includes/libs/ProcessCacheLRUTest.php8
2 files changed, 13 insertions, 67 deletions
diff --git a/includes/libs/ProcessCacheLRU.php b/includes/libs/ProcessCacheLRU.php
index b374259fdc6a..eb32d985a53c 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 c8940e5f2b0f..c9fa3205fecc 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() );
}
}