Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 814
sammarcow Avatar asked May 06 '11 16:05

sammarcow


People also ask

What is HttpContext cache?

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

How do I cache in asp net?

To manually cache application data, you can use the MemoryCache class in ASP.NET. ASP.NET also supports output caching, which stores the generated output of pages, controls, and HTTP responses in memory. You can configure output caching declaratively in an ASP.NET Web page or by using settings in the Web. config file.

How to cache in ASP net Core?

ASP.NET Core supports several different caches. The simplest cache is based on the IMemoryCache. IMemoryCache represents a cache stored in the memory of the web server. Apps running on a server farm (multiple servers) should ensure sessions are sticky when using the in-memory cache.

What is HttpRuntime cache?

HttpRuntime. Cache is much more than a simple dictionary. It offers thread-safety and cache expiration policies.


1 Answers

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.

like image 91
Chris Marisic Avatar answered Sep 28 '22 03:09

Chris Marisic