From d5afd5f9096b2c61b5be66e74e633f9e6af78c3c Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 26 Mar 2019 15:14:42 -0700 Subject: [PATCH] objectcache: minor refactoring to BagOStuff Split up expiry functions and moved add() near other primitive writes. Change-Id: I97bb91e648b797b1ab5a6934d212689b1e67f7c6 --- includes/libs/objectcache/BagOStuff.php | 33 +++++++++++++-------- includes/libs/objectcache/HashBagOStuff.php | 2 +- includes/objectcache/SqlBagOStuff.php | 6 ++-- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/includes/libs/objectcache/BagOStuff.php b/includes/libs/objectcache/BagOStuff.php index 4fe64f2641..bdfed82f05 100644 --- a/includes/libs/objectcache/BagOStuff.php +++ b/includes/libs/objectcache/BagOStuff.php @@ -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; diff --git a/includes/libs/objectcache/HashBagOStuff.php b/includes/libs/objectcache/HashBagOStuff.php index 3c6520e1b3..eaea2d15b7 100644 --- a/includes/libs/objectcache/HashBagOStuff.php +++ b/includes/libs/objectcache/HashBagOStuff.php @@ -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 ]; diff --git a/includes/objectcache/SqlBagOStuff.php b/includes/objectcache/SqlBagOStuff.php index b2d61a8925..e450212dc6 100644 --- a/includes/objectcache/SqlBagOStuff.php +++ b/includes/objectcache/SqlBagOStuff.php @@ -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__ ); -- 2.20.1