Hashtable Timeout Mechanism

I have a hash table that is under heavy traffic. I want to add a timeout mechanism to hashtable, delete too old entries. My problems, - It should be easy - Eliminating the operation is not critical time. I mean (the timeout value is 1 hour), the delete operation can be performed after 1 hour or 1 hour 15 minutes. No problems.

My opinion is this: I create a large array (as a ring buffer) that stores the time and key of the hash table. When adding to the hash table using the array index, find the next time interval, if the array slot is empty, enter the insert time and HT -key, if the array slot is not empty, compare the insert time for the timeout.
   if a timeout occurs, remove it from the Hashtable (if it has not yet been deleted); this is not a timeout, the increment index before searching for a slot for a slot or a slot with a timeout. When deleting from a hash table, there is no operation with a large array.

Soon, for each add operation to the Hashtable, you can remove one timeout from the hash table or do nothing.

What is your more elegant and lightweight solution?

Thanks for the help,

+3
source share
4
+5

Guava MapMaker:

ConcurrentMap<String, MyValue> graphs = new MapMaker()
   .maximumSize(100)
   .expireAfterWrite(1, TimeUnit.HOURS)
   .makeComputingMap(
       new Function<String, MyValue>() {
         public MyValue apply(String string) {
           return calculateMyValue(string);
         }
       });

, , , , . ( , ).

, Map, make*().

+9

, LRUMap Apache. , , , , . ( , , - :))

0

Assuming that the currently most heavily accessible items in your cache structure are in a significant minority, you can go randomly by selecting items to delete (you have a low chance of deleting something very useful). I used this technique, and in this particular application, it worked very well and did not carry out any implementation efforts.

0
source

Source: https://habr.com/ru/post/1786135/


All Articles