SpecialMovepage: Convert form to use OOUI controls
[lhc/web/wiklou.git] / includes / cache / MessageCache.php
index dafb59d..e693922 100644 (file)
@@ -160,7 +160,7 @@ class MessageCache {
                $this->mExpiry = $expiry;
 
                if ( $wgUseLocalMessageCache ) {
-                       $this->localCache = ObjectCache::newAccelerator( array(), CACHE_NONE );
+                       $this->localCache = ObjectCache::newAccelerator( CACHE_NONE );
                } else {
                        $this->localCache = wfGetCache( CACHE_NONE );
                }
@@ -190,6 +190,7 @@ class MessageCache {
         */
        protected function getLocalCache( $code ) {
                $cacheKey = wfMemcKey( __CLASS__, $code );
+
                return $this->localCache->get( $cacheKey );
        }
 
@@ -226,8 +227,6 @@ class MessageCache {
         * @return bool
         */
        function load( $code = false, $mode = null ) {
-               global $wgUseLocalMessageCache;
-
                if ( !is_string( $code ) ) {
                        # This isn't really nice, so at least make a note about it and try to
                        # fall back
@@ -260,25 +259,23 @@ class MessageCache {
                # or local cache goes out of date (e.g. due to replace() on some other server)
                list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
 
-               if ( $wgUseLocalMessageCache && $hash ) {
-                       # 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;
-                       }
+               # 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 ) {
@@ -346,7 +343,7 @@ class MessageCache {
                                                # 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->mMemc->getScopedLock( $cacheKey, self::WAIT_SEC, self::LOCK_TTL );
+                                               $this->getReentrantScopedLock( $cacheKey );
                                        }
                                }
                        }
@@ -400,7 +397,7 @@ class MessageCache {
                # 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.
-               $mainUnlocker = $memCache->getScopedLock( $cacheKey, self::WAIT_SEC, self::LOCK_TTL );
+               $mainUnlocker = $this->getReentrantScopedLock( $cacheKey );
                if ( !$mainUnlocker ) {
                        $where[] = 'could not acquire main lock';
                }
@@ -548,8 +545,13 @@ class MessageCache {
                        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 );
-               $scopedLock = $this->mMemc->getScopedLock( $cacheKey, self::WAIT_SEC, self::LOCK_TTL );
+               $scopedLock = $this->getReentrantScopedLock( $cacheKey );
                $this->load( $code, self::FOR_UPDATE );
 
                $titleKey = wfMemcKey( 'messages', 'individual', $title );
@@ -557,14 +559,14 @@ class MessageCache {
                if ( $text === false ) {
                        # Article was deleted
                        $this->mCache[$code][$title] = '!NONEXISTENT';
-                       $this->mMemc->delete( $titleKey );
+                       $this->wanCache->delete( $titleKey );
                } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
                        # 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
@@ -622,8 +624,6 @@ class MessageCache {
         * @return bool
         */
        protected function saveToCaches( $cache, $dest, $code = false ) {
-               global $wgUseLocalMessageCache;
-
                if ( $dest === 'all' ) {
                        $cacheKey = wfMemcKey( 'messages', $code );
                        $success = $this->mMemc->set( $cacheKey, $cache );
@@ -634,9 +634,7 @@ class MessageCache {
                $this->setValidationHash( $code, $cache['HASH'] );
 
                # Save to local cache
-               if ( $wgUseLocalMessageCache ) {
-                       $this->saveToLocalCache( $code, $cache );
-               }
+               $this->saveToLocalCache( $code, $cache );
 
                return $success;
        }
@@ -673,6 +671,14 @@ class MessageCache {
                );
        }
 
+       /**
+        * @param string $key A language message cache key that stores blobs
+        * @return null|ScopedCallback
+        */
+       protected function getReentrantScopedLock( $key ) {
+               return $this->mMemc->getScopedLock( $key, self::WAIT_SEC, self::LOCK_TTL, __METHOD__ );
+       }
+
        /**
         * Get a message from either the content language or the user language.
         *
@@ -914,7 +920,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;
@@ -928,14 +934,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 ) {
@@ -962,7 +966,7 @@ class MessageCache {
                                        $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 {
@@ -971,7 +975,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;