Script images provided through script

I have a script that, using several query variables, provides an image. I also use URL rewriting in IIS 7.5.

Images have this URL: http: //mydomain/pictures/ajfhajkfhal/44/thumb.jpg or http: //mydomain/pictures/ajfhajkfhal/44.jpg

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

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

Images don't change so often, so if the cache is at least stored for 30 minutes (or until the file is modified), this will be best.

I am using .NET / C # 4.0 for my site. I tried to set some cache parameters in C #, but it seems to me that I do not know how to cache these images (client side), while my static images are cached properly.

EDIT I use the following parameters to cache the image on the client side, where 'fileName' is the physical file name (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 Thank you for pointing out that this was a really stupid mistake;). I changed it to 30 minutes (DateTime.Now.AddMinutes (30)).

But this does not solve the problem. I really think the problem is in Firefox. I use Firebug to track each request and somehow, I think I'm doing something fundamentally wrong. Regular images (cached and static) return the response code "304 (Not Modified)", and my page always returns "200 (OK)".

alt text http://images.depl0y.com/capture.jpg

+3
1

, "script", Picture.aspx, , # , script.

API , ASP.NET.
, , - . 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 bytes
    HttpContext.Current.Cache[fileName] = bytes; // Set the cache
}

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

, , , , , .

EDIT:

, :

Response.Cache.SetCacheability(HttpCacheability.Public);

HttpCacheability. , . (, , - )

ASP.NET HTTP.
, (, ), " !"

, .

2:

- SetExpires(DateTime.Now.AddTicks(600)). 600 - ... (1 = 10000000 )
, , , .

:

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

(TimeSpan.FromMinutes , new TimeSpan(...).)

+1

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


All Articles