MediumSpecificBagOStuff: Make convertToRelative(0) return 0, not 1
authorRoan Kattouw <roan.kattouw@gmail.com>
Thu, 25 Jul 2019 23:39:33 +0000 (16:39 -0700)
committerRoan Kattouw <roan.kattouw@gmail.com>
Thu, 25 Jul 2019 23:53:42 +0000 (16:53 -0700)
This matches the (explicitly documented) behavior of convertToExpiry(),
and unbreaks storing things in Redis with an indefinite TTL. Without
this change, any attempt to store something in Redis indefinitely
resulted in it being stored with a TTL of 1 second instead.

Follow-up to I1bebb60307.

Bug: T228744
Change-Id: Ia3f71fb787196b956e974beb9f20d72e58ef3eb4

includes/libs/objectcache/MediumSpecificBagOStuff.php

index e742432..23cf607 100644 (file)
@@ -798,8 +798,8 @@ abstract class MediumSpecificBagOStuff extends BagOStuff {
         *   - positive (< 10 years): relative TTL; return UNIX timestamp offset by this value
         *   - positive (>= 10 years): absolute UNIX timestamp; return this value
         *
-        * @param int $exptime Absolute TTL or 0 for indefinite
-        * @return int
+        * @param int $exptime
+        * @return int Absolute TTL or 0 for indefinite
         */
        final protected function convertToExpiry( $exptime ) {
                return $this->expiryIsRelative( $exptime )
@@ -811,11 +811,17 @@ abstract class MediumSpecificBagOStuff extends BagOStuff {
         * Convert an optionally absolute expiry time to a relative time. If an
         * absolute time is specified which is in the past, use a short expiry time.
         *
+        * The input value will be cast to an integer and interpreted as follows:
+        *   - zero: no expiry; return zero (e.g. TTL_INDEFINITE)
+        *   - negative: relative TTL; return a short expiry time (1 second)
+        *   - positive (< 10 years): relative TTL; return this value
+        *   - positive (>= 10 years): absolute UNIX timestamp; return offset to current time
+        *
         * @param int $exptime
-        * @return int
+        * @return int Relative TTL or 0 for indefinite
         */
        final protected function convertToRelative( $exptime ) {
-               return $this->expiryIsRelative( $exptime )
+               return $this->expiryIsRelative( $exptime ) || !$exptime
                        ? (int)$exptime
                        : max( $exptime - (int)$this->getCurrentTime(), 1 );
        }