ASP.Net Caching


First off, I'm not going to be talking about saving an item to the view state, session state, etc. I'm talking about caching an item (for instance an HTML page) on the client's machine. And for anyone that has access to the IIS server itself and can turn the content expiration on or make it so that everything expires immediately (note that if you're using Mono, that link wont help you much). However that isn't always the case. But thankfully, ASP.Net allows us to set that up directly on each individual page using OutputCache.

            context.Response.Cache.SetLastModified(ModifiedDate);
            context.Response.Cache.SetExpires(DateTime.Now.ToUniversalTime().AddDays(7));
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.Cache.SetMaxAge(new TimeSpan(7, 0, 0, 0));
            context.Response.Cache.SetRevalidation(HttpCacheRevalidation.None);
            context.Response.Cache.SetETag(ETag);

The code above sets this item's last modified date and says that it should expire in seven days. The item may be cached by anything really since it's set to public, also no revalidation is required (normally though, you'd probably want the item to revalidate). It also sets an ETag for the item. An ETag, by the way, is really just something to tell one object/entity from another (usually just a hash of something, like last date modified).

So when the response is sent to the client, this information is put into the header of the transmission, and the client knows it. However, clients might still send a request to the server every time it wants to use the information (depending on the browser, settings, etc.). So how can we check if the item has been updated on the server?

If-None-Match

Remember the ETag we gave the response previously? It gets sent back to us as If-None-Match in the header section (which can be reached by looking at context.Request.Headers["If-None-Match"]). If this equals the current ETag for the item on the server, then we know that it hasn't been changed since the last time the client was here. In this case, all we need to do is:

                context.Response.StatusCode = (int)HttpStatusCode.NotModified;
                context.Response.End();

Now normally speaking, this is usually enough. However some browsers don't set the If-None-Match item. Instead what they use is the item:

If-Modified-Since

Instead of sending the ETag, what they will do is send back the date the item was modified (or at least the last date that the client knows it was modified).  In this case, all you need to do is compare the date that the client sends and the last time the item was actually modified. If they are equal, nothing has changed. However if the item has been changed since the If-Modified-Since date, we need to send the new information. There is one thing to note though when comparing the dates, the If-Modified-Since date does not include anything lower than seconds. So if you take the last modified date of a file, they're not going to match up as that includes milliseconds. So you are going to have to round those off first.

Now this wont cache an item on every browser. They can always set it up so that the browser doesn't cache anything, in which case you're out of luck. But since not many people do that, you should be good to go. Anyway, try it out yourself, leave feedback, and happy coding.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.Net | Web Design
Posted by James Craig on Friday, June 27, 2008 12:47 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Lightbox Extender for ASP.Net


I'm certain that if you've looked around, you've seen the various lightbox implementations out there. Most of them are pretty good (in fact I use one on this site that you can find here). The one that I use at work is Lightbox 2 and I have to admit that it's rather slick, if a bit bloated since it uses prototype and script.aculo.us. Since we use both of those libraries anyway, not an issue really. Anyway, one of the annoying aspects of the script is the fact that you have to set the rel attribute in any/all images that will use the script (and if they're to be grouped together they need that set as well)... And since I really didn't want to do that, I created my own extender to do that work for me. 

LightboxExtender.zip (1.56 kb)

The extender takes a div/span/table/etc., takes all of the images found inside it, adds the appropriate rel attribute, groups them together, and adds a caption for the group. However if you want an individual caption for each image, you still have to set the title attribute separately for each image. The code is really basic and should be pretty easy to modify but download it, give it a try, leave feedback, etc. and happy coding.

Oh, and I'm still addicted to the Spore Creature Creator... I think partially it is due to the fact that my art skills are nonexistent, so the ability to create something is rather cool to me. At the same time I see the technology that they're utilizing as potentially a huge boon to the indie scene (and they've been really open about how they've done what they've done which is really nice). I mean the real-time motion retargeting alone has some interesting potential, not to mention the procedural music, etc.

Currently rated 3.0 by 1 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: AJAX | ASP.Net | Web Design
Posted by James Craig on Monday, June 23, 2008 10:26 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Spore Creature Creator


I'm certain that you've already heard about it, but the Spore Creature Creator is out... Thanks to it, I have no code to post today. In fact I have no idea if I'll have any code to post for the rest of the week... If the game play is even half as addicting as creating the creatures, I'm never going to get anything done again. I mean anything that allows me to create my very own Zoidberg is a good thing in my book. So go, download, create, and hopefully I'll have something for you next time you're here.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: General
Posted by James Craig on Wednesday, June 18, 2008 3:27 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Recent comments

None

Calendar

<<  January 2009  >>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Sponsors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009