ASP 101 - Active Server Pages 101 - Web05
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
Migrating to ASP.NET
Getting Scripts to Run on a Schedule
The Top 10 ASP Links @ Microsoft.com

QUICK TIP:
Basic authentication vs. NT Challenge and Response
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


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


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers