I found this article who explain how to use cache items expiration to make a scheduled job for ASP.NET applications without using any scheduler or windows services.
It is really interesting for me!
In the article, the author is using HttpContext.Current.Cache to add an item. So when item expires in cache a treatment can be done..
But in his article he’s making a ‘false’ request from the server to himself to have access to HttpContext to reach Cache and add item again when the previous expires.
I tried to use System.Web.Hosting.HostingEnvironmentto access Cache without any HttpContext (so no need to make a ‘DummyRequest’) and it seems working.
Is there something I don’t understand or know about this cache? Is the HttpContext and the HostingEnvironment cache different? I think this is the same thing, intellisense describe the twos like the ‘application cache’ without differences.
They are the same and both call the
HttpRuntime.Cache– From the source code:and
Also this is state on the MSDN:
HostingEnvironment.Cache
Gets the Cache instance for the current application.
Namespace: System.Web.Hosting
Assembly: System.Web (in system.web.dll)
ref: http://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.cache(VS.85).aspx
HttpContext.Cache Property
Gets the Cache object for the current application domain.
Namespace: System.Web
Assembly: System.Web (in System.Web.dll)
ref: http://msdn.microsoft.com/en-us/library/system.web.httpcontext.cache.aspx