-
Notifications
You must be signed in to change notification settings - Fork 2
ExpiringMap
ExpiringMap is used when we want to store self-expiring objects.
First we initialize object holder
var map = new ExpiringMap();
Then we put two objects in it:
map.put("foo",{someNumber:321});
map.put("bar",{someString:"test string"});
and if within next 60 seconds we try to get key foo: map.get("foo");
//returns {someNumber:321}
if we wait longer than 60 seconds (default expire time)
map.get("foo");
//returns false
You can also specify default object timeout on init: var map = new ExpiringMap({defaultTimeout:20}); //in seconds
Or you can specify timeout for each key individually: map.put("foo",{someNumber:321}, 600); //expire key foo after 600 seconds
Also you can define callback for each key
map.put("foo",{someNumber:321}, 30, function(key, data){
console.log("key "+key+" has expired, data was:");
console.log(data);
});
Callback will be executed when we call get method for this key again after expiration time map.get("foo");
//this will trigger callback on key foo
Or when we call getKeyList after expiration time map.getKeyList();
//this will trigger callbacks on all expired keys
| Method summary | |
| boolean | put(key, object, timeout) |
| Object/boolean | get(key) |
| boolean | remove(key) |
| Array | getKeyList() |
| Object | getKeyInfo(key) |
| Number | getAccessed(key) |
| Object/boolean | getWithMetadata(key) |
getWithMetadata
- key - name under this object will be stored
- object - object to store
- timeout - [optional] expiration timeout (in seconds), if not provided default timeout will be used
- callback - [optional] on expire callback function returns true on successful input or false if key already exists
- key - name under object is stored
returns stored object or false if key doesn't exists
- key - key to be removed
returns true if successfully removed or false if object has already expired or it didn't existed in the map
returns array of currently active keys, or empty array if there is no active keys left
- key - name of object
returns object with information about stored object
for expired object it will return: { expired: true, info: null }
for active object if will return: { expired: false, info: { createdTimestamp: 1299118422871, endTime: 1299118482871, modifiedTimestamp: 1299118422871, timeout: 60 } }
where:
- createdTimestamp - timestamp when this key is created
- endTime - timestamp after which object under this key will expire
- modifiedTimestamp - last accessed timestamp
- timeout - key timeout in seconds
- key - name under object is stored
returns last accessed timestamp for this key
- key - name under object is stored
returns stored object along with meta data