Convert buildSidebar() to using getWithSetCallback()
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 28 Oct 2015 05:25:36 +0000 (22:25 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 29 Oct 2015 01:24:31 +0000 (18:24 -0700)
Change-Id: Id9a27ba2bbd3aceee26bf35844d1c970dbb32d47

includes/cache/MessageCache.php
includes/skins/Skin.php

index 09e9077..bcfa792 100644 (file)
@@ -574,7 +574,7 @@ class MessageCache {
 
                foreach ( $codes as $code ) {
                        $sidebarKey = wfMemcKey( 'sidebar', $code );
-                       $this->wanCache->delete( $sidebarKey, 5 );
+                       $this->wanCache->delete( $sidebarKey );
                }
 
                // Update the message in the message blob store
index 12ebb54..08e4885 100644 (file)
@@ -1225,29 +1225,31 @@ abstract class Skin extends ContextSource {
        function buildSidebar() {
                global $wgEnableSidebarCache, $wgSidebarCacheExpiry;
 
-               $cache = ObjectCache::getMainWANInstance();
-               $key = wfMemcKey( 'sidebar', $this->getLanguage()->getCode() );
+               $that = $this;
+               $callback = function () use ( $that ) {
+                       $bar = array();
+                       $that->addToSidebar( $bar, 'sidebar' );
+                       Hooks::run( 'SkinBuildSidebar', array( $that, &$bar ) );
 
-               if ( $wgEnableSidebarCache ) {
-                       $cachedsidebar = $cache->get( $key );
-                       if ( $cachedsidebar ) {
-                               Hooks::run( 'SidebarBeforeOutput', array( $this, &$cachedsidebar ) );
-
-                               return $cachedsidebar;
-                       }
-               }
+                       return $bar;
+               };
 
-               $bar = array();
-               $this->addToSidebar( $bar, 'sidebar' );
-
-               Hooks::run( 'SkinBuildSidebar', array( $this, &$bar ) );
                if ( $wgEnableSidebarCache ) {
-                       $cache->set( $key, $bar, $wgSidebarCacheExpiry );
+                       $cache = ObjectCache::getMainWANInstance();
+                       $sidebar = $cache->getWithSetCallback(
+                               $cache->makeKey( 'sidebar', $this->getLanguage()->getCode() ),
+                               $wgSidebarCacheExpiry,
+                               $callback,
+                               array( 'lockTSE' => 30 )
+                       );
+               } else {
+                       $sidebar = $callback();
                }
 
-               Hooks::run( 'SidebarBeforeOutput', array( $this, &$bar ) );
+               // Apply post-processing to the cached value
+               Hooks::run( 'SidebarBeforeOutput', array( $this, &$sidebar ) );
 
-               return $bar;
+               return $sidebar;
        }
 
        /**
@@ -1259,7 +1261,7 @@ abstract class Skin extends ContextSource {
         * @param array $bar
         * @param string $message
         */
-       function addToSidebar( &$bar, $message ) {
+       public function addToSidebar( &$bar, $message ) {
                $this->addToSidebarPlain( $bar, wfMessage( $message )->inContentLanguage()->plain() );
        }