Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between HttpContext's Cache and Items Properties?

What is the difference between the HttpContext class's Cache and Items properties?

From the MSDN Documentation:

Cache
Gets the Cache object for the current application domain.

Items
Gets a key/value collection that can be used to organize and share data between an IHttpModule interface and an IHttpHandler interface during an HTTP request.

I don't really understand what that documentation is trying to explain.

While working on ASP.NET web applications, I have often used Items for per-request caching of data so that multiple user controls don't end up looking up the same data from the database. This is described in this article.

Today though, I came across usages of the Cache property for, what looked like, per-request caching. I tried to understand the difference but couldn't find any good documentation comparing these two. So I would like to know...

What is the difference between HttpContext's Cache and Items properties? Please try to elaborate with examples of why you would choose to use one over the other in different real-world scenarios.

like image 946
Jesse Webb Avatar asked Feb 07 '13 16:02

Jesse Webb


1 Answers

Items is per request, so it's only available for that given user for that given HTTP request. Cache is stored in memory for a persistent period of time, and it not dependent on the specific user. So cache can be shared across multiple users across multiple requests, but Items is per user per request.

It depends on how long you want the data to live. I use Items to store things like the ObjectContext or DbContext in EF, which I only want to be shared for that given request. But reference table data all users will use across many requests, so caching would be better.

like image 65
Brian Mains Avatar answered Oct 01 '22 22:10

Brian Mains