merging latest master
[lhc/web/wiklou.git] / includes / objectcache / BagOStuff.php
index 2b26640..fcc3aa9 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,29 +144,30 @@ 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
        }
 
        /**
         * @param $key string
         * @param $value mixed
+        * @param $exptime int
         * @return bool success
         */
        public function replace( $key, $value, $exptime = 0 ) {
                if ( $this->get( $key ) !== false ) {
                        return $this->set( $key, $value, $exptime );
                }
-               return true;
+               return false; // key not already set
        }
 
        /**
         * @param $key String: Key to increase
         * @param $value Integer: Value to add to $key (Default 1)
         * @return null if lock is not possible else $key value increased by $value
-        * @return success
+        * @return bool success
         */
        public function incr( $key, $value = 1 ) {
                if ( !$this->lock( $key ) ) {
@@ -213,4 +216,23 @@ abstract class BagOStuff {
                        return $exptime;
                }
        }
+
+       /**
+        * Convert an optionally absolute expiry time to a relative time. If an 
+        * absolute time is specified which is in the past, use a short expiry time.
+        *
+        * @param $exptime integer
+        * @return integer
+        */
+       protected function convertToRelative( $exptime ) {
+               if ( $exptime >= 86400 * 3650 /* 10 years */ ) {
+                       $exptime -= time();
+                       if ( $exptime <= 0 ) {
+                               $exptime = 1;
+                       }
+                       return $exptime;
+               } else {
+                       return $exptime;
+               }
+       }
 }