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.
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.
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?
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:
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
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 inSaved
to make sure that a save actually populates the new cached value.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
Are you sure?
This is in order to be able to preview changes, otherwise you will always see stale content if you don't publish.
I think this should be a core change then. When in preview mode just render macro's instead of showing cached version.
Dave
Definitely, and we fixed that but Rick indicated he can't upgrade from 7.2.1 so I was trying to offer alternatives.
Hah...didn't get that from the post. Nice to see it has already been addressed in the core.
Dave
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
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?
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
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.
is working on a reply...