Skip to content

Instantly share code, notes, and snippets.

@andersonmat
Last active December 18, 2015 02:49
Show Gist options
  • Save andersonmat/5714059 to your computer and use it in GitHub Desktop.
Save andersonmat/5714059 to your computer and use it in GitHub Desktop.
Timed in-memory storage in JavaScript
/**
The MIT License (MIT)
Copyright (c) 2013 Matt Anderson (Question Everything, LLC)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
Create a CacheMap instance with a provided timeout value for any values
placed within the map.
A CacheMap created without a timeoutValue parameter will never time out
the value set unless a uniqueTimeout is passed.
*/
var CacheMap = function(timeOutValue) {
this.constructor(timeOutValue);
};
/**
Assign the timeout and create objects to store cached content
and timeout ids.
*/
CacheMap.prototype.constructor = function(timeOutValue) {
this.timeOutValue = timeOutValue;
this.timeoutMap = {};
this.storageMap = {};
};
/**
Set content in the CacheMap. If there is a pending timeout associated
with the provided key, it will be clearned, a new value set, and a
new timeout created.
If the uniqueTimeout parameter is passed, it will be used to timeout
the stored value rather than the default timeout value provided in the
constructor.
*/
CacheMap.prototype.set = function(key, value, uniqueTimeout) {
// check for timeout associated with key
if(this.timeoutMap[key])
clearTimeout(this.timeoutMap[key]);
// set value in local storage
this.storageMap[key] = value;
// here we check to see if: the global timeout is defined (and that it is not -1)
// or that we have a uniqueTimeout passed and that it is not -1.
if((this.timeOutValue && this.timeOutValue != -1) || // we have a global timeout
(uniqueTimeout && uniqueTimeout != -1)) { // OR we have a unique timeout
// set timeout & association
this.timeoutMap[key] =
setTimeout((function(key){
return function() {
delete this.timeoutMap[key];
delete this.storageMap[key];
}.bind(this);
}.bind(this))(key), uniqueTimeout || this.timeOutValue);
}
};
/**
Attempts to retrieve a value from the internal storage object.
If no value is present, undefined is returned, otherwise the
stored value will be returned.
*/
CacheMap.prototype.get = function(key) {
return this.storageMap[key];
};
var testObject = new CacheMap(); // create new cachemap with no global timeout
testObject.set("testKey1", "1"); // this value will never time out
testObject.set("testKey2", "2", 1500); // this will timeout in 1.5 seconds
testObject.set("testKey3", "3", 3000); // this will timeout in 3.0 seconds
setInterval(function(){
console.log("Test Key 1:", testObject.get("testKey1"));
console.log("Test Key 2:", testObject.get("testKey2"));
console.log("Test Key 3:", testObject.get("testKey3"));
}, 500);
/*
Example Output:
Test Key 1: 1
Test Key 2: 2
Test Key 3: 3
Test Key 1: 1
Test Key 2: 2
Test Key 3: 3
Test Key 1: 1
Test Key 2: undefined
Test Key 3: 3
Test Key 1: 1
Test Key 2: undefined
Test Key 3: 3
Test Key 1: 1
Test Key 2: undefined
Test Key 3: 3
Test Key 1: 1
Test Key 2: undefined
Test Key 3: undefined
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment