Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Wanted to know about - What is the different between HttpContext.Response.Cache and HttpContext.Current.Cache objects ? and What should be used in Asp.net MVC web application?

Why I am asking this question?

Because, I have my own [NoCache] attribute, which is responsible to avoid cache during the view redirection.

E.g.

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 am using this above attribute in my BaseController, like..

[NoCache]
public class BaseController : Controller
{

}

This works fine!

BUT, in the authentication part - I am storing some information in the cache by below mechanism

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

SO, my Question is..

I am using my custom attribute in base class of controller, which is responsible to clear the cache items, even though, I can still access the cache key and value throughout the application which was set by the Login method code..

Why this both cache mechanism act differently? What is the difference between these two?

Could you please suggest some idea or information on this.

like image 602
nunu Avatar asked Jan 31 '13 09:01

nunu


1 Answers

HttpContext.Current.Cache is a class that provides caching of any kind of serializable objects. It in itself equates to HttpRuntime.Cache to muddy your waters even more.

We use HttpContext.Current.Cache usually to cache data from our database servers. This saves having to continually ask the database for data that changes little. This is entirely server-side and does not affect the client.

HttpResponse.Cache allows you to set up and control the various cache control headers sent with the response content. This tells the client (and any in-between proxies) what kind of caching you suggest. Note I say suggest, because it's entirely arbitrary whether the client honors it or not.

like image 62
Lloyd Avatar answered Oct 12 '22 23:10

Lloyd