Skip to content

Instantly share code, notes, and snippets.

@nekman
Created October 25, 2011 17:55
Show Gist options
  • Save nekman/1313651 to your computer and use it in GitHub Desktop.
Save nekman/1313651 to your computer and use it in GitHub Desktop.
Simple javascript key/value cache
/*
cache.set(<key>, <value>)
cache.set({ <key1> : <value1>, <keyN> : <valueN> });
cache.get(<key>, <optional|defaultvalue>) => <value>
Example:
cache.set({ foo : 'bar' });
cache.get('foo'); //'bar'
*/
var cache = (function () {
var _internalCache = {};
return {
get: function (key, defaultValue) {
return _internalCache[key] || defaultValue;
},
set: function (key, value) {
if (typeof key === 'object') {
for (var k in key) {
_internalCache[k] = key[k];
}
return;
}
key && (_internalCache[key] = value);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment