Sunday, November 25, 2012

Performance Increase for Multi-Lingual sites

Improve Performance of Variations

Speed up custom SharePoint sites by caching often used objects.


In a project I was involved with we developed multiple public sites that were multi-lingual. We used the SharePoint Variation system to create an identical site structure in multiple languages.
Because we were developing multiple multi-lingual sites with custom webparts in this project we would often have to check in which Variation the webpart was, so we could find a list in the top level variation web, or create the correct link to a page in a variation.

After a while we found out this code was being called around 20x per page and each call would take approx. 150ms!
The object we were using frequently was Microsoft.SharePoint.Publishing.Variations.Current
We found that by caching this object in HttpContext.Current.Cache, performance increased quite significantly across all 5 live multi-lingual sites (approx. 3 secs in every page load). We also found out (the hard way) that it matters in which context you cache the object compared to the current context you're in when you get the object from the cache (e.g. http vs https), so be wary of that when you implement caching on this object.

Great results for such an easy change!


private static Microsoft.SharePoint.Publishing.Variations GetCurrentVariation()
        {
            Microsoft.SharePoint.Publishing.Variations aCachedCurrentvariation = null;

            try
            {
                bool secureConnection = HttpContext.Current.Request.IsSecureConnection;

                string aCurrentCacheKey;

                // set the cache key to ensure we're using a different cache for HTTP and HTTPS
                if (secureConnection)
                {
                    aCurrentCacheKey = Variations._cacheKeySecure;
                }
                else
                {
                    aCurrentCacheKey = Variations._cacheKeyUnsecure;
                }

                if (HttpContext.Current.Cache[aCurrentCacheKey] != null)
                {
                    aCachedCurrentvariation = HttpContext.Current.Cache[aCurrentCacheKey] as Microsoft.SharePoint.Publishing.Variations;
                }
                else
                {
                    aCachedCurrentvariation = Microsoft.SharePoint.Publishing.Variations.Current;

                    if ((aCachedCurrentvariation == null) || (aCachedCurrentvariation.UserAccessibleLabels == null) || (aCachedCurrentvariation.UserAccessibleLabels.Count == 0))
                    {
                        // do not cache when current variation is null
                    }
                    else
                    {
                        HttpContext.Current.Cache.Add(aCurrentCacheKey, aCachedCurrentvariation, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Caching error in ToolBox.Variations: " + ex.Message);
            }

            return aCachedCurrentvariation;
        }