Poor's man cache
Poor's man cache
I looking for a recommened javascript method for a simple cache.
This for my new plugin ajax calls. I got it working as follows but I
would like to know if there is better way:
function myJQueryPluginFunction(..)
{
var cfg = {
...
cacheEnabled: true,
cacheMaxSize: 50, // 50 items for cache
...
}
...
var cache = {
length: 0;
};
// prior to ajax call, check the cache using tag id
var tagId = $(this).attr("id");
if (cfg.cacheEnabled && tagId && cache[tagId]) {
doShow(cache[tagId]);
} else {
// perform ajax
$.get(url,function(data) {
if (cfg.cacheEnabled && tagId) {
if (cache.length >= cfg.cacheMaxSize) {
// clear cache
cache = [];
cache.length = 0;
}
cache.length++;
cache[ tagId ] = data;
}
doShow(data);
});
}
}
Thanks
--
HLS