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.
- <!--#include file="cache.asp"-->
- set c = new Cache
- c.interval = "h"
- c.intervalValue = 1
- c.name = "sales"
- sales = c.getItem("carolina")
- if sales = "" then
- sales = getSales()
- response.write("Sales: " & sales)
- c.store "carolina", sales
- response.write("Sales from cache: " & sales)
- end if
- 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.
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…