From: Timo Tijhof Date: Mon, 19 Oct 2015 17:52:19 +0000 (+0100) Subject: objectcache: Introduce IExpiringStore for convenient TTL constants X-Git-Tag: 1.31.0-rc.0~9200^2 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22lang_raccourcis%22%2C%22module=%24nom_module%22%29%20.%20%22?a=commitdiff_plain;h=e8275758fec2a2f1195e528dff43dec5311322a1;p=lhc%2Fweb%2Fwiklou.git objectcache: Introduce IExpiringStore for convenient TTL constants Also consistently use self:: instead of BagOStuff:: for constants referenced within the BagOStuff class. Change-Id: I20fde9fa5cddcc9e92fa6a02b05dc7effa846742 --- diff --git a/autoload.php b/autoload.php index d7bccb363a..2bcf6b5dc3 100644 --- a/autoload.php +++ b/autoload.php @@ -541,6 +541,7 @@ $wgAutoloadLocalClasses = array( 'IDatabase' => __DIR__ . '/includes/db/IDatabase.php', 'IEContentAnalyzer' => __DIR__ . '/includes/libs/IEContentAnalyzer.php', 'IEUrlExtension' => __DIR__ . '/includes/libs/IEUrlExtension.php', + 'IExpiringStore' => __DIR__ . '/includes/libs/objectcache/IExpiringStore.php', 'IJobSpecification' => __DIR__ . '/includes/jobqueue/JobSpecification.php', 'IORMRow' => __DIR__ . '/includes/db/IORMRow.php', 'IORMTable' => __DIR__ . '/includes/db/IORMTable.php', diff --git a/includes/Block.php b/includes/Block.php index 5dee23ea4a..b57b3e8b92 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -681,10 +681,10 @@ class Block { public static function isWhitelistedFromAutoblocks( $ip ) { // Try to get the autoblock_whitelist from the cache, as it's faster // than getting the msg raw and explode()'ing it. - - $lines = ObjectCache::getMainWANInstance()->getWithSetCallback( + $cache = ObjectCache::getMainWANInstance(); + $lines = $cache->getWithSetCallback( wfMemcKey( 'ipb', 'autoblock', 'whitelist' ), - 86400, + $cache::TTL_DAY, function () { return explode( "\n", wfMessage( 'autoblock_whitelist' )->inContentLanguage()->plain() ); diff --git a/includes/Collation.php b/includes/Collation.php index 40e8627187..7fa5c6e477 100644 --- a/includes/Collation.php +++ b/includes/Collation.php @@ -508,7 +508,7 @@ class IcuCollation extends Collation { // Save to cache $this->firstLetterData = $data; - $cache->set( $cacheKey, $data, 86400 * 7 /* 1 week */ ); + $cache->set( $cacheKey, $data, $cache::TTL_WEEK ); return $data; } diff --git a/includes/SiteStats.php b/includes/SiteStats.php index 5b361b9be7..33bab6573f 100644 --- a/includes/SiteStats.php +++ b/includes/SiteStats.php @@ -180,9 +180,10 @@ class SiteStats { * @return int */ static function numberingroup( $group ) { - return ObjectCache::getMainWANInstance()->getWithSetCallback( + $cache = ObjectCache::getMainWANInstance(); + return $cache->getWithSetCallback( wfMemcKey( 'SiteStats', 'groupcounts', $group ), - 3600, + $cache::TTL_HOUR, function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) { $dbr = wfGetDB( DB_SLAVE ); diff --git a/includes/User.php b/includes/User.php index 1727a4a5c7..a6b897dd92 100644 --- a/includes/User.php +++ b/includes/User.php @@ -438,10 +438,11 @@ class User implements IDBAccessObject { $data[$name] = $this->$name; } $data['mVersion'] = self::VERSION; - $key = wfMemcKey( 'user', 'id', $this->mId ); - $opts = Database::getCacheSetOptions( wfGetDB( DB_SLAVE ) ); - ObjectCache::getMainWANInstance()->set( $key, $data, 3600, $opts ); + + $cache = ObjectCache::getMainWANInstance(); + $key = wfMemcKey( 'user', 'id', $this->mId ); + $cache->set( $key, $data, $cache::TTL_HOUR, $opts ); } /** @name newFrom*() static factory methods */ diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index 0a3314e973..f898bb6aff 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -55,11 +55,11 @@ class ForeignAPIRepo extends FileRepo { ); protected $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' ); - /** @var int Check back with Commons after a day (24*60*60) */ - protected $apiThumbCacheExpiry = 86400; + /** @var int Check back with Commons after this expiry */ + protected $apiThumbCacheExpiry = 86400; // 1 day (24*3600) - /** @var int Redownload thumbnail files after a month (86400*30) */ - protected $fileCacheExpiry = 2592000; + /** @var int Redownload thumbnail files after this expiry */ + protected $fileCacheExpiry = 2592000; // 1 month (30*24*3600) /** @var array */ protected $mFileExists = array(); diff --git a/includes/jobqueue/JobQueueGroup.php b/includes/jobqueue/JobQueueGroup.php index a702d59049..4609d2d37e 100644 --- a/includes/jobqueue/JobQueueGroup.php +++ b/includes/jobqueue/JobQueueGroup.php @@ -394,7 +394,11 @@ class JobQueueGroup { return $value['v']; } else { $value = $wgConf->getConfig( $this->wiki, $name ); - $cache->set( $key, array( 'v' => $value ), 86400 + mt_rand( 0, 86400 ) ); + $cache->set( + $key, + array( 'v' => $value ), + $cache::TTL_DAY + mt_rand( 0, $cache::TTL_DAY ) + ); return $value; } diff --git a/includes/libs/objectcache/BagOStuff.php b/includes/libs/objectcache/BagOStuff.php index ecc5e372f3..703c195a2b 100644 --- a/includes/libs/objectcache/BagOStuff.php +++ b/includes/libs/objectcache/BagOStuff.php @@ -42,7 +42,7 @@ use Psr\Log\NullLogger; * * @ingroup Cache */ -abstract class BagOStuff implements LoggerAwareInterface { +abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface { /** @var array[] Lock tracking */ protected $locks = array(); @@ -220,7 +220,7 @@ abstract class BagOStuff implements LoggerAwareInterface { do { $this->clearLastError(); $casToken = null; // passed by reference - $currentValue = $this->getWithToken( $key, $casToken, BagOStuff::READ_LATEST ); + $currentValue = $this->getWithToken( $key, $casToken, self::READ_LATEST ); if ( $this->getLastError() ) { return false; // don't spam retries (retry only on races) } @@ -276,7 +276,7 @@ abstract class BagOStuff implements LoggerAwareInterface { } $this->clearLastError(); - $currentValue = $this->get( $key, BagOStuff::READ_LATEST ); + $currentValue = $this->get( $key, self::READ_LATEST ); if ( $this->getLastError() ) { $success = false; } else { @@ -319,7 +319,7 @@ abstract class BagOStuff implements LoggerAwareInterface { } } - $expiry = min( $expiry ?: INF, 86400 ); + $expiry = min( $expiry ?: INF, self::TTL_DAY ); $this->clearLastError(); $timestamp = microtime( true ); // starting UNIX timestamp @@ -389,7 +389,7 @@ abstract class BagOStuff implements LoggerAwareInterface { * @since 1.26 */ final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass = '' ) { - $expiry = min( $expiry ?: INF, 86400 ); + $expiry = min( $expiry ?: INF, self::TTL_DAY ); if ( !$this->lock( $key, $timeout, $expiry, $rclass ) ) { return null; @@ -582,7 +582,7 @@ abstract class BagOStuff implements LoggerAwareInterface { * @return int */ protected function convertExpiry( $exptime ) { - if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) { + if ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) ) { return time() + $exptime; } else { return $exptime; @@ -597,7 +597,7 @@ abstract class BagOStuff implements LoggerAwareInterface { * @return int */ protected function convertToRelative( $exptime ) { - if ( $exptime >= 86400 * 3650 /* 10 years */ ) { + if ( $exptime >= ( 10 * self::TTL_YEAR ) ) { $exptime -= time(); if ( $exptime <= 0 ) { $exptime = 1; diff --git a/includes/libs/objectcache/IExpiringStore.php b/includes/libs/objectcache/IExpiringStore.php new file mode 100644 index 0000000000..b5ad702a53 --- /dev/null +++ b/includes/libs/objectcache/IExpiringStore.php @@ -0,0 +1,40 @@ +selectRow( ... ); * $key = $cache->makeKey( 'building', $buildingId ); - * $cache->set( $key, $row, 86400, $setOpts ); + * $cache->set( $key, $row, $cache::TTL_DAY, $setOpts ); * @endcode * * @param string $key Cache key @@ -562,8 +562,8 @@ class WANObjectCache implements LoggerAwareInterface { * $catInfo = $cache->getWithSetCallback( * // Key to store the cached value under * $cache->makeKey( 'cat-attributes', $catId ), - * // Time-to-live (seconds) - * 60, + * // Time-to-live (in seconds) + * $cache::TTL_MINUTE, * // Function that derives the new key value * function ( $oldValue, &$ttl, array &$setOpts ) { * $dbr = wfGetDB( DB_SLAVE ); @@ -580,8 +580,8 @@ class WANObjectCache implements LoggerAwareInterface { * $catConfig = $cache->getWithSetCallback( * // Key to store the cached value under * $cache->makeKey( 'site-cat-config' ), - * // Time-to-live (seconds) - * 86400, + * // Time-to-live (in seconds) + * $cache::TTL_DAY, * // Function that derives the new key value * function ( $oldValue, &$ttl, array &$setOpts ) { * $dbr = wfGetDB( DB_SLAVE ); @@ -632,7 +632,7 @@ class WANObjectCache implements LoggerAwareInterface { * $lastCatActions = $cache->getWithSetCallback( * // Key to store the cached value under * $cache->makeKey( 'cat-last-actions', 100 ), - * // Time-to-live (seconds) + * // Time-to-live (in seconds) * 10, * // Function that derives the new key value * function ( $oldValue, &$ttl, array &$setOpts ) { diff --git a/includes/media/TransformationalImageHandler.php b/includes/media/TransformationalImageHandler.php index 35581493ee..30f9e2e971 100644 --- a/includes/media/TransformationalImageHandler.php +++ b/includes/media/TransformationalImageHandler.php @@ -508,9 +508,10 @@ abstract class TransformationalImageHandler extends ImageHandler { * @return string|bool Representing the IM version; false on error */ protected function getMagickVersion() { - return ObjectCache::newAccelerator( CACHE_NONE )->getWithSetCallback( - "imagemagick-version", - 3600, + $cache = ObjectCache::newAccelerator( CACHE_NONE ); + return $cache->getWithSetCallback( + 'imagemagick-version', + $cache::TTL_HOUR, function () { global $wgImageMagickConvertCommand; @@ -523,7 +524,6 @@ abstract class TransformationalImageHandler extends ImageHandler { ); if ( $x != 1 ) { wfDebug( __METHOD__ . ": ImageMagick version check failed\n" ); - return false; } diff --git a/includes/page/WikiPage.php b/includes/page/WikiPage.php index 8b4980aa28..3c6109bf49 100644 --- a/includes/page/WikiPage.php +++ b/includes/page/WikiPage.php @@ -2914,8 +2914,9 @@ class WikiPage implements Page, IDBAccessObject { $status->value = $logid; // Show log excerpt on 404 pages rather than just a link + $cache = ObjectCache::getMainStashInstance(); $key = wfMemcKey( 'page-recent-delete', md5( $logTitle->getPrefixedText() ) ); - ObjectCache::getMainStashInstance()->set( $key, 1, 86400 ); + $cache->set( $key, 1, $cache::TTL_DAY ); return $status; } diff --git a/includes/parser/DateFormatter.php b/includes/parser/DateFormatter.php index 78f7775f76..5ffca2354f 100644 --- a/includes/parser/DateFormatter.php +++ b/includes/parser/DateFormatter.php @@ -134,7 +134,7 @@ class DateFormatter { if ( !$dateFormatter ) { $dateFormatter = $cache->getWithSetCallback( $cache->makeKey( 'dateformatter', $lang->getCode() ), - 3600, + $cache::TTL_HOUR, function () use ( $lang ) { return new DateFormatter( $lang ); } diff --git a/includes/specials/SpecialUserlogin.php b/includes/specials/SpecialUserlogin.php index 8864b981a5..6c6ba3b39a 100644 --- a/includes/specials/SpecialUserlogin.php +++ b/includes/specials/SpecialUserlogin.php @@ -636,7 +636,7 @@ class LoginForm extends SpecialPage { $key = wfMemcKey( 'acctcreate', 'ip', $ip ); $value = $cache->get( $key ); if ( !$value ) { - $cache->set( $key, 0, 86400 ); + $cache->set( $key, 0, $cache::TTL_DAY ); } if ( $value >= $wgAccountCreationThrottle ) { return Status::newFatal( 'acct_creation_throttle_hit', $wgAccountCreationThrottle ); diff --git a/includes/upload/UploadBase.php b/includes/upload/UploadBase.php index f600e32135..17fcab8b09 100644 --- a/includes/upload/UploadBase.php +++ b/includes/upload/UploadBase.php @@ -1968,7 +1968,7 @@ abstract class UploadBase { if ( $value === false ) { $cache->delete( $key ); } else { - $cache->set( $key, $value, 86400 ); + $cache->set( $key, $value, $cache::TTL_DAY ); } } } diff --git a/thumb.php b/thumb.php index bd14e417a6..8daf301eed 100644 --- a/thumb.php +++ b/thumb.php @@ -392,7 +392,7 @@ function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath if ( !$done ) { // transform() gave a fatal global $wgMemc; // Randomize TTL to reduce stampedes - $wgMemc->incrWithInit( $key, 3600 + mt_rand( 0, 300 ) ); + $wgMemc->incrWithInit( $key, $wgMemc::TTL_HOUR + mt_rand( 0, 300 ) ); } } ); @@ -445,7 +445,7 @@ function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath if ( !$thumb || $thumb->isError() ) { // Randomize TTL to reduce stampedes - $wgMemc->incrWithInit( $key, 3600 + mt_rand( 0, 300 ) ); + $wgMemc->incrWithInit( $key, $wgMemc::TTL_HOUR + mt_rand( 0, 300 ) ); } return array( $thumb, $errorHtml );