From: Aaron Schulz Date: Tue, 24 Nov 2015 05:41:12 +0000 (-0800) Subject: Add 0 hold-off TTL support to WANObjectCache::delete() X-Git-Tag: 1.31.0-rc.0~8897 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/comptes/journal.php?a=commitdiff_plain;h=680f1527155d50545852ebaa3f9c4c5f90bf65b9;p=lhc%2Fweb%2Fwiklou.git Add 0 hold-off TTL support to WANObjectCache::delete() This removes the peculiar >= 1 restriction Change-Id: I255dc6ef9750ad2ed6234e3fcfed9ae22a038748 --- diff --git a/includes/libs/objectcache/WANObjectCache.php b/includes/libs/objectcache/WANObjectCache.php index 128999a517..16e894e09d 100644 --- a/includes/libs/objectcache/WANObjectCache.php +++ b/includes/libs/objectcache/WANObjectCache.php @@ -101,6 +101,8 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { const TSE_NONE = -1; /** Max TTL to store keys when a data sourced is lagged */ const TTL_LAGGED = 30; + /** Idiom for delete() for "no hold-off" */ + const HOLDOFF_NONE = 0; /** Tiny negative float to use when CTL comes up >= 0 due to clock skew */ const TINY_NEGATIVE = -0.000001; @@ -254,7 +256,8 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { $checkKeyTimesForAll = $this->processCheckKeys( $checksForAll, $wrappedValues, $now ); $checkKeyTimesByKey = array(); foreach ( $checksByKey as $cacheKey => $checks ) { - $checkKeyTimesByKey[$cacheKey] = $this->processCheckKeys( $checks, $wrappedValues, $now ); + $checkKeyTimesByKey[$cacheKey] = + $this->processCheckKeys( $checks, $wrappedValues, $now ); } // Get the main cache value for each key and validate them @@ -456,7 +459,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * * The $ttl parameter can be used when purging values that have not actually changed * recently. For example, a cleanup script to purge cache entries does not really need - * a hold-off period, so it can use the value 1. Likewise for user-requested purge. + * a hold-off period, so it can use HOLDOFF_NONE. Likewise for user-requested purge. * Note that $ttl limits the effective range of 'lockTSE' for getWithSetCallback(). * * If called twice on the same key, then the last hold-off TTL takes precedence. For @@ -468,12 +471,20 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { */ final public function delete( $key, $ttl = self::HOLDOFF_TTL ) { $key = self::VALUE_KEY_PREFIX . $key; - // Avoid indefinite key salting for sanity - $ttl = max( $ttl, 1 ); - // Update the local datacenter immediately - $ok = $this->cache->set( $key, self::PURGE_VAL_PREFIX . microtime( true ), $ttl ); - // Publish the purge to all datacenters - return $this->relayPurge( $key, $ttl ) && $ok; + + if ( $ttl <= 0 ) { + // Update the local datacenter immediately + $ok = $this->cache->delete( $key ); + // Publish the purge to all datacenters + $ok = $this->relayDelete( $key ) && $ok; + } else { + // Update the local datacenter immediately + $ok = $this->cache->set( $key, self::PURGE_VAL_PREFIX . microtime( true ), $ttl ); + // Publish the purge to all datacenters + $ok = $this->relayPurge( $key, $ttl ) && $ok; + } + + return $ok; } /** diff --git a/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php b/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php index 90b4bd01f7..e2c6ac7737 100644 --- a/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php +++ b/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php @@ -391,8 +391,22 @@ class WANObjectCacheTest extends MediaWikiTestCase { $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" ); $this->cache->set( $key, $value . 'more' ); + $v = $this->cache->get( $key, $curTTL ); $this->assertFalse( $v, "Deleted key is tombstoned and has false value" ); $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" ); + + $this->cache->set( $key, $value ); + $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE ); + + $curTTL = null; + $v = $this->cache->get( $key, $curTTL ); + $this->assertFalse( $v, "Deleted key has false value" ); + $this->assertNull( $curTTL, "Deleted key has null current TTL" ); + + $this->cache->set( $key, $value ); + $v = $this->cache->get( $key, $curTTL ); + $this->assertEquals( $value, $v, "Key was created with value" ); + $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" ); } /**