Copied to clipboard

Flag this post as spam?

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


  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 22, 2015 @ 12:27
    Dave Woestenborghs
    0

    Using ICacheRefresher to clear cache

    Hi,

    In a new project I'm storing some information in the cache using the Umbaco Application cache as described in this document :

    https://our.umbraco.org/documentation/reference/cache/updating-cache

    These 2 documents however state that to refresh your cache you need to implement a class with ICacheRefresher interface.

    https://our.umbraco.org/documentation/reference/cache/
    https://our.umbraco.org/Documentation/Reference/Cache/cache-refresher

    I have a implement my class and see that it get's loaded by Umbraco (placed breakpoint in constructor).

    What is not clear to me is how to hook it up correctly. What I have done now is hooking in to various content service events and then calling this code :

    DistributedCache.Instance.RefreshAll(new Guid(Constants.RuntimeCacheRefresherGuid)); 

    Is this the correct way or am I missing something.

    Dave

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    May 26, 2015 @ 00:52
    Shannon Deminick
    102

    This all really depends on what you are doing.

    I'm assuming you've created an ICacheRefresher with the ID of Constants.RuntimeCacheRefresherGuid?

    You should also ensure to use the base classes, so at a minimum you should use CacheRefresherBase

    If you make this call:

     DistributedCache.Instance.RefreshAll(new Guid(Constants.RuntimeCacheRefresherGuid)); 
    

    In that case, it will make a distributed call to all servers including the current one to call your ICacheRefresher.RefreshAll method.

    Your question on twitter was 'but how do I hook it up to a publish event` - we need to know what you are trying to achieve before I can advise you on what to do. If you just want to clear your own cache based on publishing then you don't even need to have your own ICacheRefresher. You can use an event:

     PageCacheRefresher.CacheUpdated
    

    This event will be raised on all servers once the PageCacheRefresher (ICacheRefresher) has executed. If all of the information you require to refresh your own cache can be found in the event's CacheRefresherEventArgs then you can use that information to refresh your custom cache.

    You would need a custom ICacheRefresher, for example, if you created your own Tree/Editor and you had custom caching taking place at that level. Whenever your own custom data changes, you'd need to use the DistributedCache.Instance to send cache refresher notifications out to all of the servers to keep your own cache in sync.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 26, 2015 @ 08:15
    Dave Woestenborghs
    1

    Hi Shannon,

    Thanks for the clarification. I want clear my own custom cache when a page is published. 

    I think I need to hook into the PageCacheRefresher.CacheUpdated event.

    But if i create my own cacherefresher using CacheRefresherBase, how do I fire for example the RefreshAll method ? 

    Is the contentservice published event the way to go then like in my example ? If i'm correct events don't get distributed to the other servers ?

    Dave

     

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Jun 03, 2015 @ 13:39
    Shannon Deminick
    2

    If you're custom cache is tied to publishing only then you don't need your own ICacheRefresher, you can just listen to PageCacheRefresher.CacheUpdated

    Again, you only need a custom ICacheRefresher if your custom cache is affiliated with some custom actions in the back office - like a custom section/tree and probably your own custom db data. If that was the case, you'd use this to execute your ICacheRefresher:

    DistributedCache.Instance.RefreshAll(new Guid("YOURCACHEREFRESHERID")); 
    

    There are other refresh methods on DistributedCache.Instance that would allow you to refresh by id, remove, etc...

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jun 03, 2015 @ 13:55
    Dave Woestenborghs
    1

    Hi Shannon,

    I ended up hooking in to these events to update my cache that depends on Umbraco content

    PageCacheRefresher.CacheUpdated
    MediaCacheRefresher.CacheUpdated
    DictionaryCacheRefresher.CacheUpdated

    And created a custom ICacheRefresher to update cache for data from external sources.

    Now or caching syncing issues on a load balanced environment are gone. Thx for the clarifcation.

    If I find some time I will make a pull request for the documentation. But that will be somewhere after Codegarden.

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft