Added merge() function to BagOStuff for CAS-like functionality.
[lhc/web/wiklou.git] / includes / objectcache / DBABagOStuff.php
index 36ced49..ee2500d 100644 (file)
@@ -111,9 +111,10 @@ class DBABagOStuff extends BagOStuff {
 
        /**
         * @param $key string
+        * @param $casToken[optional] mixed
         * @return mixed
         */
-       public function get( $key ) {
+       public function get( $key, &$casToken = null ) {
                wfProfileIn( __METHOD__ );
                wfDebug( __METHOD__ . "($key)\n" );
 
@@ -138,7 +139,10 @@ class DBABagOStuff extends BagOStuff {
                        $val = false;
                }
 
+               $casToken = $val;
+
                wfProfileOut( __METHOD__ );
+
                return $val;
        }
 
@@ -167,6 +171,41 @@ class DBABagOStuff extends BagOStuff {
                return $ret;
        }
 
+       /**
+        * @param $casToken mixed
+        * @param $key string
+        * @param $value mixed
+        * @param $exptime int
+        * @return bool
+        */
+       public function cas( $casToken, $key, $value, $exptime = 0 ) {
+               wfProfileIn( __METHOD__ );
+               wfDebug( __METHOD__ . "($key)\n" );
+
+               $blob = $this->encode( $value, $exptime );
+
+               $handle = $this->getWriter();
+               if ( !$handle ) {
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               }
+
+               // DBA is locked to any other write connection, so we can safely
+               // compare the current & previous value before saving new value
+               $val = dba_fetch( $key, $handle );
+               list( $val, $exptime ) = $this->decode( $val );
+               if ( $casToken !== $val ) {
+                       dba_close( $handle );
+                       return false;
+               }
+
+               $ret = dba_replace( $key, $blob, $handle );
+               dba_close( $handle );
+
+               wfProfileOut( __METHOD__ );
+               return $ret;
+       }
+
        /**
         * @param $key string
         * @param $time int