views:

129

answers:

1

I have a script, which by using several querystring variables provides an image. I am also using URL rewriting within IIS 7.5.

So images have an URL like this: http://mydomain/pictures/ajfhajkfhal/44/thumb.jpg or http://mydomain/pictures/ajfhajkfhal/44.jpg

This is rewritten to: http://mydomain/Picture.aspx?group=ajfhajkfhal&id=44&thumb=thumb.jpg or http://mydomain/Picture.aspx?group=ajfhajkfhal&id=44

I added caching rules to IIS to cache JPG images when they are requested. This works with my images that are REAL images on the disk. When images are provided through the script, they are somehow always requested through the script, without being cached.

The images do not change that often, so if the cache at least is being kept for 30 minutes (or until file change) that would be best.

I am using .NET/C# 4.0 for my website. I tried setting several cache options in C#, but I cant seem to find how to cache these images (client-side), while my static images are cached properly.

EDIT I use the following options to cache the image on the client side, where 'fileName' is the physical filename of the image (on disk).

context.Response.AddFileDependency(fileName);
context.Response.Cache.SetETagFromFileDependencies();
context.Response.Cache.SetLastModifiedFromFileDependencies();
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddTicks(600));
context.Response.Cache.SetMaxAge(new TimeSpan(0, 5, 0));
context.Response.Cache.SetSlidingExpiration(true);
context.Response.Cache.SetValidUntilExpires(true);
context.Response.ContentType = "image/jpg";

EDIT 2 Thanks for pointing that out, that was indeed a very stupid mistake ;). I changed it to 30 minutes from now (DateTime.Now.AddMinutes(30)).

But this doesnt solve the problem. I am really thinking the problem lies with Firefox. I use Firebug to track each request and somehow, I am thinking I am doing something fundamentally wrong. Normal images (which are cached and static) give back an response code "304 (Not Modified)", while my page always gives back a "200 (OK)".

alt text

A: 

If what you mean by "script" is the code in your Picture.aspx, I should point out that C# is not a scripting language, so it is technically not a script.

You can use the Caching API provided by ASP.NET.
I assume you alread have a method which contains something like this. Here is how you can use the Caching API:

string fileName = ... // The name of your file
byte[] bytes = null;

if (HttpContext.Current.Cache[fileName] != null)
{
    bytes = (byte[])HttpContext.Current.Cache[fileName];
}
else
{
    bytes = ... // Retrieve your image's bytes
    HttpContext.Current.Cache[fileName] = bytes; // Set the cache
}

// Send it to the client
Response.BinaryWrite(bytes);
Response.Flush();

Note that the keys you use in the cache must be unique to each cached item, so it might not be enough to just use the name of the file for this purpose.

EDIT:

If you want to enable caching the content on the client side, use the following:

Response.Cache.SetCacheability(HttpCacheability.Public);

You can experiment with the different HttpCacheability values. With this, you can specify how and where the content should be cached. (Eg. on the server, on proxies, and on the client)

This will make ASP.NET to send the client the caching rules with the appropriate HTTP headers.
This will not guarantee that the client will actually cache it (it depends on browser settings, for example), but it will tell the browser "You should cache this!"

The best practice would be to use caching on both the client and the server side.

EDIT 2:

The problem with your code is the SetExpires(DateTime.Now.AddTicks(600)). 600 ticks is only a fraction of a second... (1 second = 10000000 ticks)
Basically, the content gets cached but expires the moment it gets to the browser.

Try these:

context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(5));
context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(5));

(The TimeSpan.FromMinutes is also more readable than new TimeSpan(...).)

Venemo
Hmm, the file is then cached server side I guess, but I would love the images to be cached client side (if enabled in the browser), but now it seems only to cache the static images.
Wim Haanstra
@Wim - You can use client-side caching by specifying some http headers. This can be done with ASP.NET easily, I edited my answer to show you how.
Venemo
@Venemo, this is what I tried. But somehow my Firefox keeps reloading all the images, while I am at default cache settings. When I look at about:cache and check if my images are cached, they are in there.I think somehow Firefox always reloads those images, even if they are cached.I edited my question, to show which options I use to cache client-side. I think this is somehow a browser thing or something.
Wim Haanstra
@Wim - Edited my answer again. I believe the problem was in your code which sets the expiration date.
Venemo
Somehow that doesnt solve my problem. i edited my post to show you what my headers are responding..
Wim Haanstra
@Wim - I recommend you to remove every setting from your code except the SetCacheability, SetExpires, and ContentType.If that doesn't solve it, it may be a bug in Firefox. Also note that no matter what the status code is, the browser may still cache the result. (And if you press F5 in FF, it will also refresh the cached items.)
Venemo