/** @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 */
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
*
* @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
* 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 ) {
* @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
* @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]
// 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
} 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 ];
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
*
* $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;
* },
* @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
*
* // 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 );
* @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
/**
* @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() {
/**
* @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() {
}
/**
- * @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 ) {
* $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
}
}
/**
- * @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() {
* This must set the key to "PURGED:<UNIX timestamp>:<holdoff>"
*
* @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 ) {
* 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
*/
* 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
*/