From 83b50484e0023ec16f24f65c29009dcb2e7b5fd5 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Fri, 30 Jun 2017 21:19:31 -0700 Subject: [PATCH] Skin: Use WANObjectCache for sitenotice caching * Move the md5() hash to the cache key, this makes it much safer after a change happens by avoiding write competitions between different servers and db slaves. It also allows an undo to re-use the existing cache if it still exists. In addition, it enables idiomatic use of getWithSetCallback given that get and set are now logically separated. * Avoid fragile re-use of variable names. Previously it read the original $notice value at multiple points but also setting $notice to $parsed after a certain point. Consistently use $parsed only. (Ref T115890.) Change-Id: I5488cc894ff1544e6c20b7d51a7a2adfc292c4ec --- includes/skins/Skin.php | 44 ++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/includes/skins/Skin.php b/includes/skins/Skin.php index e9d2f076b1..3e8b3f23a0 100644 --- a/includes/skins/Skin.php +++ b/includes/skins/Skin.php @@ -20,6 +20,8 @@ * @file */ +use MediaWiki\MediaWikiServices; + /** * @defgroup Skins Skins */ @@ -1259,7 +1261,7 @@ abstract class Skin extends ContextSource { }; if ( $wgEnableSidebarCache ) { - $cache = ObjectCache::getMainWANInstance(); + $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); $sidebar = $cache->getWithSetCallback( $cache->makeKey( 'sidebar', $this->getLanguage()->getCode() ), MessageCache::singleton()->isDisabled() @@ -1506,29 +1508,27 @@ abstract class Skin extends ContextSource { $notice = $msg->plain(); } - $cache = wfGetCache( CACHE_ANYTHING ); - // Use the extra hash appender to let eg SSL variants separately cache. - $key = $cache->makeKey( $name . $wgRenderHashAppend ); - $cachedNotice = $cache->get( $key ); - if ( is_array( $cachedNotice ) ) { - if ( md5( $notice ) == $cachedNotice['hash'] ) { - $notice = $cachedNotice['html']; - } else { - $needParse = true; + $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); + $parsed = $cache->getWithSetCallback( + // Use the extra hash appender to let eg SSL variants separately cache + // Key is verified with md5 hash of unparsed wikitext + $cache->makeKey( $name, $wgRenderHashAppend, md5( $notice ) ), + // TTL in seconds + 600, + function () use ( $notice ) { + return $this->getOutput()->parse( $notice ); } - } else { - $needParse = true; - } - - if ( $needParse ) { - $parsed = $this->getOutput()->parse( $notice ); - $cache->set( $key, [ 'html' => $parsed, 'hash' => md5( $notice ) ], 600 ); - $notice = $parsed; - } + ); - $notice = Html::rawElement( 'div', [ 'id' => 'localNotice', - 'lang' => $wgContLang->getHtmlCode(), 'dir' => $wgContLang->getDir() ], $notice ); - return $notice; + return Html::rawElement( + 'div', + [ + 'id' => 'localNotice', + 'lang' => $wgContLang->getHtmlCode(), + 'dir' => $wgContLang->getDir() + ], + $parsed + ); } /** -- 2.20.1