Cache dynamic image served by the http handler

I developed a http handler for dynamically displaying images.

I have the corresponding last modified date for each image.

So, I cached the HTTP headers as shown below:

public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpeg"; DateTime lastModified = Convert.ToDateTime("2/1/2013 12:00:00 AM"); string eTag = "test.JPG" + lastModified.ToString(); // set cache info context.Response.Cache.VaryByHeaders["If-Modified-Since"] = true; context.Response.Cache.VaryByHeaders["If-None-Match"] = true; context.Response.Cache.SetLastModified(lastModified); context.Response.Cache.SetETag(eTag); //Set cache control header context.Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate); Byte[] imageBytes = CreateImage(); context.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length); } 

Note: here lastModified will be displayed dynamically for each image, and the name will be different for each image.

Answer Headers

 HTTP/1.1 200 OK Cache-Control: private Content-Type: image/jpeg Last-Modified: Thu, 31 Jan 2013 18:30:00 GMT Etag: test.JPG2/1/2013 12:00:00 AM Vary: If-Modified-Since, If-None-Match Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RTpcTGVhcm5pbmdcQ2FjaGVcR2V0SW1hZ2UuYXNoeA==?= X-Powered-By: ASP.NET Date: Mon, 08 Jul 2013 11:22:26 GMT Content-Length: 384411 

Request Headers

 GET /GetImage.ashx HTTP/1.1 Host: localhost:50432 User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Connection: keep-alive Pragma: no-cache Cache-Control: no-cache 

Question

This page is cached correctly. But the next time I load a page with a different lastModified value, which does iteration too.

But the page does not fall into the handler at all. It always shows a cache image ...

And since it does not hit the handler, how can I check the lastModified value and execute accordingly.

+4
source share

Source: https://habr.com/ru/post/1490257/


All Articles