Now: Tutorial for Web and Software Design > Web Design > ASP > Web Design Content
> A Simple Method for Caching HTTP Requests [Bookmark it]
A Simple Method for Caching HTTP Requests

A Simple Method for Caching HTTP Requests

by John Peterson

Introduction

A few years ago I wrote an article entitled Server-Side Caching Options which discussed a few different approaches to utilizing caching to speed up server-side processing. In that article I discuss element-level caching. This article will present a script that illustrates this type of caching with the source of the data being a file on a remote server.

Here's a quick explanation as to why I'm back on this topic:

After Jupitermedia Corporation (the parent company of internet.com and hence ASP 101) acquired DevX, I was asked to place a feed of some of the recent DevX .NET articles on our site. Along with the request, I was sent some php code to use. Well that obviously wasn't going to happen... so I whipped up some ASP code on my own. I naturally included some caching so as not to hammer their server or slow down ours. (Truth be told... I actually just copied and modified a script I've been using for years, but I did write it at some point and it always included the caching.) A couple days ago I received a follow up request asking me to implement some form of caching! Apparently their original script included no caching at all and they had asked so many people to pull in the feed that their server wasn't able to keep up.

With the advent of web services and all the behind the scenes requests that are processed these days, I figured I should share the simple little script we've been using so sucessfully.

The Code

The code basically issues an HTTP request to the URL specified, retrieves the response, HTML encodes it (just to give an example of some sort of modification), and saves it to an Application-level variable. When it does this it sets a second variable with the current time and date. On future calls to the page this timestamp is compared to the current time to determine if enough time has passed so as to require a cache refresh. If so the cache is refreshed first, but either way the data is served from the cache.

As it stands, the script really only requires two basic things (above and beyond a server to run on): the WinHttp object (ships with the current versions of Windows) and that your web be configured as an application so that it has access to the Application object. Please note that sessions are a totally different thing. You can easily have sessions disabled and still use application-scoped objects.

If you have issues getting the WinHttp object to work, try using the proxycfg.exe utility. I didn't have any problem on my WinXP box so I think the default is now set to no proxy, but in previous versions you sometimes needed to run proxycfg.exe even to tell WinHttp you weren't using a proxy.

So without any more rambling on my part... here it is:

httpcaching.asp



<%

' I use an application var in order to cache the content as well as

' to store the timestamp when the cache was last updated.

If DateDiff("h", Application("HttpDataUpdated"), Now()) >= 2 _

  Or Request.QueryString("force") <> "" Then



  ' Declare our vars

  Dim objWinHttp

  Dim strHttpData



  ' Create our HTTP object... using 5.1 from WinXP Sp1

  Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")



  ' Send our request

  objWinHttp.Open "GET", "http://www.asp101.com/samples/httpsamp.asp"

  objWinHttp.Send



  ' If the response status indicates success then we update the cache

  If objWinHttp.Status = "200" Then

    ' Get the text retreived from the URL

    strHttpData = objWinHttp.ResponseText



    ' If you need to do anything to the text received, do it here

    ' before you cache it.  That way you can cache the data as

    ' you want it to be displayed.

    strHttpData = Server.HTMLEncode(strHttpData)



    ' Lock the Application object and set our values

    Application.Lock

    Application("HttpDataContent") = strHttpData

    Application("HttpDataUpdated") = Now()

    Application.UnLock

  End If



  ' Kill our HTTP object

  Set objWinHttp = Nothing

End If



' Now whenever you need to disply the information we retrieved you

' can just print out the value of Application("HttpDataContent")

Response.Write Application("HttpDataContent")

%>

The version in the zip file is a little longer, has more comments, and has some links to illustrate manually refreshing the cache, but the bulk of it is the same as the listing above.

Download the Code

You can dowload the code from here: httpcaching.zip (1.5 KB)

I recently upgraded to XP and it appears my zip tool of choice (PKZip CLI) is no longer available. I created this using WinXP's built-in zip support. If you have any issues with it, please let me know.

Related Content

  • Server-Side Caching Options
  • Building a (Not So) Simple Page Caching System
  • Why Use Time-Based Page Level Caching?
[Bookmark][Print] [Close][To Top]
  • Prev Article-Web Design:

  • Next Article-Web Design:
  • Related Materias
    Microsoft Expression Web D
    An Overview of the New Cac
    Storing Data from Dynamic 
    Multi-User Login With Data
    ASP 101s Year in Review - 
    Effective Data Paging Usin
    The Value of .NET Server C
    ASP 101s Year in Review - 
    Server-Side Printing to a 
    ASP.NET 2.0 Compilation Mo
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial