*/
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
*/
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 );
* @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];
}
/**
*/
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] );
*/
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 ) {
unset( $this->cache[$key] );
$this->cache[$key] = $item;
}
+
+ /**
+ * Get cache size
+ * @return int
+ */
+ public function getSize() {
+ return $this->maxCacheKeys;
+ }
}