Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use output caching on .ashx handler

How can I use output caching with a .ashx handler? In this case I'm doing some heavy image processing and would like the handler to be cached for a minute or so.

Also, does anyone have any recommendations on how to prevent dogpiling?

like image 996
Kieran Benton Avatar asked Jul 10 '09 14:07

Kieran Benton


People also ask

How does output cache work?

The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked. Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index.

Where is output cache stored?

The output cache is located on the Web server where the request was processed. This value corresponds to the Server enumeration value. The output cache can be stored only at the origin server or at the requesting client. Proxy servers are not allowed to cache the response.

How do I cache an ASPX page?

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.

How can use cache in ASP.NET MVC?

In ASP.NET MVC, there is an OutputCache filter attribute that you can apply and this is the same concept as output caching in web forms. The output cache enables you to cache the content returned by a controller action. Output caching basically allows you to store the output of a particular controller in the memory.


1 Answers

There are some good sources but you want to cache you processing server side and client-side.

Adding HTTP headers should help in the client side caching

here are some Response headers to get started on..

You can spend hours tweaking them until you get the desired performance

//Adds document content type context.Response.ContentType = currentDocument.MimeType; context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(10)); context.Response.Cache.SetMaxAge(new TimeSpan(0,10,0));  context.Response.AddHeader("Last-Modified", currentDocument.LastUpdated.ToLongDateString());  // Send back the file content context.Response.BinaryWrite(currentDocument.Document); 

As for server side caching that is a different monster... and there are plenty of caching resources out there...

like image 114
BigBlondeViking Avatar answered Oct 04 '22 04:10

BigBlondeViking