Skin: Use WANObjectCache for sitenotice caching
authorTimo Tijhof <krinklemail@gmail.com>
Sat, 1 Jul 2017 04:19:31 +0000 (21:19 -0700)
committerKrinkle <krinklemail@gmail.com>
Mon, 3 Jul 2017 22:04:15 +0000 (22:04 +0000)
* 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

index e9d2f07..3e8b3f2 100644 (file)
@@ -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
+               );
        }
 
        /**