Expiring Javascript Structures – intro

What is self-expiring data structure?

To simply put, those are smart structures which have expire time for each element or whole structure. When you put an element into one such structure you can retrieve it only within specified time. If you put an object, specify 30 seconds expire time, and try to fetch that object from the structure after 60 second object shouldn't be there.

Let try to illustrate this on javascript example, lets say we have ExpiringMap data structure which has put method that accepts key under we want to store our object, object it self, and expire time

var map = new ExpiringMap();

//let put two objects with expire time of 5 seconds
map.put("foo",{someNumber:321} , 5);   
map.put("bar",{someString:"test string"}, 5);

//execute after 4 seconds
setInterval(function(){
    console.log(map.get("bar"));
    //prints object {someString:"test string"} to console
}, 4*1000);


//execute after 10 seconds
setInterval(function(){
    console.lof(map.get("bar"));
    //prints false to console
}, 10*1000);

If you ever tried to create self-expiring data structure in any programming language you must know how hard that is. Best thing i found so far in Java is expiring hash map from google guava project.

You can see my implementation of those structures on my Github Repo