objectcache: minor refactoring to BagOStuff
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 26 Mar 2019 22:14:42 +0000 (15:14 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Tue, 26 Mar 2019 22:14:42 +0000 (15:14 -0700)
Split up expiry functions and moved add() near other primitive writes.

Change-Id: I97bb91e648b797b1ab5a6934d212689b1e67f7c6

includes/libs/objectcache/BagOStuff.php
includes/libs/objectcache/HashBagOStuff.php
includes/objectcache/SqlBagOStuff.php

index 4fe64f2..bdfed82 100644 (file)
@@ -260,6 +260,17 @@ abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
         */
        abstract public function delete( $key, $flags = 0 );
 
+       /**
+        * Insert an item if it does not already exist
+        *
+        * @param string $key
+        * @param mixed $value
+        * @param int $exptime
+        * @param int $flags Bitfield of BagOStuff::WRITE_* constants (since 1.33)
+        * @return bool Success
+        */
+       abstract public function add( $key, $value, $exptime = 0, $flags = 0 );
+
        /**
         * Merge changes into the existing cache value (possibly creating a new one)
         *
@@ -587,16 +598,6 @@ abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
                return $res;
        }
 
-       /**
-        * Insertion
-        * @param string $key
-        * @param mixed $value
-        * @param int $exptime
-        * @param int $flags Bitfield of BagOStuff::WRITE_* constants (since 1.33)
-        * @return bool Success
-        */
-       abstract public function add( $key, $value, $exptime = 0, $flags = 0 );
-
        /**
         * Increase stored value of $key by $value while preserving its TTL
         * @param string $key Key to increase
@@ -703,13 +704,21 @@ abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
                }
        }
 
+       /**
+        * @param int $exptime
+        * @return bool
+        */
+       protected function expiryIsRelative( $exptime ) {
+               return ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) );
+       }
+
        /**
         * Convert an optionally relative time to an absolute time
         * @param int $exptime
         * @return int
         */
-       protected function convertExpiry( $exptime ) {
-               if ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) ) {
+       protected function convertToExpiry( $exptime ) {
+               if ( $this->expiryIsRelative( $exptime ) ) {
                        return (int)$this->getCurrentTime() + $exptime;
                } else {
                        return $exptime;
index 3c6520e..eaea2d1 100644 (file)
@@ -91,7 +91,7 @@ class HashBagOStuff extends BagOStuff {
                unset( $this->bag[$key] );
                $this->bag[$key] = [
                        self::KEY_VAL => $value,
-                       self::KEY_EXP => $this->convertExpiry( $exptime ),
+                       self::KEY_EXP => $this->convertToExpiry( $exptime ),
                        self::KEY_CAS => $this->token . ':' . ++self::$casCounter
                ];
 
index b2d61a8..e450212 100644 (file)
@@ -344,7 +344,7 @@ class SqlBagOStuff extends BagOStuff {
                        if ( $exptime == 0 ) {
                                $encExpiry = $this->getMaxDateTime( $db );
                        } else {
-                               $exptime = $this->convertExpiry( $exptime );
+                               $exptime = $this->convertToExpiry( $exptime );
                                $encExpiry = $db->timestamp( $exptime );
                        }
                        foreach ( $serverKeys as $tableName => $tableKeys ) {
@@ -406,7 +406,7 @@ class SqlBagOStuff extends BagOStuff {
                        if ( $exptime == 0 ) {
                                $encExpiry = $this->getMaxDateTime( $db );
                        } else {
-                               $exptime = $this->convertExpiry( $exptime );
+                               $exptime = $this->convertToExpiry( $exptime );
                                $encExpiry = $db->timestamp( $exptime );
                        }
                        // (T26425) use a replace if the db supports it instead of
@@ -542,7 +542,7 @@ class SqlBagOStuff extends BagOStuff {
                        $db = $this->getDB( $serverIndex );
                        $db->update(
                                $tableName,
-                               [ 'exptime' => $db->timestamp( $this->convertExpiry( $expiry ) ) ],
+                               [ 'exptime' => $db->timestamp( $this->convertToExpiry( $expiry ) ) ],
                                [ 'keyname' => $key, 'exptime > ' . $db->addQuotes( $db->timestamp( time() ) ) ],
                                __METHOD__
                        );