summaryrefslogtreecommitdiff
path: root/includes/cache/MessageCache.php
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-05-01 15:32:59 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-05-01 15:32:59 -0400
commit6dc1997577fab2c366781fd7048144935afa0012 (patch)
tree8918d28c7ab4342f0738985e37af1dfc42d0e93a /includes/cache/MessageCache.php
parent150f94f051128f367bc89f6b7e5f57eb2a69fc62 (diff)
parentfa89acd685cb09cdbe1c64cbb721ec64975bbbc1 (diff)
Merge commit 'fa89acd'
# Conflicts: # .gitignore # extensions/ArchInterWiki.sql
Diffstat (limited to 'includes/cache/MessageCache.php')
-rw-r--r--includes/cache/MessageCache.php557
1 files changed, 292 insertions, 265 deletions
diff --git a/includes/cache/MessageCache.php b/includes/cache/MessageCache.php
index a55e25a3..f22c860a 100644
--- a/includes/cache/MessageCache.php
+++ b/includes/cache/MessageCache.php
@@ -25,24 +25,7 @@
* MediaWiki message cache structure version.
* Bump this whenever the message cache format has changed.
*/
-define( 'MSG_CACHE_VERSION', 1 );
-
-/**
- * Memcached timeout when loading a key.
- * See MessageCache::load()
- */
-define( 'MSG_LOAD_TIMEOUT', 60 );
-
-/**
- * Memcached timeout when locking a key for a writing operation.
- * See MessageCache::lock()
- */
-define( 'MSG_LOCK_TIMEOUT', 30 );
-/**
- * Number of times we will try to acquire a lock from Memcached.
- * This comes in addition to MSG_LOCK_TIMEOUT.
- */
-define( 'MSG_WAIT_TIMEOUT', 30 );
+define( 'MSG_CACHE_VERSION', 2 );
/**
* Message cache
@@ -50,12 +33,20 @@ define( 'MSG_WAIT_TIMEOUT', 30 );
* @ingroup Cache
*/
class MessageCache {
+ const FOR_UPDATE = 1; // force message reload
+
+ /** How long to wait for memcached locks */
+ const WAIT_SEC = 15;
+ /** How long memcached locks last */
+ const LOCK_TTL = 30;
+
/**
* Process local cache of loaded messages that are defined in
* MediaWiki namespace. First array level is a language code,
* second level is message key and the values are either message
* content prefixed with space, or !NONEXISTENT for negative
* caching.
+ * @var array $mCache
*/
protected $mCache;
@@ -84,6 +75,16 @@ class MessageCache {
protected $mLoadedLanguages = array();
/**
+ * @var bool $mInParser
+ */
+ protected $mInParser = false;
+
+ /** @var BagOStuff */
+ protected $mMemc;
+ /** @var WANObjectCache */
+ protected $wanCache;
+
+ /**
* Singleton instance
*
* @var MessageCache $instance
@@ -91,11 +92,6 @@ class MessageCache {
private static $instance;
/**
- * @var bool $mInParser
- */
- protected $mInParser = false;
-
- /**
* Get the signleton instance of this class
*
* @since 1.18
@@ -124,11 +120,31 @@ class MessageCache {
}
/**
+ * Normalize message key input
+ *
+ * @param string $key Input message key to be normalized
+ * @return string Normalized message key
+ */
+ public static function normalizeKey( $key ) {
+ global $wgContLang;
+ $lckey = strtr( $key, ' ', '_' );
+ if ( ord( $lckey ) < 128 ) {
+ $lckey[0] = strtolower( $lckey[0] );
+ } else {
+ $lckey = $wgContLang->lcfirst( $lckey );
+ }
+
+ return $lckey;
+ }
+
+ /**
* @param BagOStuff $memCached A cache instance. If none, fall back to CACHE_NONE.
* @param bool $useDB
* @param int $expiry Lifetime for cache. @see $mExpiry.
*/
function __construct( $memCached, $useDB, $expiry ) {
+ global $wgUseLocalMessageCache;
+
if ( !$memCached ) {
$memCached = wfGetCache( CACHE_NONE );
}
@@ -136,6 +152,14 @@ class MessageCache {
$this->mMemc = $memCached;
$this->mDisable = !$useDB;
$this->mExpiry = $expiry;
+
+ if ( $wgUseLocalMessageCache ) {
+ $this->localCache = ObjectCache::newAccelerator( CACHE_NONE );
+ } else {
+ $this->localCache = wfGetCache( CACHE_NONE );
+ }
+
+ $this->wanCache = ObjectCache::getMainWANInstance();
}
/**
@@ -153,70 +177,26 @@ class MessageCache {
}
/**
- * Try to load the cache from a local file.
+ * Try to load the cache from APC.
*
- * @param string $hash The hash of contents, to check validity.
* @param string $code Optional language code, see documenation of load().
- * @return array The cache array
+ * @return array|bool The cache array, or false if not in cache.
*/
- function getLocalCache( $hash, $code ) {
- global $wgCacheDirectory;
-
- $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
-
- # Check file existence
- wfSuppressWarnings();
- $file = fopen( $filename, 'r' );
- wfRestoreWarnings();
- if ( !$file ) {
- return false; // No cache file
- }
-
- // Check to see if the file has the hash specified
- $localHash = fread( $file, 32 );
- if ( $hash === $localHash ) {
- // All good, get the rest of it
- $serialized = '';
- while ( !feof( $file ) ) {
- $serialized .= fread( $file, 100000 );
- }
- fclose( $file );
-
- return unserialize( $serialized );
- } else {
- fclose( $file );
+ protected function getLocalCache( $code ) {
+ $cacheKey = wfMemcKey( __CLASS__, $code );
- return false; // Wrong hash
- }
+ return $this->localCache->get( $cacheKey );
}
/**
- * Save the cache to a local file.
- * @param string $serialized
- * @param string $hash
+ * Save the cache to APC.
+ *
* @param string $code
+ * @param array $cache The cache array
*/
- function saveToLocal( $serialized, $hash, $code ) {
- global $wgCacheDirectory;
-
- $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
- wfMkdirParents( $wgCacheDirectory, null, __METHOD__ ); // might fail
-
- wfSuppressWarnings();
- $file = fopen( $filename, 'w' );
- wfRestoreWarnings();
-
- if ( !$file ) {
- wfDebug( "Unable to open local cache file for writing\n" );
-
- return;
- }
-
- fwrite( $file, $hash . $serialized );
- fclose( $file );
- wfSuppressWarnings();
- chmod( $filename, 0666 );
- wfRestoreWarnings();
+ protected function saveToLocalCache( $code, $cache ) {
+ $cacheKey = wfMemcKey( __CLASS__, $code );
+ $this->localCache->set( $cacheKey, $cache );
}
/**
@@ -236,12 +216,11 @@ class MessageCache {
* is disabled.
*
* @param bool|string $code Language to which load messages
+ * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
* @throws MWException
* @return bool
*/
- function load( $code = false ) {
- global $wgUseLocalMessageCache;
-
+ function load( $code = false, $mode = null ) {
if ( !is_string( $code ) ) {
# This isn't really nice, so at least make a note about it and try to
# fall back
@@ -250,7 +229,7 @@ class MessageCache {
}
# Don't do double loading...
- if ( isset( $this->mLoadedLanguages[$code] ) ) {
+ if ( isset( $this->mLoadedLanguages[$code] ) && $mode != self::FOR_UPDATE ) {
return true;
}
@@ -269,45 +248,60 @@ class MessageCache {
$success = false; # Keep track of success
$staleCache = false; # a cache array with expired data, or false if none has been loaded
$where = array(); # Debug info, delayed to avoid spamming debug log too much
- $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
- # Local cache
- # Hash of the contents is stored in memcache, to detect if local cache goes
- # out of date (e.g. due to replace() on some other server)
- if ( $wgUseLocalMessageCache ) {
-
- $hash = $this->mMemc->get( wfMemcKey( 'messages', $code, 'hash' ) );
- if ( $hash ) {
- $cache = $this->getLocalCache( $hash, $code );
- if ( !$cache ) {
- $where[] = 'local cache is empty or has the wrong hash';
- } elseif ( $this->isCacheExpired( $cache ) ) {
- $where[] = 'local cache is expired';
- $staleCache = $cache;
- } else {
- $where[] = 'got from local cache';
- $success = true;
- $this->mCache[$code] = $cache;
- }
- }
+ # Hash of the contents is stored in memcache, to detect if data-center cache
+ # or local cache goes out of date (e.g. due to replace() on some other server)
+ list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
+
+ # Try the local cache and check against the cluster hash key...
+ $cache = $this->getLocalCache( $code );
+ if ( !$cache ) {
+ $where[] = 'local cache is empty';
+ } elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) {
+ $where[] = 'local cache has the wrong hash';
+ $staleCache = $cache;
+ } elseif ( $this->isCacheExpired( $cache ) ) {
+ $where[] = 'local cache is expired';
+ $staleCache = $cache;
+ } elseif ( $hashVolatile ) {
+ $where[] = 'local cache validation key is expired/volatile';
+ $staleCache = $cache;
+ } else {
+ $where[] = 'got from local cache';
+ $success = true;
+ $this->mCache[$code] = $cache;
}
if ( !$success ) {
+ $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
# Try the global cache. If it is empty, try to acquire a lock. If
# the lock can't be acquired, wait for the other thread to finish
# and then try the global cache a second time.
- for ( $failedAttempts = 0; $failedAttempts < 2; $failedAttempts++ ) {
- $cache = $this->mMemc->get( $cacheKey );
- if ( !$cache ) {
- $where[] = 'global cache is empty';
- } elseif ( $this->isCacheExpired( $cache ) ) {
- $where[] = 'global cache is expired';
- $staleCache = $cache;
+ for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
+ if ( $hashVolatile && $staleCache ) {
+ # Do not bother fetching the whole cache blob to avoid I/O.
+ # Instead, just try to get the non-blocking $statusKey lock
+ # below, and use the local stale value if it was not acquired.
+ $where[] = 'global cache is presumed expired';
} else {
- $where[] = 'got from global cache';
- $this->mCache[$code] = $cache;
- $this->saveToCaches( $cache, 'local-only', $code );
- $success = true;
+ $cache = $this->mMemc->get( $cacheKey );
+ if ( !$cache ) {
+ $where[] = 'global cache is empty';
+ } elseif ( $this->isCacheExpired( $cache ) ) {
+ $where[] = 'global cache is expired';
+ $staleCache = $cache;
+ } elseif ( $hashVolatile ) {
+ # DB results are slave lag prone until the holdoff TTL passes.
+ # By then, updates should be reflected in loadFromDBWithLock().
+ # One thread renerates the cache while others use old values.
+ $where[] = 'global cache is expired/volatile';
+ $staleCache = $cache;
+ } else {
+ $where[] = 'got from global cache';
+ $this->mCache[$code] = $cache;
+ $this->saveToCaches( $cache, 'local-only', $code );
+ $success = true;
+ }
}
if ( $success ) {
@@ -315,68 +309,12 @@ class MessageCache {
break;
}
- # We need to call loadFromDB. Limit the concurrency to a single
- # process. This prevents the site from going down when the cache
- # expires.
- $statusKey = wfMemcKey( 'messages', $code, 'status' );
- $acquired = $this->mMemc->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT );
- if ( $acquired ) {
- # Unlock the status key if there is an exception
- $that = $this;
- $statusUnlocker = new ScopedCallback( function () use ( $that, $statusKey ) {
- $that->mMemc->delete( $statusKey );
- } );
-
- # Now let's regenerate
- $where[] = 'loading from database';
-
- # Lock the cache to prevent conflicting writes
- # If this lock fails, it doesn't really matter, it just means the
- # write is potentially non-atomic, e.g. the results of a replace()
- # may be discarded.
- if ( $this->lock( $cacheKey ) ) {
- $mainUnlocker = new ScopedCallback( function () use ( $that, $cacheKey ) {
- $that->unlock( $cacheKey );
- } );
- } else {
- $mainUnlocker = null;
- $where[] = 'could not acquire main lock';
- }
-
- $cache = $this->loadFromDB( $code );
- $this->mCache[$code] = $cache;
+ # We need to call loadFromDB. Limit the concurrency to one process.
+ # This prevents the site from going down when the cache expires.
+ # Note that the DB slam protection lock here is non-blocking.
+ $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
+ if ( $loadStatus === true ) {
$success = true;
- $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
-
- # Unlock
- ScopedCallback::consume( $mainUnlocker );
- ScopedCallback::consume( $statusUnlocker );
-
- if ( !$saveSuccess ) {
- # Cache save has failed.
- # There are two main scenarios where this could be a problem:
- #
- # - The cache is more than the maximum size (typically
- # 1MB compressed).
- #
- # - Memcached has no space remaining in the relevant slab
- # class. This is unlikely with recent versions of
- # memcached.
- #
- # Either way, if there is a local cache, nothing bad will
- # happen. If there is no local cache, disabling the message
- # cache for all requests avoids incurring a loadFromDB()
- # overhead on every request, and thus saves the wiki from
- # complete downtime under moderate traffic conditions.
- if ( !$wgUseLocalMessageCache ) {
- $this->mMemc->set( $statusKey, 'error', 60 * 5 );
- $where[] = 'could not save cache, disabled globally for 5 minutes';
- } else {
- $where[] = "could not save global cache";
- }
- }
-
- # Load from DB complete, no need to retry
break;
} elseif ( $staleCache ) {
# Use the stale cache while some other thread constructs the new one
@@ -385,22 +323,19 @@ class MessageCache {
$success = true;
break;
} elseif ( $failedAttempts > 0 ) {
- # Already retried once, still failed, so don't do another lock/unlock cycle
+ # Already blocked once, so avoid another lock/unlock cycle.
# This case will typically be hit if memcached is down, or if
- # loadFromDB() takes longer than MSG_WAIT_TIMEOUT
+ # loadFromDB() takes longer than LOCK_WAIT.
$where[] = "could not acquire status key.";
break;
+ } elseif ( $loadStatus === 'cantacquire' ) {
+ # Wait for the other thread to finish, then retry. Normally,
+ # the memcached get() will then yeild the other thread's result.
+ $where[] = 'waited for other thread to complete';
+ $this->getReentrantScopedLock( $cacheKey );
} else {
- $status = $this->mMemc->get( $statusKey );
- if ( $status === 'error' ) {
- # Disable cache
- break;
- } else {
- # Wait for the other thread to finish, then retry
- $where[] = 'waited for other thread to complete';
- $this->lock( $cacheKey );
- $this->unlock( $cacheKey );
- }
+ # Disable cache; $loadStatus is 'disabled'
+ break;
}
}
}
@@ -415,6 +350,7 @@ class MessageCache {
# All good, just record the success
$this->mLoadedLanguages[$code] = true;
}
+
$info = implode( ', ', $where );
wfDebugLog( 'MessageCache', __METHOD__ . ": Loading $code... $info\n" );
@@ -422,16 +358,82 @@ class MessageCache {
}
/**
+ * @param string $code
+ * @param array $where List of wfDebug() comments
+ * @param integer $mode Use MessageCache::FOR_UPDATE to use DB_MASTER
+ * @return bool|string True on success or one of ("cantacquire", "disabled")
+ */
+ protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
+ global $wgUseLocalMessageCache;
+
+ # If cache updates on all levels fail, give up on message overrides.
+ # This is to avoid easy site outages; see $saveSuccess comments below.
+ $statusKey = wfMemcKey( 'messages', $code, 'status' );
+ $status = $this->mMemc->get( $statusKey );
+ if ( $status === 'error' ) {
+ $where[] = "could not load; method is still globally disabled";
+ return 'disabled';
+ }
+
+ # Now let's regenerate
+ $where[] = 'loading from database';
+
+ # Lock the cache to prevent conflicting writes.
+ # This lock is non-blocking so stale cache can quickly be used.
+ # Note that load() will call a blocking getReentrantScopedLock()
+ # after this if it really need to wait for any current thread.
+ $cacheKey = wfMemcKey( 'messages', $code );
+ $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
+ if ( !$scopedLock ) {
+ $where[] = 'could not acquire main lock';
+ return 'cantacquire';
+ }
+
+ $cache = $this->loadFromDB( $code, $mode );
+ $this->mCache[$code] = $cache;
+ $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
+
+ if ( !$saveSuccess ) {
+ # Cache save has failed.
+ # There are two main scenarios where this could be a problem:
+ #
+ # - The cache is more than the maximum size (typically
+ # 1MB compressed).
+ #
+ # - Memcached has no space remaining in the relevant slab
+ # class. This is unlikely with recent versions of
+ # memcached.
+ #
+ # Either way, if there is a local cache, nothing bad will
+ # happen. If there is no local cache, disabling the message
+ # cache for all requests avoids incurring a loadFromDB()
+ # overhead on every request, and thus saves the wiki from
+ # complete downtime under moderate traffic conditions.
+ if ( !$wgUseLocalMessageCache ) {
+ $this->mMemc->set( $statusKey, 'error', 60 * 5 );
+ $where[] = 'could not save cache, disabled globally for 5 minutes';
+ } else {
+ $where[] = "could not save global cache";
+ }
+ }
+
+ return true;
+ }
+
+ /**
* Loads cacheable messages from the database. Messages bigger than
* $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
* on-demand from the database later.
*
- * @param string $code Language code.
- * @return array Loaded messages for storing in caches.
+ * @param string $code Language code
+ * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
+ * @return array Loaded messages for storing in caches
*/
- function loadFromDB( $code ) {
+ function loadFromDB( $code, $mode = null ) {
global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
- $dbr = wfGetDB( DB_SLAVE );
+
+ $dbr = wfGetDB( ( $mode == self::FOR_UPDATE ) ? DB_MASTER : DB_SLAVE );
+
$cache = array();
# Common conditions
@@ -502,6 +504,8 @@ class MessageCache {
}
$cache['VERSION'] = MSG_CACHE_VERSION;
+ ksort( $cache );
+ $cache['HASH'] = md5( serialize( $cache ) );
$cache['EXPIRY'] = wfTimestamp( TS_MW, time() + $this->mExpiry );
return $cache;
@@ -510,41 +514,57 @@ class MessageCache {
/**
* Updates cache as necessary when message page is changed
*
- * @param string $title Name of the page changed.
+ * @param string|bool $title Name of the page changed (false if deleted)
* @param mixed $text New contents of the page.
*/
public function replace( $title, $text ) {
- global $wgMaxMsgCacheEntrySize;
+ global $wgMaxMsgCacheEntrySize, $wgContLang, $wgLanguageCode;
if ( $this->mDisable ) {
-
return;
}
list( $msg, $code ) = $this->figureMessage( $title );
+ if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
+ // Content language overrides do not use the /<code> suffix
+ return;
+ }
+ // Note that if the cache is volatile, load() may trigger a DB fetch.
+ // In that case we reenter/reuse the existing cache key lock to avoid
+ // a self-deadlock. This is safe as no reads happen *directly* in this
+ // method between getReentrantScopedLock() and load() below. There is
+ // no risk of data "changing under our feet" for replace().
$cacheKey = wfMemcKey( 'messages', $code );
- $this->load( $code );
- $this->lock( $cacheKey );
+ $scopedLock = $this->getReentrantScopedLock( $cacheKey );
+ $this->load( $code, self::FOR_UPDATE );
$titleKey = wfMemcKey( 'messages', 'individual', $title );
-
if ( $text === false ) {
- # Article was deleted
+ // Article was deleted
$this->mCache[$code][$title] = '!NONEXISTENT';
- $this->mMemc->delete( $titleKey );
+ $this->wanCache->delete( $titleKey );
} elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
- # Check for size
+ // Check for size
$this->mCache[$code][$title] = '!TOO BIG';
- $this->mMemc->set( $titleKey, ' ' . $text, $this->mExpiry );
+ $this->wanCache->set( $titleKey, ' ' . $text, $this->mExpiry );
} else {
$this->mCache[$code][$title] = ' ' . $text;
- $this->mMemc->delete( $titleKey );
+ $this->wanCache->delete( $titleKey );
}
- # Update caches
- $this->saveToCaches( $this->mCache[$code], 'all', $code );
- $this->unlock( $cacheKey );
+ // Mark this cache as definitely "latest" (non-volatile) so
+ // load() calls do try to refresh the cache with slave data
+ $this->mCache[$code]['LATEST'] = time();
+
+ // Update caches if the lock was acquired
+ if ( $scopedLock ) {
+ $this->saveToCaches( $this->mCache[$code], 'all', $code );
+ }
+
+ ScopedCallback::consume( $scopedLock );
+ // Relay the purge to APC and other DCs
+ $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
// Also delete cached sidebar... just in case it is affected
$codes = array( $code );
@@ -554,19 +574,16 @@ class MessageCache {
$codes = array_keys( Language::fetchLanguageNames() );
}
- global $wgMemc;
foreach ( $codes as $code ) {
$sidebarKey = wfMemcKey( 'sidebar', $code );
- $wgMemc->delete( $sidebarKey );
+ $this->wanCache->delete( $sidebarKey, 5 );
}
// Update the message in the message blob store
- global $wgContLang;
$blobStore = new MessageBlobStore();
$blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
Hooks::run( 'MessageCacheReplace', array( $title, $text ) );
-
}
/**
@@ -598,63 +615,79 @@ class MessageCache {
* @param string|bool $code Language code (default: false)
* @return bool
*/
- protected function saveToCaches( $cache, $dest, $code = false ) {
- global $wgUseLocalMessageCache;
-
- $cacheKey = wfMemcKey( 'messages', $code );
-
+ protected function saveToCaches( array $cache, $dest, $code = false ) {
if ( $dest === 'all' ) {
+ $cacheKey = wfMemcKey( 'messages', $code );
$success = $this->mMemc->set( $cacheKey, $cache );
} else {
$success = true;
}
- # Save to local cache
- if ( $wgUseLocalMessageCache ) {
- $serialized = serialize( $cache );
- $hash = md5( $serialized );
- $this->mMemc->set( wfMemcKey( 'messages', $code, 'hash' ), $hash );
- $this->saveToLocal( $serialized, $hash, $code );
- }
+ $this->setValidationHash( $code, $cache );
+ $this->saveToLocalCache( $code, $cache );
return $success;
}
/**
- * Represents a write lock on the messages key.
+ * Get the md5 used to validate the local APC cache
*
- * Will retry MessageCache::MSG_WAIT_TIMEOUT times, each operations having
- * a timeout of MessageCache::MSG_LOCK_TIMEOUT.
- *
- * @param string $key
- * @return bool Success
+ * @param string $code
+ * @return array (hash or false, bool expiry/volatility status)
*/
- function lock( $key ) {
- $lockKey = $key . ':lock';
- $acquired = false;
- $testDone = false;
- for ( $i = 0; $i < MSG_WAIT_TIMEOUT && !$acquired; $i++ ) {
- $acquired = $this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT );
- if ( $acquired ) {
- break;
- }
+ protected function getValidationHash( $code ) {
+ $curTTL = null;
+ $value = $this->wanCache->get(
+ wfMemcKey( 'messages', $code, 'hash', 'v1' ),
+ $curTTL,
+ array( wfMemcKey( 'messages', $code ) )
+ );
- # Fail fast if memcached is totally down
- if ( !$testDone ) {
- $testDone = true;
- if ( !$this->mMemc->set( wfMemcKey( 'test' ), 'test', 1 ) ) {
- break;
- }
+ if ( !$value ) {
+ // No hash found at all; cache must regenerate to be safe
+ $hash = false;
+ $expired = true;
+ } else {
+ $hash = $value['hash'];
+ if ( ( time() - $value['latest'] ) < WANObjectCache::HOLDOFF_TTL ) {
+ // Cache was recently updated via replace() and should be up-to-date
+ $expired = false;
+ } else {
+ // See if the "check" key was bumped after the hash was generated
+ $expired = ( $curTTL < 0 );
}
- sleep( 1 );
}
- return $acquired;
+ return array( $hash, $expired );
}
- function unlock( $key ) {
- $lockKey = $key . ':lock';
- $this->mMemc->delete( $lockKey );
+ /**
+ * Set the md5 used to validate the local disk cache
+ *
+ * If $cache has a 'LATEST' UNIX timestamp key, then the hash will not
+ * be treated as "volatile" by getValidationHash() for the next few seconds
+ *
+ * @param string $code
+ * @param array $cache Cached messages with a version
+ */
+ protected function setValidationHash( $code, array $cache ) {
+ $this->wanCache->set(
+ wfMemcKey( 'messages', $code, 'hash', 'v1' ),
+ array(
+ 'hash' => $cache['HASH'],
+ 'latest' => isset( $cache['LATEST'] ) ? $cache['LATEST'] : 0
+ ),
+ WANObjectCache::TTL_NONE
+ );
+ }
+
+ /**
+ * @param string $key A language message cache key that stores blobs
+ * @param integer $timeout Wait timeout in seconds
+ * @return null|ScopedCallback
+ */
+ protected function getReentrantScopedLock( $key, $timeout = self::WAIT_SEC ) {
+ return $this->mMemc->getScopedLock( $key, $timeout, self::LOCK_TTL, __METHOD__ );
}
/**
@@ -713,12 +746,7 @@ class MessageCache {
}
// Normalise title-case input (with some inlining)
- $lckey = strtr( $key, ' ', '_' );
- if ( ord( $lckey ) < 128 ) {
- $lckey[0] = strtolower( $lckey[0] );
- } else {
- $lckey = $wgContLang->lcfirst( $lckey );
- }
+ $lckey = MessageCache::normalizeKey( $key );
Hooks::run( 'MessageCache::get', array( &$lckey ) );
@@ -903,7 +931,7 @@ class MessageCache {
# Try the individual message cache
$titleKey = wfMemcKey( 'messages', 'individual', $title );
- $entry = $this->mMemc->get( $titleKey );
+ $entry = $this->wanCache->get( $titleKey );
if ( $entry ) {
if ( substr( $entry, 0, 1 ) === ' ' ) {
$this->mCache[$code][$title] = $entry;
@@ -917,14 +945,12 @@ class MessageCache {
return false;
} else {
# Corrupt/obsolete entry, delete it
- $this->mMemc->delete( $titleKey );
+ $this->wanCache->delete( $titleKey );
}
}
# Try loading it from the database
- $revision = Revision::newFromTitle(
- Title::makeTitle( NS_MEDIAWIKI, $title ), false, Revision::READ_LATEST
- );
+ $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
if ( $revision ) {
$content = $revision->getContent();
if ( !$content ) {
@@ -945,13 +971,13 @@ class MessageCache {
wfDebugLog(
'MessageCache',
__METHOD__ . ": message content doesn't provide wikitext "
- . "(content model: " . $content->getContentHandler() . ")"
+ . "(content model: " . $content->getModel() . ")"
);
$message = false; // negative caching
} else {
$this->mCache[$code][$title] = ' ' . $message;
- $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
+ $this->wanCache->set( $titleKey, ' ' . $message, $this->mExpiry );
}
}
} else {
@@ -960,7 +986,7 @@ class MessageCache {
if ( $message === false ) { // negative caching
$this->mCache[$code][$title] = '!NONEXISTENT';
- $this->mMemc->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
+ $this->wanCache->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
}
return $message;
@@ -1042,7 +1068,8 @@ class MessageCache {
if ( !$title || !$title instanceof Title ) {
global $wgTitle;
- wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' . wfGetAllCallers( 5 ) . ' with no title set.' );
+ wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' .
+ wfGetAllCallers( 5 ) . ' with no title set.' );
$title = $wgTitle;
}
// Sometimes $wgTitle isn't set either...
@@ -1073,11 +1100,10 @@ class MessageCache {
function clear() {
$langs = Language::fetchLanguageNames( null, 'mw' );
foreach ( array_keys( $langs ) as $code ) {
- # Global cache
- $this->mMemc->delete( wfMemcKey( 'messages', $code ) );
- # Invalidate all local caches
- $this->mMemc->delete( wfMemcKey( 'messages', $code, 'hash' ) );
+ # Global and local caches
+ $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
}
+
$this->mLoadedLanguages = array();
}
@@ -1087,6 +1113,7 @@ class MessageCache {
*/
public function figureMessage( $key ) {
global $wgLanguageCode;
+
$pieces = explode( '/', $key );
if ( count( $pieces ) < 2 ) {
return array( $key, $wgLanguageCode );