Reputation: 13835
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.HostingEnvironment
to 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.
Upvotes: 2
Views: 2037
Reputation: 66641
They are the same and both call the HttpRuntime.Cache
- From the source code:
public static Cache System.Web.Hosting.HostingEnvironment.Cache
{
get
{
return HttpRuntime.Cache;
}
}
and
public Cache System.Web.HttpContext.Cache
{
get
{
return HttpRuntime.Cache;
}
}
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
Upvotes: 3
Reputation: 51644
Both point to the same instance of the cache. In some cases you can't get HttpContext.Current
. Then you can always get the cache through System.Web.Hosting.HostingEnvironment.Cache
as a fallback.
Upvotes: 1