Created
October 25, 2011 17:55
-
-
Save nekman/1313651 to your computer and use it in GitHub Desktop.
Simple javascript key/value cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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