What is the difference between HttpContext.Current.Cache and HttpContext.Response.Cache?

Want to know about - what is different between HttpContext.Response.Cache and HttpContext.Current.Cache ? and what should be used in an ASP.NET MVC web application?

Why am I asking this question?

Since I have my own [NoCache] attribute, which is responsible for [NoCache] cache during view redirection.

eg.

 public override void OnActionExecuting(ActionExecutingContext filterContext) { var cache = filterContext.HttpContext.Response.Cache; cache.SetExpires(DateTime.Now.AddDays(-1)); cache.SetValidUntilExpires(false); cache.SetRevalidation(HttpCacheRevalidation.AllCaches); cache.SetCacheability(HttpCacheability.NoCache); cache.SetNoStore(); base.OnActionExecuting(filterContext); } 

And I use this attribute above in my BaseController , for example ..

 [NoCache] public class BaseController : Controller { } 

It works great!

BUT, in terms of authentication - I store some information in the cache below the mechanism

 public ActionResult Login() { HttpContext.Current.Cache.Insert("someKey", "someValue", null, expiredTime.Value, Cache.NoSlidingExpiration); return view(); } 

SO, my question is:

I use my own attribute in the base class of the controller, which is responsible for clearing cache elements, although I can still access the key and cache value in the entire application that was set by the login method code.

Why do both cache mechanisms act differently? What is the difference between the two?

Could you offer some idea or information about this.

+4
source share
1 answer

HttpContext.Current.Cache is a class that provides caching of any type of serializable objects. This in itself means HttpRuntime.Cache to further pollute your waters.

Usually we use HttpContext.Current.Cache to cache data from our database servers. This saves the need to constantly query the database for data that changes little. This is a fully server side and does not affect the client.

HttpResponse.Cache allows you to configure and manage various cache control headers sent with response content. This tells the client (and any intermediate proxies) what kind of caching you offer. Note. I say, I offer, because he is completely arbitrary if the client honors him or not.

+10
source

All Articles