filebackend: clean up some comments and remove unused FileBackendStoreOpHandle field
[lhc/web/wiklou.git] / includes / libs / ProcessCacheLRU.php
index b374259..f0cd83e 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,29 +77,17 @@ 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 );
        }
 
        /**
         * Clear one or several cache entries, or all cache entries.
         *
-        * @param string|array $keys
+        * @param string|array|null $keys
         * @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();
        }
 }