Copied to clipboard

Flag this post as spam?

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


  • Rick 92 posts 278 karma points
    Sep 08, 2015 @ 15:38
    Rick
    0

    Trying to clear macro cache

    Hi,

    I thought I'd move this over to Our as opposed to through Twitter (probably should have done this from the outset...)

    I'm trying to clear the macro cache on the save of a page (to get around the error detailed here: http://issues.umbraco.org/issue/U4-5162)

    @cultiv had the idea of writing an eventhandler for ContentService.Save that clears macro cache.

    @zpqrtbnk said to do DistributedCache.Instance.RefreshAll(macroRefresherGuid); where macroRefresherGuid is (...)

    So here is my code:

            static void Content_Saved(IContentService contentService, SaveEventArgs<IContent> saveEventArgs)
            {
    
                foreach (var item in saveEventArgs.SavedEntities)
                {
    
                    DistributedCache.Instance.RefreshAll(new Guid(DistributedCache.MacroCacheRefresherId));
    
    
                }
            }
    

    Unfortunately, this is still not working for me.

    By clicking the "Preview" button, this calls the "Saved" event; however, this call makes no difference.

    Thanks for your help so far.

    Rick

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 08, 2015 @ 19:10
    Sebastiaan Janssen
    0

    Are you sure the eventhandler get hit and are you sure the cache does not get cleared (for example: add a datetime in your cache to make sure that you're really looking at old data). It might be worth a try to clear the cache in the Saving event instead of in Saved to make sure that a save actually populates the new cached value.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 09, 2015 @ 06:08
    Dave Woestenborghs
    0

    Can you explain why you want to clear the macro cache when saving the page ? It is cleared when you publish a page.

    Clearing cache on save actions is not good for your performance.

    Dave

  • Wojciech Tengler 96 posts 199 karma points
    Jul 06, 2017 @ 08:36
    Wojciech Tengler
    0

    It is cleared when you publish a page.

    Are you sure?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 09, 2015 @ 06:43
    Sebastiaan Janssen
    0

    This is in order to be able to preview changes, otherwise you will always see stale content if you don't publish.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 09, 2015 @ 07:02
    Dave Woestenborghs
    0

    I think this should be a core change then. When in preview mode just render macro's instead of showing cached version.

    Dave

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 09, 2015 @ 07:04
    Sebastiaan Janssen
    0

    Definitely, and we fixed that but Rick indicated he can't upgrade from 7.2.1 so I was trying to offer alternatives.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 09, 2015 @ 07:06
    Dave Woestenborghs
    0

    Hah...didn't get that from the post. Nice to see it has already been addressed in the core.

    Dave

  • Rick 92 posts 278 karma points
    Sep 09, 2015 @ 08:45
    Rick
    0

    Yeah apologies Dave - it started out as a question over Twitter and I decided that it should have originally been a post on Our.

    So, I moved the above call to the "Saving" method but still no joy - I'm 100% sure that my event is being hit as I've stepped through the code - when I click the "Preview" button - the event is being hit.

    Appreciate the help/support so far.

    Thanks guys, Rick

  • Stephen 767 posts 2273 karma points c-trib
    Sep 09, 2015 @ 14:31
    Stephen
    0

    OK, I'll join. So - just to clarify - the issue is that when you hit Preview to preview the content of your page, some macros in the page still render some obsolete content. Correct?

    We should not use the cache when rendering a macro in preview mode. Sebastiaan seems to hint that you're running a version (7.2.1) that has a bug that was fixed later on.

    Just tested on 7.3 with a simple macro returning DateTime.Now: when previewing, the cache is not used, and when exiting preview, the previously cached value is returned (ie the cache is not updated during preview) -- so it works as expected.

    So I looked at the macro rendering code. The proper test for preview mode is in place since at least may, 2013 in method macro.GetMacroFromCache - and 7.2.1 was released more than a year after that.

    Must say that I am confused. Can you try with a very simple macro that just returns DateTime.Now and tell us if you see the time changing when previewing? So we can verify whether the macro actually renders?

    (note about the DistributedCache code I suggested to you: you only need to run it once, no need for the foreach loop on saved entities.)

    But again, unless I'm misunderstanding and you're running a much older version, you should not need such code. Can you also confirm the version you're running?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 09, 2015 @ 14:45
    Sebastiaan Janssen
    0

    FYI Stephen, you recently had to fix it some more, which was only included in 7.3.0 RC:

    https://github.com/Umbraco/Umbraco-CMS/commit/bef6b8cd4c55ffa90f88ae2bffd239a6f914cc60

  • Stephen 767 posts 2273 karma points c-trib
    Sep 09, 2015 @ 14:51
    Stephen
    0

    Sebastiaan: that fix was to make sure we do not update the cache during preview (with preview content) but we were already not reading the cache.

Please Sign in or register to post replies

Write your reply to:

Draft