Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / libs / objectcache / MemcachedBagOStuff.php
index ff9dedf..40f2836 100644 (file)
@@ -49,29 +49,25 @@ abstract class MemcachedBagOStuff extends MediumSpecificBagOStuff {
                // custom prefixes used by thing like WANObjectCache, limit to 205.
                $charsLeft = 205 - strlen( $keyspace ) - count( $args );
 
-               $args = array_map(
-                       function ( $arg ) use ( &$charsLeft ) {
-                               $arg = strtr( $arg, ' ', '_' );
+               foreach ( $args as &$arg ) {
+                       $arg = strtr( $arg, ' ', '_' );
 
-                               // Make sure %, #, and non-ASCII chars are escaped
-                               $arg = preg_replace_callback(
-                                       '/[^\x21-\x22\x24\x26-\x39\x3b-\x7e]+/',
-                                       function ( $m ) {
-                                               return rawurlencode( $m[0] );
-                                       },
-                                       $arg
-                               );
+                       // Make sure %, #, and non-ASCII chars are escaped
+                       $arg = preg_replace_callback(
+                               '/[^\x21-\x22\x24\x26-\x39\x3b-\x7e]+/',
+                               function ( $m ) {
+                                       return rawurlencode( $m[0] );
+                               },
+                               $arg
+                       );
 
-                               // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
-                               if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
-                                       $arg = '#' . md5( $arg );
-                               }
+                       // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
+                       if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
+                               $arg = '#' . md5( $arg );
+                       }
 
-                               $charsLeft -= strlen( $arg );
-                               return $arg;
-                       },
-                       $args
-               );
+                       $charsLeft -= strlen( $arg );
+               }
 
                if ( $charsLeft < 0 ) {
                        return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
@@ -96,17 +92,24 @@ abstract class MemcachedBagOStuff extends MediumSpecificBagOStuff {
        }
 
        /**
-        * TTLs higher than 30 days will be detected as absolute TTLs
-        * (UNIX timestamps), and will result in the cache entry being
-        * discarded immediately because the expiry is in the past.
-        * Clamp expires >30d at 30d, unless they're >=1e9 in which
-        * case they are likely to really be absolute (1e9 = 2011-09-09)
-        * @param int $exptime
+        * @param int|float $exptime
         * @return int
         */
        protected function fixExpiry( $exptime ) {
-               return ( $exptime > self::TTL_MONTH && !$this->isRelativeExpiration( $exptime ) )
-                       ? self::TTL_MONTH
-                       : (int)$exptime;
+               if ( $exptime < 0 ) {
+                       // The PECL driver does not seem to like negative relative values
+                       $expiresAt = $this->getCurrentTime() + $exptime;
+               } elseif ( $this->isRelativeExpiration( $exptime ) ) {
+                       // TTLs higher than 30 days will be detected as absolute TTLs
+                       // (UNIX timestamps), and will result in the cache entry being
+                       // discarded immediately because the expiry is in the past.
+                       // Clamp expires >30d at 30d, unless they're >=1e9 in which
+                       // case they are likely to really be absolute (1e9 = 2011-09-09)
+                       $expiresAt = min( $exptime, self::TTL_MONTH );
+               } else {
+                       $expiresAt = $exptime;
+               }
+
+               return (int)$expiresAt;
        }
 }