Made default BagOStuff functions work consistently.
authorAaron Schulz <aschulz@wikimedia.org>
Sun, 3 Jun 2012 23:25:37 +0000 (16:25 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Sun, 3 Jun 2012 23:29:39 +0000 (16:29 -0700)
* Made getMulti() only return keys for items that exists.
* Made add() return false if the item already exist.
* Made replace() return false it the item does not already exist.

Change-Id: Idb9d9843ace0c81f71abfc22b90e30eb33d7579d

includes/objectcache/BagOStuff.php

index 2b26640..e6ba042 100644 (file)
@@ -60,20 +60,6 @@ abstract class BagOStuff {
         */
        abstract public function get( $key );
 
-       /**
-        * Get an associative array containing the item for each of the given keys.
-        * Each item will be false if it does not exist.
-        * @param $keys Array List of strings
-        * @return Array
-        */
-       public function getMulti( array $keys ) {
-               $res = array();
-               foreach ( $keys as $key ) {
-                       $res[$key] = $this->get( $key );
-               }
-               return $res;
-       }
-
        /**
         * Set an item.
         * @param $key string
@@ -135,6 +121,22 @@ abstract class BagOStuff {
 
        /* *** Emulated functions *** */
 
+       /**
+        * Get an associative array containing the item for each of the keys that have items.
+        * @param $keys Array List of strings
+        * @return Array
+        */
+       public function getMulti( array $keys ) {
+               $res = array();
+               foreach ( $keys as $key ) {
+                       $val = $this->get( $key );
+                       if ( $val !== false ) {
+                               $res[$key] = $val;
+                       }
+               }
+               return $res;
+       }
+
        /**
         * @param $key string
         * @param $value mixed
@@ -142,10 +144,10 @@ abstract class BagOStuff {
         * @return bool success
         */
        public function add( $key, $value, $exptime = 0 ) {
-               if ( !$this->get( $key ) ) {
+               if ( $this->get( $key ) === false ) {
                        return $this->set( $key, $value, $exptime );
                }
-               return true;
+               return false; // key already set
        }
 
        /**
@@ -157,7 +159,7 @@ abstract class BagOStuff {
                if ( $this->get( $key ) !== false ) {
                        return $this->set( $key, $value, $exptime );
                }
-               return true;
+               return false; // key not already set
        }
 
        /**