Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Browser Caching control issue
kjensonmpi
#1 Posted : Friday, May 28, 2010 10:52:36 AM(UTC)
Groups: Member
Joined: 5/28/2010(UTC)
Posts: 3
Location: Orem, UT

I am trying to implement a caching control mechanism for my application which would allow me to expire the browser cache for .CSS, .JSS and .PNG files every morning at a specific time, or after a specified interval (say 8 hrs).

Default behaviour I'm seeing with static content:
1. 200 OK - first time load
2. 304 OK - second time load, Expires header set to 24 hrs.

I've tried
1. Putting IE7 cache control settings in web.config:
Code:
<staticContent>
<clientCache cacheControlCustom="public;max-age" cacheControlMode="UseMaxAge" cacheControlMaxAge="0.00:01:00" />
</staticContent>


2. Creating a global.asax and setting headers on every request (yuch!)

Code:
Response.Cache.SetCacheability(HttpCacheability.Private);
//Response.Cache.SetExpires(DateTime.Now.AddMinutes(1));
TimeSpan span = new TimeSpan(0, 0, 1, 0);
Response.Cache.SetMaxAge(span);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);


Note: SetExpires didn't work at all. Set MaxAge worked the first time, then MaxAge was automatically set to 0 from then on..

I've considered, but not tried yet:
1. Creating an HttpModule to try to implement this (similar to global.asax)

Can anyone tell me how to accomplish this under Cassini? It has always been very simple under apache or IIS..

thanks

Ken Jenson
Ultidev Team
#2 Posted : Monday, May 31, 2010 11:29:39 AM(UTC)
Ultidev Team

Groups: Administration
Joined: 11/3/2005(UTC)
Posts: 2,253

Thanks: 28 times
Was thanked: 60 time(s) in 59 post(s)
Ken,

<staticContent> is IIS-specific setting, not ASP.NET. To work around this your best bet is probably to use Application_BeginRequest() method of global.asax.

We'd like to point out a couple of things though.

1. Even when cache expiration instructions are send to a browser, browser will still attempt to get a file, but it will do a conditional GET request, with either "If-Modified-Since" request header, or with tag request header. Either way, server will still get a request and will need to decide whether to send back 304 or send the whole response. IE, BTW, understands a couple of non-standard headers that will preclude it from even sending a request if resource is cached and has not expired: google for "post-check" and "pre-check" headers if you are interested in this behaviour.

2. UltiDev Cassini does not support server-side caching functionality out of the box. For example, if conditional GET request arrives for a static file like CSS, Cassini won't try to determine whether file was modified and will treat such request as regular GET and will send the full content of the file as a response. If you would like to implement proper caching, you could create an HttpModule to have a general proper checking of static files dates and returning 304 when necessary. Here's an example of a handler (not a module) that does similar thing:
http://ultidevwebbasedmp3pl.codeplex.com/SourceControl/changeset/view/43771#474086

Please let us know if this information has helped.

Best regards,
UltiDev Team.
Please donate at http://www.ultidev.com/products/Donate.aspx to help us improve our products.
kjensonmpi
#3 Posted : Thursday, June 3, 2010 3:19:03 PM(UTC)
Groups: Member
Joined: 5/28/2010(UTC)
Posts: 3
Location: Orem, UT

Guys, thanks for your feedback.

Here is my code in global.asax.cs that works (expires js, css and images every 8 hours):

Code:
protected void Application_BeginRequest(object sender, EventArgs e)
{
string url = Request.Url.ToString();

if (url.LastIndexOf(".css") != -1 || url.LastIndexOf(".js") != -1 || url.LastIndexOf(".png") != -1)
{
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
TimeSpan span = new TimeSpan(0, 0, 8, 0);
Response.Cache.SetMaxAge(span);
}
}


Ultidev Team
#4 Posted : Friday, June 4, 2010 6:31:56 AM(UTC)
Ultidev Team

Groups: Administration
Joined: 11/3/2005(UTC)
Posts: 2,253

Thanks: 28 times
Was thanked: 60 time(s) in 59 post(s)
Ken,

These are properly done cache-control settings, but your browser will still request the files even if they are cached, even though they will do it using conditional GET to allow the server to return 304 instead of the full content. Cassini does not return 304 in its current version. Check out what is really going on on the wire by employing Fiddler - you will probably see that caching the way you have implemented does really nothing: you are likely still getting full responses back from Cassini. To make caching really work, your Application_BeginRequest() would have to rely on dates or tags to determine whether the file has changed and then return 304.

Best regards,
UltiDev Team.
Please donate at http://www.ultidev.com/products/Donate.aspx to help us improve our products.
kjensonmpi
#5 Posted : Monday, June 7, 2010 11:02:02 AM(UTC)
Groups: Member
Joined: 5/28/2010(UTC)
Posts: 3
Location: Orem, UT

Actually, I was using Fiddler to validate the results (also WireShark)

IE by default will cache something forever, and never go back to the server at all.

Once these cache headers were set however, the files were downloaded once, and then expired after 8 hrs...downloaded, and max age set again.

It's working right now as I had planned so I'm not sure what else you think I would need to do. I've been dealing with these types of issues for years (mostly under apache where they are easy to control) so I do know what I'm doing. It just took a lot of trial and error to get this going under Cassini.

Regards

Ken

Ultidev Team
#6 Posted : Monday, June 7, 2010 4:16:53 PM(UTC)
Ultidev Team

Groups: Administration
Joined: 11/3/2005(UTC)
Posts: 2,253

Thanks: 28 times
Was thanked: 60 time(s) in 59 post(s)
Thank you for the information, Ken.

Best regards,
UltiDev Team.
Please donate at http://www.ultidev.com/products/Donate.aspx to help us improve our products.
Rss Feed  Atom Feed
Users browsing this topic
Guest (5)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.