Wednesday, May 30th, 2007

Javascript LRU Cache

Category: JavaScript, Library

Monsur Hossain has created a simple Javascript LRU Cache:

It is loosely based on ASP.NET’s Cache, and includes many caching options such as absolute expiration, sliding expiration, cache priority, and a callback function. It can be used to cache data locally in the user’s browser, saving a server roundtrip in AJAX heavy applications.



  1. var cache = new Cache();
  3. cache.setItem("A", "1", {expirationAbsolute: null,  
  4.                           expirationSliding: 60,  
  5.                           priority: CachePriority.High,  
  6.                           callback: function(k, v) { alert('removed ' + k); }  
  7. });  
  9. cache.getItem("A");  
  11. cache.clear();

Posted by Dion Almaer at 7:49 am

3.3 rating from 41 votes


Comments feed TrackBack URI

I hope jQuery bundles this with their library. I can think of countless uses of this. Our users have to download a 2MB ajax application every time they visit our website. It would be much faster if we could cache 90% of our application in the browser cache using this LRU Cache so that the next time the user visits our website, they only need to download a small 10K file! Who says ajax has to be slow!?

Comment by Jordan — May 30, 2007

i think this wouldn`t help you,since it use the memory instead of some persistent storage.
this is just a wrapper around a JavaScript object that is used as a hash table.
the thing it add is the concept of expiration of objects(with alot of options).

really nice work!.

Comment by uriel katz — May 30, 2007

Well, there was a whatwg spec for client side storage…

too bad nobody supports it right now (xcept maybe mozilla)

Comment by Vasili Sviridov — May 30, 2007

“Our users have to download a 2MB ajax application every time they visit our website.”

Why don’t you use your server’s HTACESS file to cache the app on the user side? For Linux/Apache, with the mod_expires module…

### activate mod_expires
ExpiresActive On
### Expire images and javascript 1 month from when they’re accessed
ExpiresByType image/gif “access plus 4 weeks”
ExpiresByType text/javascript “access plus 4 weeks”
ExpiresByType image/jpg “access plus 4 weeks”
ExpiresByType text/css “access plus 4 weeks”

Comment by Bob — May 30, 2007

uh… sorry about the spelling… that’s HTACCESS

Comment by Bob — May 30, 2007

Firefox 2 already implemented DOM Storage

Comment by José Jeria — May 30, 2007

Nifty. Good timing too, I’ve been using an object as a hash cache, this’ll mean I can put logical timeouts and not chew too much browser memory.

Comment by Dan F — May 30, 2007

Will the script work for caching between different browser sessions or it will work only under one browser session?

Comment by Charlie Cheng — May 30, 2007

It currently works for one browser session. However it could probably be modified to work across browser sessions with a little help from the various javascript persistence frameworks out there.

Comment by Monsur — May 30, 2007

Leave a comment

You must be logged in to post a comment.