Merge "[search] Fix method call on null value"
[lhc/web/wiklou.git] / includes / libs / objectcache / BagOStuff.php
index 5dfec6e..703c195 100644 (file)
@@ -42,7 +42,7 @@ use Psr\Log\NullLogger;
  *
  * @ingroup Cache
  */
-abstract class BagOStuff implements LoggerAwareInterface {
+abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
        /** @var array[] Lock tracking */
        protected $locks = array();
 
@@ -220,7 +220,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
                do {
                        $this->clearLastError();
                        $casToken = null; // passed by reference
-                       $currentValue = $this->getWithToken( $key, $casToken, BagOStuff::READ_LATEST );
+                       $currentValue = $this->getWithToken( $key, $casToken, self::READ_LATEST );
                        if ( $this->getLastError() ) {
                                return false; // don't spam retries (retry only on races)
                        }
@@ -276,7 +276,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
                }
 
                $this->clearLastError();
-               $currentValue = $this->get( $key, BagOStuff::READ_LATEST );
+               $currentValue = $this->get( $key, self::READ_LATEST );
                if ( $this->getLastError() ) {
                        $success = false;
                } else {
@@ -319,7 +319,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
                        }
                }
 
-               $expiry = min( $expiry ?: INF, 86400 );
+               $expiry = min( $expiry ?: INF, self::TTL_DAY );
 
                $this->clearLastError();
                $timestamp = microtime( true ); // starting UNIX timestamp
@@ -328,7 +328,8 @@ abstract class BagOStuff implements LoggerAwareInterface {
                } elseif ( $this->getLastError() || $timeout <= 0 ) {
                        $locked = false; // network partition or non-blocking
                } else {
-                       $uRTT = ceil( 1e6 * ( microtime( true ) - $timestamp ) ); // estimate RTT (us)
+                       // Estimate the RTT (us); use 1ms minimum for sanity
+                       $uRTT = max( 1e3, ceil( 1e6 * ( microtime( true ) - $timestamp ) ) );
                        $sleep = 2 * $uRTT; // rough time to do get()+set()
 
                        $attempts = 0; // failed attempts
@@ -388,7 +389,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @since 1.26
         */
        final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass = '' ) {
-               $expiry = min( $expiry ?: INF, 86400 );
+               $expiry = min( $expiry ?: INF, self::TTL_DAY );
 
                if ( !$this->lock( $key, $timeout, $expiry, $rclass ) ) {
                        return null;
@@ -581,7 +582,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @return int
         */
        protected function convertExpiry( $exptime ) {
-               if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
+               if ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) ) {
                        return time() + $exptime;
                } else {
                        return $exptime;
@@ -596,7 +597,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @return int
         */
        protected function convertToRelative( $exptime ) {
-               if ( $exptime >= 86400 * 3650 /* 10 years */ ) {
+               if ( $exptime >= ( 10 * self::TTL_YEAR ) ) {
                        $exptime -= time();
                        if ( $exptime <= 0 ) {
                                $exptime = 1;
@@ -626,7 +627,11 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @return string
         */
        public function makeKeyInternal( $keyspace, $args ) {
-               $key = $keyspace . ':' . implode( ':', $args );
+               $key = $keyspace;
+               foreach ( $args as $arg ) {
+                       $arg = str_replace( ':', '%3A', $arg );
+                       $key = $key . ':' . $arg;
+               }
                return strtr( $key, ' ', '_' );
        }