Cache object for classic ASP

By | November 18

As I have written my own framework for building web based applications with classic ASP I thought of posting some classes, components, controls, etc. on here. The complete framework is not publicly available (yet). Nevertheless I want to publish some fragments of it here if they are somehow standalone, so its not a real mission to use them without the full framework.
Lately I needed to write a RSS component which reads all kind of RSS Feeds, writes its own and so on. What’s more is that I thought it would be nice to cache some RSS Feeds for all users on the server. Especially the ones which arent updated so often. Thats a nice thing because normally we would send a request to the server where the feed is located and this may take some while (depending on the network) whereas with the cache we could get it directly from our server if it has not expired yet.

For this I thought about a caching mechanism which uses the application variables because they are shared through all visitors. I’ve created an own object for the caching which allows you to store everything you want to. For instance you could cache a whole HTML table if the generation takes so long but the data ist quite static and changes rarely. So you gonna ask how to use this class. Please first take a look at the Cache documentation (links at the bottom). After you have briefly looked at it you will recognize that it is pretty straightforward. Here is an example of how to use it and after it some comments about the code.

  1. <!--#include file="cache.asp"-->
  2. set c = new Cache
  3. c.interval = "h"
  4. c.intervalValue = 1
  5. c.name = "sales"
  6.  
  7. sales = c.getItem("carolina")
  8. if sales = "" then
  9.   sales = getSales()
  10.   response.write("Sales: " & sales)
  11.   c.store "carolina", sales
  12. else
  13.   response.write("Sales from cache: " & sales)
  14. end if
  15. set c = nothing

First we reference the cache with the include. After that the cache instance is being set up till line 5. It defines that the items will be valid for 1 hour and the cache is called “sales”. The name is important to identify the cache so it nows which items are stored in which container. This lets us to get the cached items on the next request and even to use this cache on other pages too. In this example the cache is named “sales” because we want to cache different sales for different regions. So you see that you are able to create more cache containers for different things. E.g. RSS, Sales, etc. Each one of them could go into his own container with seperately defined settings. Thats good because you might want to cache the sales longer than some RSS feeds.

However, after you have setup the instance you should first try to get the value from the cache with getItem(). If you get some value back then just print it out (line 13) otherwise get it from e.g. the database (in this example i assume a function called getSales) print it out and store it afterwards (lines 9-11) so it can be taken from the cache the next time.

That was it! Simple isn’t it? Indeed it is simple and enough for everyday usage when you deal with expensive queries. Currently I am using it for my RSS Reader (will post that later) where i transform the RSS with XSL and store the whole HTML within my cache. This saves me the repetetive requests to the RSS and the transformations as well.

Download the class here: Cache class (v0.1) - Documentation

Some more interesting things you should know about it:

  • items can be also arrays, dictionaries, etc. but not your own types
  • it has a protection against too much memory usage. Thats done by the maxSlots property which defines the maximum amount of items (when it exceeds the slots then “first in first” out approach takes place unless there is at least one item which is already expired) for the cache and a limitation of the item size (maxItemsize property). When the item exceeds the given max size then it wont be stored.
  • I’ve copied the class straight from the framework so when you experience some lib.error call then just check the code and read the exception message.
  • if you play around with it you may need the clear() method in order to reset the cache when you have some crap stored in it

Have fun with it and let me know what you think about it. If there will be any updates I will update the version here as well.

I have found another solution for caching in ASP. Its a different solution from the 4guysfromrolla. Check it out as well maybe it fits your requirements more:

NOTE: Cache class is part of the ajaxed library now!
check ajaxed library for a newer version…

3 comments on “Cache object for classic ASP

  1. Looks great!

    I’ve always been a “trust-no-one, code it yourself” kind of developer, which has generally served me well, and I had built up quite a library of classes, subs, and functions over the 5 years that I was an active ASP developer. I say “was” because apart from developing some advertising functionality for a family member’s site I haven’t done any serious coding since (I’ve since become more of a systems analyst, working on the requirements analysis and higher level design of software projects).

    However, I’m about to start coding myself a custom site for my photography portfolio featuring web 2.0 / RIA functionality & presentation, and I’m rather looking forward to getting stuck into some code again! And now that a plethora of robust client-side JS libraries exist (Prototype, script., ExtJS and YUI have impressed me in particular) I thought I’d use them instead of roll my own.

    Similarly, I’ve been pleased to find that I’m not the only one still using ASP Classic, and your Cache class will come in very handy indeed - at present I plan to use it to cache RSS feeds that my portfolio will generate (rather than consume).

    I’ll look forward to seeing what ASP more tidbits you post in future (Any idea if/when you might post your complete framework?) and I’ll almost certainly be posting some of my own once I’ve resurrected my old developer blog and got cracking on the work itself - it’s yet to get started at this point, still scribbling out UI concepts and functionality requirements at this stage…

    Anyway, thanks very much! And long live ASP Classic! :)

  2. Marcus thanks for you comment. I have had not much time to blog in the past but new things will come .. for example i have a ready-to-use RSS class which i wanna to post … i dont think that i’ll post the whole framework cause it requires too much documentation and its not ready for being published …but the most important components will be posted and updated regularly. thanks for passing by!
    yeah long live classicAsp :)

  3. Since I use Option explicit I had to add this to your class

    class Cache
    ‘Added Declarations
    private c,expires,packedItem,cachedItem