OrchardCMS / Orchard

Orchard is a free, open source, community-focused Content Management System built on the ASP.NET MVC platform.
https://orchardproject.net
BSD 3-Clause "New" or "Revised" License
2.38k stars 1.12k forks source link

Storage provider of DefaultCacheStorageProvider Output Cache #6675

Open ghost opened 8 years ago

ghost commented 8 years ago

Hey I was exploring the code of DefaultCacheStorageProvider class because I wanted to invalidate it for different instances with a message bus. But I found that is uses HttpContext.Cache instead of MemoryCache that was causing exception since we don't have HttpRequest that will init the HttpContext. So my question is what is the reason to use HttpContext.Cache instead of MemoryCache?

donaldboulton commented 8 years ago

It is mem cache = no? Stack Overflow The HttpRuntime.Cache is available to all "users" (IP's, IIS Session, etc.) on the 'net but HttpContext is only available to the current IIS session? See: Peter Johnson's page. Need it under the correct namespace in VS 2015 hover over HttpContext.Cache in Modules\Orchard.OutputCache\Services\CacheService.cs and see what message it gives ya. using System.Web.Caching; MSDN System.Web.Caching Namespace

sebastienros commented 8 years ago

To answer the original question, we don't use MemoryCache as we want a store that can be cleared when the system requests memory. This is the case with HttpContext.Cache, not with MemoryCache.

sebastienros commented 8 years ago

There is a way to tag the cache entries though, that you could use to define custom invalidation. If it lack more extensibility feel free to raise the concern. Also, cache entries are automatically removed when a content item they rendered has been changed.

ghost commented 8 years ago

So my concern is that I want to keep the Cache in memory but I want to invalidate it when an item is changed in distributed environment. So I created a decorator of TagCache class that hooks on RemoveByTag and sends message though message bus to all subscribers that will have to invalidate their in memory cache but the _workContext.HttpContext object is disposed when some of the subscribed machines try to invalidate the cache entries for this tagged item. I created an implementation of the storage provider with the memory cache instead of HttContext.Cache and it worked as intended. As far as I know when you add a cache entry into the MemryCache you can specify the CacheItemPolicy.Priority to Default and by doing this you will have again a cache that can be cleared when the system requests memory.

@donaldboulton here is something for you to read my friend https://msdn.microsoft.com/en-us/library/dd997357.aspx

ghost commented 8 years ago

Also Microsoft recomeds using of System.Runtime.Caching instead of System.Web.Caching for applications build over .NET Framework 4.0 and above. That is why I wonder why did you decide to use the "old" way of implementation.