Merge "jquery.tablesorter: Never initialize twice on the same element"
[lhc/web/wiklou.git] / includes / libs / objectcache / RESTBagOStuff.php
index fc3d575..c127ec6 100644 (file)
@@ -124,16 +124,9 @@ class RESTBagOStuff extends BagOStuff {
                return false;
        }
 
-       /**
-        * Set an item
-        *
-        * @param string $key
-        * @param mixed $value
-        * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
-        * @param int $flags Bitfield of BagOStuff::WRITE_* constants
-        * @return bool Success
-        */
        public function set( $key, $value, $exptime = 0, $flags = 0 ) {
+               // @TODO: respect WRITE_SYNC (e.g. EACH_QUORUM)
+               // @TODO: respect $exptime
                $req = [
                        'method' => 'PUT',
                        'url' => $this->url . rawurlencode( $key ),
@@ -146,13 +139,17 @@ class RESTBagOStuff extends BagOStuff {
                return $this->handleError( "Failed to store $key", $rcode, $rerr );
        }
 
-       /**
-        * Delete an item.
-        *
-        * @param string $key
-        * @return bool True if the item was deleted or not found, false on failure
-        */
-       public function delete( $key ) {
+       public function add( $key, $value, $exptime = 0, $flags = 0 ) {
+               // @TODO: make this atomic
+               if ( $this->get( $key ) === false ) {
+                       return $this->set( $key, $value, $exptime, $flags );
+               }
+
+               return false; // key already set
+       }
+
+       public function delete( $key, $flags = 0 ) {
+               // @TODO: respect WRITE_SYNC (e.g. EACH_QUORUM)
                $req = [
                        'method' => 'DELETE',
                        'url' => $this->url . rawurlencode( $key ),
@@ -163,4 +160,16 @@ class RESTBagOStuff extends BagOStuff {
                }
                return $this->handleError( "Failed to delete $key", $rcode, $rerr );
        }
+
+       public function incr( $key, $value = 1 ) {
+               // @TODO: make this atomic
+               $n = $this->get( $key, self::READ_LATEST );
+               if ( $this->isInteger( $n ) ) { // key exists?
+                       $n = max( $n + intval( $value ), 0 );
+                       // @TODO: respect $exptime
+                       return $this->set( $key, $n ) ? $n : false;
+               }
+
+               return false;
+       }
 }