Tache: a jQuery AJAX cache plugin

October 2008

There are lots of good AJAX cache plugins already in existence but, unable to find one that simply slotted straight into jQuery in a "native" fashion, I wrote my own.

In short, a caching plugin is used in a environment where lots of AJAX calls are made to
a) speed things up for the user, and/or
b) reduce load on the web server.

It does this by "storing" AJAX requests that have already been made into memory so no identical call is made twice from the same page instance. If you've made an AJAX call using jQuery before, I'm sure that you can type the four extra characters to make one using Tache.

Currently when doing a jQuery AJAX call, one does something like...

$.ajax({
  type: "POST",
  url: "some.php",
  data: "name=Jon&location=Cambridge",
  success: function(msg){
    alert( "Data Saved: " + msg );
  }
});

Using Tache, simply swap the $.ajax bit to be $.Tache.Get

$.Tache.Get({
  type: "POST",
  url: "some.php",
  data: "name=Jon&location=Cambridge",
  success: function(msg){
    alert( "Data Saved: " + msg );
  }
});

There, that wasn't so hard, was it? Whatever data you pass into a normal $.ajax() call you can pass into $.Tache.Get().

Tache has a 600 seconds timeout. That means any cached data over 10 minutes old gets discarded and a new call to the server will be made if it is requested. You can alter this value by editing the plugin itself or by changing it programmatically (see below). If you never want the cache to expire, just set the value to be ridiculously high.

To change the timeout: $.Tache.SetTimeout(3600); (would set it to be 3600 seconds; that's one hour to you and me). To wipe all caches: $.Tache.DeleteAll();.

Finally, to wipe a single cache:

$.Tache.Delete({
  type: "POST",
  url: "some.php",
  data: "name=Jon&location=Cambridge",
  success: function(msg){
    alert( "Data Saved: " + msg );
  }
});

You are welcome to download and use my plugin which is distributed under the MIT licence. If you're interested to see other jQuery work I've done, checkout JAWStats web analytics.

Jon Combe