Wednesday, May 30th, 2007
Javascript LRU Cache
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.
Usage
-
-
var cache = new Cache();
-
-
cache.setItem("A", "1", {expirationAbsolute: null,
-
expirationSliding: 60,
-
priority: CachePriority.High,
-
callback: function(k, v) { alert('removed ' + k); }
-
});
-
-
cache.getItem("A");
-
-
cache.clear();
-












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!?
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!.
Well, there was a whatwg spec for client side storage…
http://www.whatwg.org/specs/web-apps/current-work/multipage/section-storage.html
too bad nobody supports it right now (xcept maybe mozilla)
“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”
uh… sorry about the spelling… that’s HTACCESS
Firefox 2 already implemented DOM Storage
http://developer.mozilla.org/en/docs/DOM:Storage
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.
Will the script work for caching between different browser sessions or it will work only under one browser session?
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.
I’m learning Javascript now, very important coding, my teacher told me.
my search before:human growth hormone
by by kdotiy on Tues. Apr. 22, 2008