sammarcow
sammarcow

Reputation: 2956

asp.net cache within httpcontext

I want to cache objects being pulled from a database that do not often get modified, because every time the page loads nearly 2000 items are selected, causing a noticable performance issue during page load.

After review here and a few MSDN articles (most relevant is here) it seems that these are solutions to prevent a single user from making multiple roundtrips to the database, and that this cache will expire once the httprequest is closed.

Can any one clear the confusion, providing an applicable reference if found?

Upvotes: 5

Views: 773

Answers (1)

Chris Marisic
Chris Marisic

Reputation: 33098

You want to store the items in HttpRuntime.Cache items will exist in here for the duration of your app domain, they expire, or are scavenged. Which ever happens first. Note this is exactly the same as HttpContext.Current.Cache which points to HttpRuntime.Cache. Invoking the later is easier to work with in service layers since you don't have to be concerned about whether a context exists or not. The cache always exists.

Items stored in HttpContext.Current.Request.Items will only exist for the duration of that request. This is useful for storing single request information that could be read / wrote through multiple layers of your application.

Upvotes: 6

Related Questions