X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2Flibs%2Fobjectcache%2FWANObjectCache.php;h=15e5759ce8596eb9da62e8930b67f0abb20a4ef4;hp=a80ed8d66354491c473181473e6025d205baf6a5;hb=1380b0dbeaa37aab3d46e44a77fe16034296a93d;hpb=6c9a2923fe1ee3a65cb027be5e781772f2b12fbd diff --git a/includes/libs/objectcache/WANObjectCache.php b/includes/libs/objectcache/WANObjectCache.php index a80ed8d663..15e5759ce8 100644 --- a/includes/libs/objectcache/WANObjectCache.php +++ b/includes/libs/objectcache/WANObjectCache.php @@ -92,11 +92,11 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { /** @var int ERR_* constant for the "last error" registry */ protected $lastRelayError = self::ERR_NONE; - /** @var integer Callback stack depth for getWithSetCallback() */ + /** @var int Callback stack depth for getWithSetCallback() */ private $callbackDepth = 0; /** @var mixed[] Temporary warm-up cache */ private $warmupCache = []; - /** @var integer Key fetched */ + /** @var int Key fetched */ private $warmupKeyMisses = 0; /** Max time expected to pass between delete() and DB commit finishing */ @@ -148,7 +148,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { const FLD_FLAGS = 4; // key to the flags bitfield const FLD_HOLDOFF = 5; // key to any hold-off TTL - /** @var integer Treat this value as expired-on-arrival */ + /** @var int Treat this value as expired-on-arrival */ const FLG_STALE = 1; const ERR_NONE = 0; // no error @@ -404,7 +404,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * * @param string $key Cache key * @param mixed $value - * @param integer $ttl Seconds to live. Special values are: + * @param int $ttl Seconds to live. Special values are: * - WANObjectCache::TTL_INDEFINITE: Cache forever * @param array $opts Options map: * - lag : Seconds of replica DB lag. Typically, this is either the replica DB lag @@ -537,7 +537,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * idempotence, the $ttl should not vary for different delete() calls on the same key. * * @param string $key Cache key - * @param integer $ttl Tombstone TTL; Default: WANObjectCache::HOLDOFF_TTL + * @param int $ttl Tombstone TTL; Default: WANObjectCache::HOLDOFF_TTL * @return bool True if the item was purged or not found, false on failure */ final public function delete( $key, $ttl = self::HOLDOFF_TTL ) { @@ -797,7 +797,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * @see WANObjectCache::set() * * @param string $key Cache key - * @param integer $ttl Seconds to live for key updates. Special values are: + * @param int $ttl Seconds to live for key updates. Special values are: * - WANObjectCache::TTL_INDEFINITE: Cache forever * - WANObjectCache::TTL_UNCACHEABLE: Do not cache at all * @param callable $callback Value generation function @@ -929,7 +929,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * @see WANObjectCache::getWithSetCallback() * * @param string $key - * @param integer $ttl + * @param int $ttl * @param callback $callback * @param array $opts Options map for getWithSetCallback() * @param float &$asOf Cache generation timestamp of returned value [returned] @@ -987,11 +987,8 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { // Use the INTERIM value for tombstoned keys to reduce regeneration load. // For hot keys, either another thread has the lock or the lock failed; // use the INTERIM value from the last thread that regenerated it. - $wrapped = $this->cache->get( self::INTERIM_KEY_PREFIX . $key ); - list( $value ) = $this->unwrap( $wrapped, microtime( true ) ); - if ( $value !== false && $this->isValid( $value, $versioned, $asOf, $minTime ) ) { - $asOf = $wrapped[self::FLD_TIME]; - + $value = $this->getInterimValue( $key, $versioned, $minTime, $asOf ); + if ( $value !== false ) { return $value; } // Use the busy fallback value if nothing else @@ -1013,24 +1010,19 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { } finally { --$this->callbackDepth; } + $valueIsCacheable = ( $value !== false && $ttl >= 0 ); + // When delete() is called, writes are write-holed by the tombstone, // so use a special INTERIM key to pass the new value around threads. - if ( ( $isTombstone && $lockTSE > 0 ) && $value !== false && $ttl >= 0 ) { + if ( ( $isTombstone && $lockTSE > 0 ) && $valueIsCacheable ) { $tempTTL = max( 1, (int)$lockTSE ); // set() expects seconds $newAsOf = microtime( true ); $wrapped = $this->wrap( $value, $tempTTL, $newAsOf ); // Avoid using set() to avoid pointless mcrouter broadcasting - $this->cache->merge( - self::INTERIM_KEY_PREFIX . $key, - function () use ( $wrapped ) { - return $wrapped; - }, - $tempTTL, - 1 - ); + $this->setInterimValue( $key, $wrapped, $tempTTL ); } - if ( $value !== false && $ttl >= 0 ) { + if ( $valueIsCacheable ) { $setOpts['lockTSE'] = $lockTSE; // Use best known "since" timestamp if not provided $setOpts += [ 'since' => $preCallbackTime ]; @@ -1046,6 +1038,41 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { return $value; } + /** + * @param string $key + * @param bool $versioned + * @param float $minTime + * @param mixed $asOf + * @return mixed + */ + protected function getInterimValue( $key, $versioned, $minTime, &$asOf ) { + $wrapped = $this->cache->get( self::INTERIM_KEY_PREFIX . $key ); + list( $value ) = $this->unwrap( $wrapped, microtime( true ) ); + if ( $value !== false && $this->isValid( $value, $versioned, $asOf, $minTime ) ) { + $asOf = $wrapped[self::FLD_TIME]; + + return $value; + } + + return false; + } + + /** + * @param string $key + * @param array $wrapped + * @param int $tempTTL + */ + protected function setInterimValue( $key, $wrapped, $tempTTL ) { + $this->cache->merge( + self::INTERIM_KEY_PREFIX . $key, + function () use ( $wrapped ) { + return $wrapped; + }, + $tempTTL, + 1 + ); + } + /** * Method to fetch multiple cache keys at once with regeneration * @@ -1083,7 +1110,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * $setOpts += Database::getCacheSetOptions( $dbr ); * * // Load the row for this file - * $row = $dbr->selectRow( 'file', '*', [ 'id' => $id ], __METHOD__ ); + * $row = $dbr->selectRow( 'file', File::selectFields(), [ 'id' => $id ], __METHOD__ ); * * return $row ? (array)$row : false; * }, @@ -1098,7 +1125,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * @endcode * * @param ArrayIterator $keyedIds Result of WANObjectCache::makeMultiKeys() - * @param integer $ttl Seconds to live for key updates + * @param int $ttl Seconds to live for key updates * @param callable $callback Callback the yields entity regeneration callbacks * @param array $opts Options map * @return array Map of (cache key => value) in the same order as $keyedIds @@ -1169,7 +1196,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * * // Load the rows for these files * $rows = []; - * $res = $dbr->select( 'file', '*', [ 'id' => $ids ], __METHOD__ ); + * $res = $dbr->select( 'file', File::selectFields(), [ 'id' => $ids ], __METHOD__ ); * foreach ( $res as $row ) { * $rows[$row->id] = $row; * $mtime = wfTimestamp( TS_UNIX, $row->timestamp ); @@ -1184,7 +1211,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * @endcode * * @param ArrayIterator $keyedIds Result of WANObjectCache::makeMultiKeys() - * @param integer $ttl Seconds to live for key updates + * @param int $ttl Seconds to live for key updates * @param callable $callback Callback the yields entity regeneration callbacks * @param array $opts Options map * @return array Map of (cache key => value) in the same order as $keyedIds @@ -1315,8 +1342,8 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { /** * @see BagOStuff::makeKey() - * @param string $keys,... Key component - * @return string + * @param string $keys,... Key component (starting with a key collection name) + * @return string Colon-delimited list of $keyspace followed by escaped components of $args * @since 1.27 */ public function makeKey() { @@ -1325,8 +1352,8 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { /** * @see BagOStuff::makeGlobalKey() - * @param string $keys,... Key component - * @return string + * @param string $keys,... Key component (starting with a key collection name) + * @return string Colon-delimited list of $keyspace followed by escaped components of $args * @since 1.27 */ public function makeGlobalKey() { @@ -1393,8 +1420,8 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { } /** - * @param integer $flag ATTR_* class constant - * @return integer QOS_* class constant + * @param int $flag ATTR_* class constant + * @return int QOS_* class constant * @since 1.28 */ public function getQoS( $flag ) { @@ -1417,14 +1444,14 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * $ttl = $cache->adaptiveTTL( $mtime, $cache::TTL_DAY ); * @endcode * - * @param integer|float $mtime UNIX timestamp - * @param integer $maxTTL Maximum TTL (seconds) - * @param integer $minTTL Minimum TTL (seconds); Default: 30 + * @param int|float $mtime UNIX timestamp + * @param int $maxTTL Maximum TTL (seconds) + * @param int $minTTL Minimum TTL (seconds); Default: 30 * @param float $factor Value in the range (0,1); Default: .2 - * @return integer Adaptive TTL + * @return int Adaptive TTL * @since 1.28 */ - public function adaptiveTTL( $mtime, $maxTTL, $minTTL = 30, $factor = .2 ) { + public function adaptiveTTL( $mtime, $maxTTL, $minTTL = 30, $factor = 0.2 ) { if ( is_float( $mtime ) || ctype_digit( $mtime ) ) { $mtime = (int)$mtime; // handle fractional seconds and string integers } @@ -1439,7 +1466,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { } /** - * @return integer Number of warmup key cache misses last round + * @return int Number of warmup key cache misses last round * @since 1.30 */ public function getWarmupKeyMisses() { @@ -1452,8 +1479,8 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * This must set the key to "PURGED::" * * @param string $key Cache key - * @param integer $ttl How long to keep the tombstone [seconds] - * @param integer $holdoff HOLDOFF_* constant controlling how long to ignore sets for this key + * @param int $ttl How long to keep the tombstone [seconds] + * @param int $holdoff HOLDOFF_* constant controlling how long to ignore sets for this key * @return bool Success */ protected function relayPurge( $key, $ttl, $holdoff ) { @@ -1540,8 +1567,8 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * and get hits too. Similar to worthRefreshExpiring(), randomization is used. * * @param float $asOf UNIX timestamp of the value - * @param integer $ageNew Age of key when this might recommend refreshing (seconds) - * @param integer $timeTillRefresh Age of key when it should be refreshed if popular (seconds) + * @param int $ageNew Age of key when this might recommend refreshing (seconds) + * @param int $timeTillRefresh Age of key when it should be refreshed if popular (seconds) * @param float $now The current UNIX timestamp * @return bool */ @@ -1590,7 +1617,7 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * Do not use this method outside WANObjectCache * * @param mixed $value - * @param integer $ttl [0=forever] + * @param int $ttl [0=forever] * @param float $now Unix Current timestamp just before calling set() * @return array */