开发者

Cache images provided through script

开发者 https://www.devze.com 2022-12-31 01:15 出处:网络
I have a script, which by using several querystring variables provides an image. I am also using URL rewriting within IIS 7.5.

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/pictur开发者_运维知识库es/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 http://images.depl0y.com/capture.jpg


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(...).)

0

精彩评论

暂无评论...
验证码 换一张
取 消