The HTTP Spec contains various headers used to convey information about the "cacheability" of resources to clients. Wikipedia also has a good summary here.
Basically, when a server hands the client some resource, it comes with various metadata. Based on this metadata, the client decides whether or not to cache the resource, and if so, for how long.
You can solve your problem various ways. The easiest way may just be to always set the Expires
header on your image response to a value of January 1, 1970 (Unix Epoch). This will tell the client to expire the cache entry in 1970. Since it's always past 1970, the client will never cache the resource and will always request a new copy.
This approach does have its downsides, namely that you needlessly incur extra bandwidth and load on the server if the image never changes. That's where the ETag and LastModified headers come in.
The server can calculate a short hash for every resource. Let's say one image's hash works out to a423fedc
. The client will store this hash and when asking for the resource a second time, provide it to the server using the If-None-Match
request header. If the server calculates that the ETag for that resource is unchanged, it will just send back a 304 Not Modified
and the client can use the cached version. ETags are explained further here.
But I think the best solution in your case is the Last-Modified
header. The server will send a date corresponding to the last time an image was modified. Upon reloading the page, the client sends back this same date as part of the If-Modified-Since
header. If the image has been modified since that date, the server sends the updated image. If not, it sends back the 304 Not Modified
code.
I'm not an ASP developer, so I can't tell you how to set these headers on your response, but if you can figure out how, every web browser will do the caching correctly. I did find this article which looks like it can tell you how to do this in ASP-land, but just knowing about the headers should be enough to get you started.
For debugging, it's a good idea to test with a browser like Firefox using the Firebug plugin. It can show you the headers on your requests and responses, so you know what's actually going over the wire. If you are using a cached version, or if the server isn't returning new data, you'll actually see the 304 response in Firebug, which can be useful.