1 CacheIt

1.1 What CacheIt does

CacheIt helps you cache things.

For example, suppose you're using Snoopy to retreive some webpages, do something with them, and then return the results. If you access the same page repeatedly, it would be nice to use a cache instead of retriving the webpage again, don't you think? (As of 2003-02-20 Snoopy did not implement it's own caching.)

1.2 Why use CacheIt

CacheIt is probably better than what you would write quickly from scratch, but probably not as good as File cache class. Neither, however, is it as complicated.

1.3 How you use CacheIt

1.3.1 How to set up CacheIt for use

There are two classes: Cachable and CacheIt.

Cachable is a dummy class - it serves as an interface definition. You don't have to subclass Cachable - you could just write your own class that implemented a method called get which takes a key as an argument and returns a value.

key is an indication of what object it is that is being cached. For example, if we're caching web pages, you might want to have the key be a URL.

value is the object to be cached. For example, it might be the text of the web page, or a structure containing the data returned as well as the headers returned by the request.

Instantiate your subclass of Cachable (or the class that implements the get method, as defined above). Let's call this theCachable.

Instantiate an instance of CacheIt with the following three parameters:

  • theCachable

  • the path to the directory the cached data should be stored in (the directory name must end in a "/")

  • the number of seconds an object can be around before a new copy should be refreshed

1.3.2 How to use CacheIt after it has been set up

Simply invoke the get method of the instance of CacheIt. If the data does not exist in the cache yet (or the file it exists in is too old), the get method you provided in theCachable will be used to retrieve the value based on the key and the value and key will be stored in a file whose name is a function of the key. If the data does exist in a file that isn't too old, the file will be opened and the value will be read.

In either case, the value will be returned, and that's all you (the user of CacheIt) should care about.

1.4 What else you should know/do

Eventually I'll implement support for conserving disk space by proactively deleting cached objects that are too old. In the meantime, you can write a cron job that goes through the directory and if there are any files older than that number of seconds they can be deleted. I won't provide such a thing here as I'm certain different people have different preferred ways of writing such a script. If you feel such a thing is necessary, let me know.

The file names are, as of 2003-02-20, the md5_hex hash of the serialize of the data (key and value). In the rare case where the hashes happen to be the same, the file will not be over-written. Instead, whichever one wrote to the file gets to use it (until it gets too old), and the other one just continues to get the value directly.

1.5 What else you can do

Enjoy CacheIt and let me know if you find this useful (covered under the LGPL).

2 Where you can get CacheIt

The source code can be downloaded http://www.andrewsw.com/text/CacheIt.class.inc. If you're prefer to view it in your browser first, you can view it http://www.andrewsw.com/text/CacheIt.class.inc.txt

3 Other places of information about CacheIt

Creative Commons License
This work is licensed under a Creative Commons License.