Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching the response of an ASP.NET HTTP Handler server and client side

Is it possible to cache the response of a http handler on the server and on the client?

This doesn't seem to be doing the trick:

_context.Response.Cache.SetCacheability(HttpCacheability.Public); _context.Response.Cache.SetExpires(DateTime.Now.AddDays(7));

The _context is the HTTPContext passed as an argument to the ProcessRequest method on the IHttpHandler implementation.

Any ideas?

Update: The client does cache images that are loaded through the httphandler, but if another client does the same call, the server hasn't got it cached. So for each client that asks for the image, the server goes to the database (and filestream). If we use a aspx page instead of a httphandler together with a caching profile, then the images are cached both on the client and the server.

like image 350
Bert Vandamme Avatar asked Mar 11 '10 16:03

Bert Vandamme


People also ask

Does Web Cache act as both client and server?

A Web cache (or HTTP cache) is a system for optimizing the World Wide Web. It is implemented both client-side and server-side. The caching of multimedias and other files can result in less overall delay when browsing the Web.

What is HTTP response cache?

The HTTP cache stores a response associated with a request and reuses the stored response for subsequent requests. There are several advantages to reusability. First, since there is no need to deliver the request to the origin server, then the closer the client and cache are, the faster the response will be.

What is response caching in .NET Core?

Response caching reduces the number of requests a client or proxy makes to a web server. Response caching also reduces the amount of work the web server performs to generate a response. Response caching is controlled by headers that specify how you want client, proxy, and middleware to cache responses.


2 Answers

With IIS7, this can be done in web.config.

For example, suppose your ashx url is:

/ashxfiles/myhandler.ashx

...and you want to vary by querystring params:

id, lang

Add the following to your web.config:

<location path="ashxfiles">
    <system.webServer>
        <caching>
            <profiles>
                <add extension=".ashx" policy="CacheForTimePeriod" duration="00:00:10" varyByQueryString="id, lang" />
            </profiles>
        </caching>
    </system.webServer>
</location>

This rule applies to all .ashx files in the directory, so you might want to put your .ashx files in separate folders if you need more granular control of the cache settings.

like image 199
frankadelic Avatar answered Sep 30 '22 01:09

frankadelic


Thanks for your answer in the comments.

Cache.SetCacheability is used to define whether a proxy or the client is allowed to cache, not on the server.

Have a look at IIS 7 where it is explained how to cache the output of an HTTP handler at the server.

like image 38
Timores Avatar answered Sep 30 '22 01:09

Timores