Skip to content

Instantly share code, notes, and snippets.

@haxorjim
Created June 23, 2017 01:05
Show Gist options
  • Save haxorjim/d009a703877deda627bffbdc05df889d to your computer and use it in GitHub Desktop.
Save haxorjim/d009a703877deda627bffbdc05df889d to your computer and use it in GitHub Desktop.
jsCache
jsCache = {
expirationSeconds: 60,
cache: {},
cacheFunction: function (func, parameters) {
var value;
// generate the cache key using the function definition and parameters to be passed
var key = this.getCacheKey(func, parameters);
// check the cache for an existing value
if(this.cache[key]) {
// get the value
value = this.cache[key].value;
// reset it's expiration
this.cache[key].slidingExpiration = this.expirationSeconds;
console.log("Extended %s", key);
}
if (value)
{
console.log('Retrieved %s', key);
}
else
{
value = func.apply(null, parameters);
this.set(key, value);
console.log("Stored %s", key);
}
return value;
},
getCacheKey: function (func, parameters) {
return func + "(" + JSON.stringify(parameters) + ")";
},
get: function(key) {
return this.cache[key].value;
},
set: function(key, value) {
this.cache[key] = {
value: value,
dependencies: [], // not implemented
absoluteExpiration: null, // not implemented
slidingExpiration: this.expirationSeconds,
priority: 'normal', // not implemented
onRemoveCallback: null // not implemented
};
},
remove: function (key) {
delete this.cache[key];
console.log("Removed %s", key);
},
start: function() {
var self = this;
setInterval(function(){ return self.autoExpiration(); }, 1000);
},
autoExpiration: function () {
var expiredKeys = [];
var size = 0;
// tick down and mark as expired
for (var key in this.cache) {
this.cache[key].slidingExpiration--;
size++;
//console.log(this.cache[key].slidingExpiration);
if (this.cache[key].slidingExpiration == 0) {
console.log('%s expired.', key);
expiredKeys.push(key);
}
}
// remove the expired cache entries
for (var x = 0; x < expiredKeys.length; x++) {
this.remove(expiredKeys[x]);
}
console.log('%d objects in cache.', size);
// for debugging only
document.body.innerHTML = "";
for (var key in jsCache.cache) {
document.write('<b>' + key + '</b>: ' + jsCache.cache[key].value + "<br>");
}
}
};
// set it up
jsCache.start();
// objects and functions for testing
function example() { return new Date(); }
function noparams() { return 'test 1'; }
function params(one, two) { return one + two; }
var something = {
test3: function(var1) { return var1; }
};
// call some functions and cache the result
console.log(jsCache.cacheFunction(example));
console.log(jsCache.cacheFunction(noparams));
console.log(jsCache.cacheFunction(params, [1, 1]));
console.log(jsCache.cacheFunction(something.test3, ['this works too']));
// notice this retrieves the cached result now
console.log(jsCache.cacheFunction(example));
// manually add some items to the cache
jsCache.set('asdf', 1);
jsCache.set('qwer', { ffff: 123});
// read some values by key
console.log(jsCache.get('qwer'));
// remove some objects by key
jsCache.remove('asdf');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment