Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top