Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Mar 02, 2017 @ 12:51
    Ismail Mayat
    0

    Using umbraco 7.5.7 and in my controller i am adding item to cache

            public SiteGlobalContent GetSiteGlobalContentNode()
        {
            var umbracoRequestCache = ApplicationContext.Current.ApplicationCache.RequestCache;
    
            var siteGlobalContentNode = umbracoRequestCache.GetCacheItem(ApplicationConstants.CacheKeys.GetSiteGlobalContentNode, () =>
            {
                var configurationFolder = GetConfigurationFolder();
                var siteGlobalContent = configurationFolder?.Children.FirstOrDefault(x => x.DocumentTypeAlias == SiteGlobalContent.ModelTypeAlias);
                return siteGlobalContent;
            });
    
            return siteGlobalContentNode as SiteGlobalContent;
        }
    

    all works fine.

    Now on publish of that siteglobalcontent doc type i need to clear the cache item. So I have events class and am implementing published event and doing:

            private void ClearCache(PublishEventArgs<IContent> publishEventArgs)
        {
            foreach (var item in publishEventArgs.PublishedEntities)
            {
                if (item.ContentType.Alias == SiteGlobalContent.ModelTypeAlias)
                {
                   var cache = ApplicationContext.Current.ApplicationCache.RequestCache;
                   cache.ClearCacheItem(ApplicationConstants.CacheKeys.GetSiteGlobalContentNode);
                   return; 
                }
            }
        }
    

    However my item is not removed from cache.

    Looking at the docs https://our.umbraco.org/documentation/Reference/Cache/ there is mention of cache clear using events but nothing there. Does anyone know how to do this?

    Regards

    Ismail

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Mar 02, 2017 @ 12:57
    Matt Brailsford
    101

    Is it because your are using RequestCache which gets renewed on each request? If you are wanting to store things for a while, use RuntimeCache or StaticCache and see if that helps?

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Mar 03, 2017 @ 08:28
    Ismail Mayat
    1

    Matt,

    So I have updated to use runtime cache however its still not removed. I have seen this before with another cache library i used before. So here is my theory. When adding to runtime cache or any of the cache methods in umbraco cache we pass in delegate method to get the item so

    umbracoRequestCache.GetCacheItem(ApplicationConstants.CacheKeys.GetSiteGlobalContentNode, () =>
        {
            var configurationFolder = GetConfigurationFolder();
            var siteGlobalContent = configurationFolder?.Children.FirstOrDefault(x => x.DocumentTypeAlias == SiteGlobalContent.ModelTypeAlias);
            return siteGlobalContent;
        });
    

    After the key we pass in the delegate. I am fairly certain but would need to check the source of umbraco cache that when you do a remove its really more an update. So the delegate is called again so item is readded to cache effectively updating rather than fully removing. However because in the delegate method which gets the item to put into cache we are using umbraco context and httpcontext under the hood and in event handler we dont have those so its silently failing and there cache is not being updated.

    As I say i have seen this before with a library i have used in the past that adds stuff via delegates and locks etc however when it came to removing it would not remove when the delegate was using anything that needed httpcontext or umbraco context.

    Regards

    Ismail

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Mar 03, 2017 @ 12:30
    Ismail Mayat
    0

    ok sorted it was runtime cache that needed to be used. ignore last post we had another level of caching doh!!!

Please Sign in or register to post replies

Write your reply to:

Draft