jenkins-bot has submitted this change and it was merged. Change subject: objectcache: Add changeTTL() method ......................................................................
objectcache: Add changeTTL() method This can change the TTL without fetching the object so that keys can be renewed or set to expire soon. Change-Id: Id1c2c9f89b3445cfa34263057dc5029cbe170833 --- M includes/libs/objectcache/BagOStuff.php M includes/libs/objectcache/MemcachedBagOStuff.php M includes/libs/objectcache/MemcachedClient.php M includes/objectcache/MemcachedPeclBagOStuff.php M includes/objectcache/RedisBagOStuff.php M tests/phpunit/includes/libs/objectcache/BagOStuffTest.php 6 files changed, 102 insertions(+), 3 deletions(-) Approvals: BryanDavis: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/libs/objectcache/BagOStuff.php b/includes/libs/objectcache/BagOStuff.php index 25a5a26..5472e83 100644 --- a/includes/libs/objectcache/BagOStuff.php +++ b/includes/libs/objectcache/BagOStuff.php @@ -373,6 +373,20 @@ } /** + * Reset the TTL on a key if it exists + * + * @param string $key + * @param int $expiry + * @return bool Success Returns false if there is no key + * @since 1.28 + */ + public function changeTTL( $key, $expiry = 0 ) { + $value = $this->get( $key ); + + return ( $value === false ) ? false : $this->set( $key, $value, $expiry ); + } + + /** * Acquire an advisory lock on a key string * * Note that if reentry is enabled, duplicate calls ignore $expiry diff --git a/includes/libs/objectcache/MemcachedBagOStuff.php b/includes/libs/objectcache/MemcachedBagOStuff.php index ba8c736..5967441 100644 --- a/includes/libs/objectcache/MemcachedBagOStuff.php +++ b/includes/libs/objectcache/MemcachedBagOStuff.php @@ -79,6 +79,11 @@ return $this->mergeViaCas( $key, $callback, $exptime, $attempts ); } + public function changeTTL( $key, $exptime = 0 ) { + return $this->client->touch( $this->validateKeyEncoding( $key ), + $this->fixExpiry( $exptime ) ); + } + /** * Get the underlying client object. This is provided for debugging * purposes. diff --git a/includes/libs/objectcache/MemcachedClient.php b/includes/libs/objectcache/MemcachedClient.php index 668135d..c3fcab9 100644 --- a/includes/libs/objectcache/MemcachedClient.php +++ b/includes/libs/objectcache/MemcachedClient.php @@ -361,6 +361,48 @@ } /** + * Changes the TTL on a key from the server to $time + * + * @param string $key Key + * @param int $time TTL in seconds + * + * @return bool True on success, false on failure + */ + public function touch( $key, $time = 0 ) { + if ( !$this->_active ) { + return false; + } + + $sock = $this->get_sock( $key ); + if ( !is_resource( $sock ) ) { + return false; + } + + $key = is_array( $key ) ? $key[1] : $key; + + if ( isset( $this->stats['touch'] ) ) { + $this->stats['touch']++; + } else { + $this->stats['touch'] = 1; + } + $cmd = "touch $key $time\r\n"; + if ( !$this->_fwrite( $sock, $cmd ) ) { + return false; + } + $res = $this->_fgets( $sock ); + + if ( $this->_debug ) { + $this->_debugprint( sprintf( "MemCache: touch %s (%s)", $key, $res ) ); + } + + if ( $res == "TOUCHED" ) { + return true; + } + + return false; + } + + /** * @param string $key * @param int $timeout * @return bool diff --git a/includes/objectcache/MemcachedPeclBagOStuff.php b/includes/objectcache/MemcachedPeclBagOStuff.php index 090ace8..bb760bd 100644 --- a/includes/objectcache/MemcachedPeclBagOStuff.php +++ b/includes/objectcache/MemcachedPeclBagOStuff.php @@ -226,4 +226,10 @@ $result = $this->client->setMulti( $data, $this->fixExpiry( $exptime ) ); return $this->checkResult( false, $result ); } + + public function changeTTL( $key, $expiry = 0 ) { + $this->debugLog( "touch($key)" ); + $result = $this->client->touch( $key, $expiry ); + return $this->checkResult( $key, $result ); + } } diff --git a/includes/objectcache/RedisBagOStuff.php b/includes/objectcache/RedisBagOStuff.php index 90508da..c3e0c96 100644 --- a/includes/objectcache/RedisBagOStuff.php +++ b/includes/objectcache/RedisBagOStuff.php @@ -272,10 +272,10 @@ if ( !$conn ) { return false; } - if ( !$conn->exists( $key ) ) { - return null; - } try { + if ( !$conn->exists( $key ) ) { + return null; + } // @FIXME: on races, the key may have a 0 TTL $result = $conn->incrBy( $key, $value ); } catch ( RedisException $e ) { @@ -287,6 +287,24 @@ return $result; } + public function changeTTL( $key, $expiry = 0 ) { + list( $server, $conn ) = $this->getConnection( $key ); + if ( !$conn ) { + return false; + } + + $expiry = $this->convertToRelative( $expiry ); + try { + $result = $conn->expire( $key, $expiry ); + } catch ( RedisException $e ) { + $result = false; + $this->handleException( $conn, $e ); + } + + $this->logRequest( 'expire', $key, $server, $result ); + return $result; + } + public function modifySimpleRelayEvent( array $event ) { if ( array_key_exists( 'val', $event ) ) { $event['val'] = serialize( $event['val'] ); // this class uses PHP serialization diff --git a/tests/phpunit/includes/libs/objectcache/BagOStuffTest.php b/tests/phpunit/includes/libs/objectcache/BagOStuffTest.php index a8beb91..92fb954 100644 --- a/tests/phpunit/includes/libs/objectcache/BagOStuffTest.php +++ b/tests/phpunit/includes/libs/objectcache/BagOStuffTest.php @@ -139,6 +139,20 @@ } /** + * @covers BagOStuff::changeTTL + */ + public function testChangeTTL() { + $key = wfMemcKey( 'test' ); + $value = 'meow'; + + $this->cache->add( $key, $value ); + $this->assertTrue( $this->cache->changeTTL( $key, 5 ) ); + $this->assertEquals( $this->cache->get( $key ), $value ); + $this->cache->delete( $key ); + $this->assertFalse( $this->cache->changeTTL( $key, 5 ) ); + } + + /** * @covers BagOStuff::add */ public function testAdd() { -- To view, visit https://gerrit.wikimedia.org/r/304428 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Id1c2c9f89b3445cfa34263057dc5029cbe170833 Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org> Gerrit-Reviewer: BryanDavis <bda...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits