Copied to clipboard

Flag this post as spam?

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


  • anthony hall 222 posts 536 karma points
    Oct 19, 2016 @ 13:36
    anthony hall
    0

    Clearing the donut cache for a particular page on publish

    I have donutcache attributes on the controllers of my website. On the "Content_Published" event i would like to clear the cache for a particular page.

    var cacheManager = new OutputCacheManager();
    cacheManager.RemoveItems();
    

    The above would clear the cache for all pages. There are overrides that enable entering a "ControllerName", "ActionName".

    Whenever i try to get these. I get Controller "Content", Action "Post_Saved". I'm assuming these are the internal umbraco routes.

    private RouteValueDictionary GetRouteData()
    {
        var httpContext = System.Web.HttpContext.Current;
        var routeData = RouteTable.Routes.GetRouteData(new System.Web.HttpContextWrapper(httpContext));
        return routeData.Values;
    }
    

    I'm interested in recommendations for managing the output cache. Is there a way to save a key and remove the cache via the key.

  • Martijn Duizendstra 18 posts 113 karma points
    Jan 11, 2017 @ 23:23
    Martijn Duizendstra
    0

    Yeah, I was wondering about the same actually.

    I have multiple websites running and a publish on any of the sites will clear the cache for all pages... and all sites.

    It would be really nice to limit the cache refresh to a page/template/nodetype... ? But would this be possible?

  • anthony hall 222 posts 536 karma points
    Jan 12, 2017 @ 09:55
    anthony hall
    0

    Hi Martijn! I still haven't resolved this. There must be a way to configure this.

  • Martijn Duizendstra 18 posts 113 karma points
    Jan 13, 2017 @ 16:31
    Martijn Duizendstra
    0

    Thanks for your reply Anthony... that's a shame, I was hoping you got a bit more luckier in the meanwhile. However, I'm very happy that I can actually clear the cache on publish, makes life a bit easier not having to restart stuff.

    I'm going to experiment on this next week... keep you posted.

    I think I'll have to setup a second environment, because publishing on my local host (using the same DB) also triggers the cache to be cleared on all sites (~ 10) which causes a CPU spike on production if I'm not careful.

    Maybe in the meanwhile one of the Umbraco overlords on this forum reads this thread and goes "Nerds... just add this, et voila"

    ;')

    Have a nice weekend.

  • Martijn Duizendstra 18 posts 113 karma points
    Feb 16, 2017 @ 08:30
    Martijn Duizendstra
    0

    Hey Anthony,

    I'm still busy figuring this out and I think I might be on to something. What i've done so far is the following:

    In my app startup, I've added a reference to Umbraco.Core.Services

    using Umbraco.Core.Services;
    

    Then in ApplicationStarting I've hooked into the ContentService:

    ContentService.Published += ContentService_Published;
    

    This references the following code:

    void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
        {
            foreach (var entity in e.PublishedEntities)
            {
                // Clear the cache
            }
        }
    

    But now I'm stuck clearing a particular cached item.

    My idea was to fetch the document type from the published entity, this matches up with my custom controller names.

    For example, I have a doctype Tour and a view called Index and a custom controller called TourController.

    When doctype Tour is published, I want to call the outputcachemanager and clear the cache for this type. Which could be something like this:

       var docType = entity.ContentType;
       var view = entity.Template;
    
       // Clear the cache
       OutputCacheManager manager = new OutputCacheManager();
       manager.RemoveItem(docType, view);
    

    But I cannot get this to work. I also tried doing it hard coded as a test, but the same result, the cache will not clear.

    manager.RemoveItem("Tour", "Index");
    

    This does work:

    manager.RemoveItems();
    

    But will result in all the cache being cleared of course.

    Maybe the problem is with the way I'm caching my controllers? Our umbraco installation hold multiple sites, that's why I'm varying by baseurl to prevent cross-site caching.

    [DonutOutputCache(Duration = 360, VaryByParam = "url;category;", VaryByCustom = "baseurl")]
    

    Testing is a bit tricky, because of the events on the production env which will clear the cache while developing. I think I'll setup a blank solution with a fresh install and develop this further on there.

    I hope this is the right track, I'm still kind of a Umbraco/C# noob because I'm a UX Designer. But hey... I'm learning.

  • Martijn Duizendstra 18 posts 113 karma points
    May 05, 2017 @ 14:17
    Martijn Duizendstra
    1

    Anthony... my man! I've fixed it! Will post a reply with the details soon, gotta release this first!

  • mzaibi 4 posts 72 karma points
    Feb 05, 2018 @ 11:28
    mzaibi
    0

    Hi,

    I'm trying to find a solution to the same problem, please can you post the your own idea :)

  • Ali Kazai 11 posts 82 karma points c-trib
    Dec 15, 2018 @ 02:47
    Ali Kazai
    0

    Hi had a smimilar issue for my site, but came accross this article by paul seal.

    https://codeshare.co.uk/blog/how-to-use-donut-caching-in-umbraco-and-mvc/

    He does something like this:

    var navigationDocTypeAliases = new List

            //if the document type alias of the published content item is in the list above then invalidate the cache. 
            if (e.PublishedEntities.Any(x => navigationDocTypeAliases.Contains(x.ContentType.Alias)))
            {
                var cacheManager = new OutputCacheManager();
                cacheManager.RemoveItem("SiteLayout", "RenderMainNavigation");
            }
    

    hope this helps

Please Sign in or register to post replies

Write your reply to:

Draft