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?
HttpContext. Current. Cache is a class that provides caching of any kind of serializable objects. It in itself equates to HttpRuntime.
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.
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.
HttpRuntime. Cache is much more than a simple dictionary. It offers thread-safety and cache expiration policies.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With