'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',
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() );
// Save to cache
$this->firstLetterData = $data;
- $cache->set( $cacheKey, $data, 86400 * 7 /* 1 week */ );
+ $cache->set( $cacheKey, $data, $cache::TTL_WEEK );
return $data;
}
* @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 );
$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 */
);
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();
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;
}
*
* @ingroup Cache
*/
-abstract class BagOStuff implements LoggerAwareInterface {
+abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
/** @var array[] Lock tracking */
protected $locks = array();
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)
}
}
$this->clearLastError();
- $currentValue = $this->get( $key, BagOStuff::READ_LATEST );
+ $currentValue = $this->get( $key, self::READ_LATEST );
if ( $this->getLastError() ) {
$success = false;
} else {
}
}
- $expiry = min( $expiry ?: INF, 86400 );
+ $expiry = min( $expiry ?: INF, self::TTL_DAY );
$this->clearLastError();
$timestamp = microtime( true ); // starting UNIX timestamp
* @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;
* @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;
* @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;
--- /dev/null
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Cache
+ * @author 2015 Timo Tijhof
+ */
+
+/**
+ * Generic base class for storage interfaces.
+ *
+ * Provides convenient TTL constants.
+ *
+ * @ingroup Cache
+ * @since 1.27
+ */
+interface IExpiringStore {
+
+ // Constants for TTL values, in seconds
+ const TTL_MINUTE = 60;
+ const TTL_HOUR = 3600;
+ const TTL_DAY = 86400; // 24 * 3600
+ const TTL_WEEK = 604800; // 7 * 24 * 3600
+ const TTL_MONTH = 2592000; // 30 * 24 * 3600
+ const TTL_YEAR = 31536000; // 365 * 24 * 3600
+}
* @ingroup Cache
* @since 1.26
*/
-class WANObjectCache implements LoggerAwareInterface {
+class WANObjectCache implements IExpiringStore, LoggerAwareInterface {
/** @var BagOStuff The local datacenter cache */
protected $cache;
/** @var HashBagOStuff Script instance PHP cache */
const HOLDOFF_TTL = 14; // MAX_COMMIT_DELAY + MAX_REPLICA_LAG + MAX_SNAPSHOT_LAG + 1
/** Seconds to keep dependency purge keys around */
- const CHECK_KEY_TTL = 31536000; // 1 year
+ const CHECK_KEY_TTL = self::TTL_YEAR;
/** Seconds to keep lock keys around */
const LOCK_TTL = 5;
/** Default remaining TTL at which to consider pre-emptive regeneration */
* // Fetch the row from the DB
* $row = $dbr->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
* $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 );
* $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 );
* $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 ) {
* @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;
);
if ( $x != 1 ) {
wfDebug( __METHOD__ . ": ImageMagick version check failed\n" );
-
return false;
}
$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;
}
if ( !$dateFormatter ) {
$dateFormatter = $cache->getWithSetCallback(
$cache->makeKey( 'dateformatter', $lang->getCode() ),
- 3600,
+ $cache::TTL_HOUR,
function () use ( $lang ) {
return new DateFormatter( $lang );
}
$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 );
if ( $value === false ) {
$cache->delete( $key );
} else {
- $cache->set( $key, $value, 86400 );
+ $cache->set( $key, $value, $cache::TTL_DAY );
}
}
}
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 ) );
}
} );
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 );