objectcache: Fixes WinCache increment losing TTL.
authorJuan Osorio <osorio.juan@microsoft.com>
Thu, 13 Dec 2018 19:31:17 +0000 (11:31 -0800)
committerJuan Osorio <osorio.juan@microsoft.com>
Thu, 13 Dec 2018 22:36:06 +0000 (14:36 -0800)
WinCache incr function adds a number to the value of a key, but
in the process loses the Time To Live of the cache entry. This
fixes such bug.

Bug: T211914
Change-Id: I02889e63b95188b8d5123a9788a020ca3aa7d1b1

includes/libs/objectcache/WinCacheBagOStuff.php

index 9c6c907..764230b 100644 (file)
@@ -97,4 +97,27 @@ class WinCacheBagOStuff extends BagOStuff {
 
                return $keyspace . ':' . implode( ':', $args );
        }
+
+       /**
+        * Increase stored value of $key by $value while preserving its original TTL
+        * @param string $key Key to increase
+        * @param int $value Value to add to $key (Default 1)
+        * @return int|bool New value or false on failure
+        */
+       public function incr( $key, $value = 1 ) {
+               if ( !$this->lock( $key ) ) {
+                       return false;
+               }
+               $n = $this->get( $key );
+               if ( $this->isInteger( $n ) ) { // key exists?
+                       $n += intval( $value );
+                       $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
+                       $this->set( $key, max( 0, $n ), $oldTTL );
+               } else {
+                       $n = false;
+               }
+               $this->unlock( $key );
+
+               return $n;
+       }
 }