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?
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?
you can use like this
Old, question but the answer didn't really mentioned the server-side handling.
As in the winning answer, I would use this for the
client side
:and for the
server side
, since you are using a ashx instead of a web page, I'm assuming that you are directly writing the output to theContext.Response
.In that case you could use something like this (in this case I want to save the response based on parameter "q", and Im using a sliding window expiration)
I used the following with success and thought it worthwhile to post here .
Manually controlling the ASP.NET page output cache
From http://dotnetperls.com/cache-examples-aspnet
Setting cache options in Handler.ashx files
Sample code excerpted:
C# to cache response for 1 hour
The solution with the OutputCachedPage works fine, however at a price of the performance, since you need to instantiate an object derived from the System.Web.UI.Page base class.
A simple solution would be to use the Response.Cache.SetCacheability, as suggested by some of the above answers. However for the response to be cached at the server (inside Output Cache) one needs to use HttpCacheability.Server, and set a VaryByParams or VaryByHeaders (note that when using VaryByHeaders URL can't contain a query string, since the cache will be skipped).
Here's a simple example (based on https://support.microsoft.com/en-us/kb/323290):
Hint: you monitor the caching in the Performance Counters "ASP.NET Applications__Total__\Output Cache Total".
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
As for server side caching that is a different monster... and there are plenty of caching resources out there...