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;
}
}
}
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?
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.
Clear cache issue
Using umbraco 7.5.7 and in my controller i am adding item to cache
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:
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
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?
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
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
ok sorted it was runtime cache that needed to be used. ignore last post we had another level of caching doh!!!
is working on a reply...