Managing Caching Options with ASP

Friday, August 31, 2012

tags ASP

When it comes to the approach for caching your web page content, there really is not a straight forward answer regarding how you should be managing content caching by browsers and/or proxy servers. This all really depends on what type of content you have and what your goals and objectives are with regard to your content.

Browser Caching

If you take a moment to look at the default options for any typical modern web browser, such as Internet Explorer, you will notice some type of client-side “cache” setting. This settings allows you to control how the browser should handle the web pages you request. For example, you may configure the browser to check for newer versions of the target web page every time you visit the site, versus always using a cached copy of the page. Caching is especially helpful in a scenario where the user navigates via the use of their back button for pages that they have just finished looking at. Caching large images is also helpful so that the same static images you host are not downloaded by regular visitors every time they visit your page.

Proxy Caching

Web Proxies are very common on corporate networks. They work on the same principle as the browser cache, but handling a much larger scope. Proxies can serve hundreds, thousands, or even tens of thousands of users on a network. Proxies can have web traffic routed to or through them, depending how the network administrators have configured the networking services. Since you have so many users browsing web sites through proxies, the content that is cached is served back to the users without having to visit the web site multiple times. This technique reduces latency and network traffic.

Controlling Caching with ASP

While there are several methods that can be used to manipulate the cache settings, this article covers some of the methods that you can use with ASP.

ASP Expires Property

The Expires property sets how long, in minutes, a page will be cached before it expires. If a user agent returns to the page before it expires, the cached version is displayed.

Response.Expires = [minutes]

If you want to instruct the user agent not to cache the page, then assign a -1 to the Expires property.

Response.Expires = -1

ASP ExpiresAbsolute Property

You can also use the ExpiresAbsolute property to set a specific date and time when a cached page on a browser will expire. If a user returns to the page before this date/time, the cached version is displayed.

Response.ExpiresAbsolute = #January 10,2012 12:00:00#

ASP CacheControl Property

The HTTP 1.1 specification introduced the Cache-control response header. It gives web publishers more control over their content, and to address the limitations of the Expires property. Here is a listing of the various options you have with regard to the Cache-control response header.

  • max-age=[seconds]
    This specifies the maximum amount of time that the content will be considered fresh, relative to the time of the request in seconds.
  • s-maxage=[seconds]
    Similar to max-age, but specific to proxy caches.
  • public
    This marks the content as cacheable.
  • private
    This marks the content as cacheable to one user, not shared proxy caches.
  • no-cache
    This setting forces the cache to request validation from the server prior to using the cached copy.
  • no-store
    This setting instructs caches not to keep a copy of the content under any condition.
  • must-revalidate
    This setting instructs the cache to obey freshness information about the content, i.e. strictly follow the rules
  • proxy-revalidate
    This setting is similar to must-revalidate, but applies to proxy caches.
Response.CacheControl = "Public"

Keep in mind that one approach may not work for all of the different user agents and/or proxies between your web server and the end users. In your ASP code, it is customary to implement more than one solution. For example, if you do not want for your pages to be cached, your ASP page may include some of the following code.

Response.Expires = -1
Response.CacheControl = "no-cache"
<!DOCTYPE html>
  <title>My Title</title>
  <!-- HTML Content -->

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

Sams Teach Yourself Active Server Pages 3.0 in 21 Days ASP In A Nutshell Beginning ASP Databases