I have been reading a bit about caching in c# 3.5. Got a bit confused and would appreciate clarification on elaborating what is the difference between
HttpContext.Current.Cache vs HttpRuntime.Cache vs System.Web.Caching.Cache
Also, I have read using any of the above in non-web application is not recommended but it works. What are the drawbacks?
System.Web.Caching.Cacheis the type which implements the cache for a Web application.HttpContext.Current.Cacheis just a wrapper and returnHttpRuntime.Cachewhich is nothing but instance ofSystem.Web.Caching.Cache.Update
Refer to Is it OK to use HttpRuntime.Cache outside ASP.NET applications? for your second part.
Update: Why HttpRuntime.Cache needs to be wrapped?
In my Personal Opinion,
HttpContextis the type which gets passed toIHttpHandler.ProcessPostBackandHttpApplicationexposesHttpContextwhich is passedIHttpModule.Init. This would ensure all the dependencies are injected using Method injection. Hence they introduced a level of indirection.The Handlers and Modules should be ignorant of the HttpRuntime on which they are hosted. While you are in a ASP.NET page, it is advisable to use
this.Page.CacheinsteadHttpContext.Current.CacheorHttpRuntime.Cacheas using HttpContext.Current would involve a overhead of resolving the current thread andHttpRuntime.Cachewould create an external dependency.Page.Cacheis initialized with theHttpContext.Cachewhich is passed toProcessRequest