Register   Login
     
  Categories  
  Archives  
  Authors  
  Blog  

Current Articles | Archives | Search |

Monday, April 24, 2006
Caching VaryByParams
By smcculloch @ 7:59 AM :: 1446 Views :: 1 Comments :: Article Rating :: Module Development, ASP.NET 2.0
 

I hope this tip helps someone, as it took me quite a while to get this working successfully.

Simple Gallery uses a number of HttpHandlers to serve up images, in particular, resized images. The caching API in .NET allows you to cache an entire pages/handlers output and vary the cache by parameters. Now, I've used this API quite a bit before, but never for multiple parameters.

Caching by multiple parameters is a perfect solution for Simple Gallery, but took a little while to get the syntax correct, as the API isn't exactly a collection for adding parameters like I first thought.

The actual code is as follows:-

context.Response.Cache.SetExpires(DateTime.Now.AddDays(1))
context.Response.Cache.VaryByParams("FileName") = True
context.Response.Cache.VaryByParams("HomeDirectory") = True
context.Response.Cache.VaryByParams("Width") = True
context.Response.Cache.VaryByParams("Height") = True

This will create cached instances of the handler's output on a page that is unique by the 4 parameters above. It seem to improve performance quite a bit, especially when using the higher quality compression that just got implemented.

Rating
Comments
comment By ReTox @ Wednesday, October 25, 2006 11:55 AM
try

Response.Cache.VaryByParams["param1;param2"]=true;

Name (required)

Email (required)

Website

Enter the code shown above: