* @file
* @ingroup Cache
*/
-use Wikimedia\Assert\Assert;
/**
* Class for process caching individual properties of expiring items
* 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 );
}
/**
* @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 );
}
/**
* @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 );
}
/**
* @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 );
}
/**
* @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 );
}
/**
* @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 );
}
/**
* @return int
*/
public function getSize() {
- return $this->maxCacheKeys;
+ return $this->cache->getMaxSize();
}
}
* 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 );
}
/**
* 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() );
}
}