Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make per- http Request cache in ASP.NET 3.5

We using ASP.NET 3.5 (Controls-based approach) and need to have storage specific for one http request only.

Thread-specific cache with keys from session id won't work because threads are supposed to be pooled and therefore I have a chance to have data from some previous request in cache, which is undesirable in my case. I always need to have brand new storage for each request available through whole request.

Any ideas how to do it in ASP.NET 3.5?

like image 380
Artem Avatar asked Apr 27 '10 18:04

Artem


People also ask

What is Memorycache C#?

In-Memory Cache is used for when you want to implement cache in a single process. When the process dies, the cache dies with it. If you're running the same process on several servers, you will have a separate cache for each server. Persistent in-process Cache is when you back up your cache outside of process memory.

What is partial page caching?

This can be done by creating user controls for each area that needs to be cached. After you create user controls for the cached fragments of the web page, you can set their @ OutputCache attributes as if they were a regular web page. This process is referred to as fragment or partial page caching.

How can we cache data or cache web page in ASPX pages?

For output caching, an OutputCache directive can be added at the top of the . aspx page , specifying the duration (in seconds) that the page should be cached. All the attributes that we specify in an OutputCache directive are used to populate an instance of the System.

What is cache in ASP.NET with example?

ASP.NET provides caching of web pages through Page Output Caching. A page is enabled for caching using the @OutputCache directive. This directive is written at the top of the page which is to be cached. The code below shows the code in the hold to cache a web page for 60 seconds.


2 Answers

We have used HttpContext.Current.Items collection to do RequestScope caching. It works well.

like image 176
Valera Kolupaev Avatar answered Oct 16 '22 20:10

Valera Kolupaev


just to clarify what ggonsalv was referring to

http://msdn.microsoft.com/en-us/library/system.web.httpcontext.items.aspx

HttpContext.Items["key"] = value;

UPDATE: the mvc specific version

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.httpcontext(v=VS.100).aspx

like image 6
house9 Avatar answered Oct 16 '22 20:10

house9