Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Reing 2 posts 72 karma points
    Jul 22, 2016 @ 21:39
    David Reing
    0

    Back Office Not Clearing Webserver Cache

    We have added MVC output caching to our Umbraco site. Each page is cache for some period of time. To prevent stale pages we have tied into the Umbraco ContentService and FileService events to clear the cache.

    We are in a load balanced environment using Webserver A and Webserver B to serve content and Admin Server A as the back-end office.

    The problem we are having is that when a publish occurs from Admin Server A it is not firing the events to clear the cache on either webserver. The cache is cleared on the Admin server.

    Our current work around is to then go to the webserver and publish the content again there. When doing that, both webserver caches clear.

    I'm wondering why the Admin Server publish is not triggering the events for the webservers when doing the same thing directly on one of the webservers triggers the event for both web servers.

    Here is the code we are using to clear the memory cache:

    protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            HttpContext.Current.Cache.Insert("CacheControl", DateTime.Now, null,
                  DateTime.MaxValue, TimeSpan.Zero,
                  System.Web.Caching.CacheItemPriority.NotRemovable,
                  null);
            ContentService.Published += HandlePublish;
            ContentService.Deleted += HandleFileChange;
            ContentService.Moved += HandleFileChange;
            FileService.SavedTemplate += HandleFileChange;
            FileService.DeletedTemplate += HandleFileChange;
            FileService.SavedPartialView += HandleFileChange;
            FileService.DeletedPartialView += HandleFileChange;
            base.ApplicationStarting(umbracoApplication, applicationContext);
        }
        private void HandleFileChange(IService sender, EventArgs e)
        {
            ClearCache();
        }
        private void HandlePublish(IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
            ClearCache();
        }
    
        private static void ClearCache()
        {
            try
            {
                HttpContext.Current.Cache.Insert("CacheControl", DateTime.Now, null,
                      DateTime.MaxValue, TimeSpan.Zero,
                      System.Web.Caching.CacheItemPriority.NotRemovable,
                      null);
                var cacheManager = new OutputCacheManager();
                cacheManager.RemoveItems();
            }
            catch (Exception e)
            {
                ErrorSignal.FromCurrentContext().Raise(new Exception("Cache failed to clear", e));
            }
        }
    

    Thanks for any assistance.

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Jul 26, 2016 @ 22:28
    Marc Goodson
    0

    Hi David

    Which strategy are you using for load balancing with Umbraco ?

    Flexible or Traditional ?

    https://our.umbraco.org/documentation/getting-started/setup/server-setup/load-balancing/

    Essentially your events aren't happening on the other servers, only on the admin server, and then Umbraco (if configured correctly) should be telling the other servers to 'refresh their cache' (and this won't trigger the actual events to occur again)

    Umbraco uses 'ICacheRefreshers': and classes that implement this interface take responsibility for updating the Umbraco Cache for the type of item across other configured load balanced servers.

    https://github.com/umbraco/Umbraco-CMS/blob/75c2b07ad3a093b5b65b6ebd45697687c062f62a/src/umbraco.interfaces/ICacheRefresher.cs

    For example here is an implementation responsible for clearing the cache of dictionary items when they are updated:

    https://github.com/umbraco/Umbraco-CMS/blob/75c2b07ad3a093b5b65b6ebd45697687c062f62a/src/Umbraco.Web/Cache/DictionaryCacheRefresher.cs

    You can implement your own custom ICacheRefresher but you may not need to...

    ... this is because the existing Cacherefreshers should fire an event 'CacheUpdated' on each of the load balanced servers.

    So you could hook into the PageCacheRefresher.CacheUpdated and MediaCacheRefresher.CacheUpdated

    events to clear your output caching.

    But another reason why you may not need to do this is Umbraco kind of has a basic output caching mechanism already built in that gets cleared across load balanced servers whenever you publish.

    If the items you want to cache are in a partial view, then just write them out in your template as:

    Html.CachedPartial("partialName", model, 3600)
    

    where 3600 is the length of time to cache, there are further overloads to indicate whether to cache by page or cache by member.

    https://our.umbraco.org/documentation/reference/templating/mvc/partial-views#caching

    if that helps

    regards

    Marc

  • David Reing 2 posts 72 karma points
    Jul 27, 2016 @ 13:19
    David Reing
    0

    Thanks for the response Marc. We are modifying our staging environment now so that it better reflects production (currently staging does not use the proper dedicated admin server for load balancing).

    Once we have made those changes I'll try your recommendations there and get back to you on the results in a few days.

  • Ayo Adesina 445 posts 1059 karma points
    Nov 21, 2018 @ 13:38
    Ayo Adesina
    0

    What happened? Did you solve this?

Please Sign in or register to post replies

Write your reply to:

Draft