Copied to clipboard

Flag this post as spam?

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


  • Stefan Kip 1614 posts 4131 karma points c-trib
    Aug 18, 2016 @ 13:06
    Stefan Kip
    0

    Refresh custom cache on Azure together with Umbraco refreshes

    Hi,

    Small introduction: After a small Q&A on twitter, Stephan asked me to put it continue on our :-) The conversation so far:

    Me: Is it possible to hook into the umbraco SQL based cache updates? To invalidate custom cache on Azure on (un)publish?

    Stephan: Not sure I understand the question... You can hook into cache refresher events... Is that what you want?

    Me: Yeah I guess! So a cache refresh instruction is sent to all nodes and I'd like to refresh a custom cache too (on Azure). Do you have an example by any chance? ;-)

    Long description:
    I'm working on a Web API project based on Umbraco, which will be hosted on Azure.
    Some GET endpoint methods got server-side caching applied, based on the ASP.NET Web API CacheOutput package ([CacheOutput(ServerTimeSpan = int.MaxValue)]).
    The used cache type is MemoryCacheDefault and I guess on Azure this is a private cache of the server.
    So I'm looking for a way to call my cache clearing code whenever something happens in Umbraco, like (un)publishing, which alters the umbraco cache data.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 18, 2016 @ 13:17
    Jeroen Breuer
    0

    Here is an example of a custom refresher:

    The refresher:

    /// <summary>
    /// The relation cache refresher.
    /// </summary>
    internal class RelationCacheRefresher : JsonCacheRefresherBase<RelationCacheRefresher>
    {
        /// <summary>
        /// Gets the id.
        /// </summary>
        public static Guid Id
        {
            get
            {
                return new Guid("7b8053d6-6ff4-46e2-94c0-115055a8a192");
            }
        }
    
        /// <summary>
        /// Gets the unique identifier.
        /// </summary>
        public override Guid UniqueIdentifier
        {
            get
            {
                return Id;
            }
        }
    
        /// <summary>
        /// Gets the name.
        /// </summary>
        public override string Name
        {
            get
            {
                return "Relation Cache Refresher";
            }
        }
    
        /// <summary>
        /// Returns the real instance of the object ('this') for use  in strongly typed events
        /// </summary>
        protected override RelationCacheRefresher Instance
        {
            get
            {
                return this;
            }
        }
    }
    

    Call the refresher:

    private void ContentServiceSaved(IContentService sender, SaveEventArgs<IContent> e)
    {
        DistributedCache.Instance.RefreshAll(RelationCacheRefresher.Id);
    }
    

    Execute the refresher:

    RelationCacheRefresher.CacheUpdated += (sender, args) =>
    {
        // Clear cache or something
    };
    

    Jeroen

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Aug 19, 2016 @ 07:36
    Stefan Kip
    0

    Thanks Jeroen, I did a quick Google search on JsonCacheRefresherBase to find more information about what it is and I came across this post, where Shannon explains that there's already an event available for exactly this purpose: PageCacheRefresher.CacheUpdated.

    Tried it just now and it works perfectly :-)

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 19, 2016 @ 07:43
    Jeroen Breuer
    0

    Ah ok I thought you already knew about PageCacheRefresher.CacheUpdated and you wanted to have your own version of it. But yes if you're on Azure and you want to update all servers you need to use PageCacheRefresher.CacheUpdated. It's the new ContentService.Published ;-).

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft