Merge "Added a separate error message for mkdir failures"
[lhc/web/wiklou.git] / includes / libs / objectcache / HashBagOStuff.php
index 7b85d92..baa3c32 100644 (file)
@@ -20,7 +20,6 @@
  * @file
  * @ingroup Cache
  */
-use Wikimedia\Assert\Assert;
 
 /**
  * Simple store for keeping values in an associative array for the current process.
@@ -31,7 +30,7 @@ use Wikimedia\Assert\Assert;
  */
 class HashBagOStuff extends BagOStuff {
        /** @var mixed[] */
-       protected $bag = array();
+       protected $bag = [];
        /** @var integer Max entries allowed */
        protected $maxCacheKeys;
 
@@ -42,11 +41,13 @@ class HashBagOStuff extends BagOStuff {
         * @param array $params Additional parameters include:
         *   - maxKeys : only allow this many keys (using oldest-first eviction)
         */
-       function __construct( $params = array() ) {
+       function __construct( $params = [] ) {
                parent::__construct( $params );
 
                $this->maxCacheKeys = isset( $params['maxKeys'] ) ? $params['maxKeys'] : INF;
-               Assert::parameter( $this->maxCacheKeys > 0, 'maxKeys', 'must be above zero' );
+               if ( $this->maxCacheKeys <= 0 ) {
+                       throw new InvalidArgumentException( '$maxKeys parameter must be above zero' );
+               }
        }
 
        protected function expire( $key ) {
@@ -60,8 +61,19 @@ class HashBagOStuff extends BagOStuff {
                return true;
        }
 
+       /**
+        * Does this bag have a non-null value for the given key?
+        *
+        * @param string $key
+        * @return bool
+        * @since 1.27
+        */
+       protected function hasKey( $key ) {
+               return isset( $this->bag[$key] );
+       }
+
        protected function doGet( $key, $flags = 0 ) {
-               if ( !isset( $this->bag[$key] ) ) {
+               if ( !$this->hasKey( $key ) ) {
                        return false;
                }
 
@@ -80,10 +92,10 @@ class HashBagOStuff extends BagOStuff {
        public function set( $key, $value, $exptime = 0, $flags = 0 ) {
                // Refresh key position for maxCacheKeys eviction
                unset( $this->bag[$key] );
-               $this->bag[$key] = array(
+               $this->bag[$key] = [
                        self::KEY_VAL => $value,
                        self::KEY_EXP => $this->convertExpiry( $exptime )
-               );
+               ];
 
                if ( count( $this->bag ) > $this->maxCacheKeys ) {
                        reset( $this->bag );
@@ -101,6 +113,6 @@ class HashBagOStuff extends BagOStuff {
        }
 
        public function clear() {
-               $this->bag = array();
+               $this->bag = [];
        }
 }