Java hash cache expire daily

I would like to have a HashMap<String, String> that expires the cache every day at midnight.

Please note that this J2EE solution allows multiple threads to access it.

What is the best way to implement it in Java?

+6
source share
4 answers

While other offers may work during expiration, it is important to note that:

Expiration hashmap can be done lazily

i.e. without any monitor threads!

The simplest way to implement the expiration is as follows:

1) Extend the hash map and create a local variable nextMidtime (Long), initialized in System.currentTime .... in the constructor. This will be set equal to the next midnight, in milliseconds ...

2) Add the following snippet to the first line of the "containsKey" and "get" methods (or any other methods that are responsible for ensuring that the data is not outdated) as follows:

 if (currentTime> nextMidTime) this.clear(); //Lets assume there is a getNextMidnight method which tells us the nearest midnight time after a given time stamp. nextMidTime=getNextMidnight(System.currentTimeInMilliseconds); 
+6
source

Do you want every item in the hash map to be cleared at midnight?

If I think so, I would create a new hash file and replace it with the original. Since the substitution is done in one step and nothing else is trying to make the same change, I believe that it should be thread safe.

Anything in the middle access will be allowed to complete its access. This suggests that nothing else refers to this hash map.

If the hasmap links are not combined in one place, then I think hashmap.clear() is the best bet if the hash map is synchronized.

If the hashmap is not synchronized, you can change it from only one thread, right? Ask this thread to call hashmap.clear() .

I think it covers all cases.

+4
source

You can also explore the use of Guava CacheBuilder , which allows you to create an individual instance of their Cache . Among other useful features, CacheBuilder allows you to set an end time, etc. For your own purposes.

EDIT: It should be noted that you set the expiration of records based either on the last access or the last record , so this sentence is not suitable for asking exactly what should be reset all at night. You can still consider design changes if using a third-party API is an option. Guava is a very smart IMHO library, not just for caching.

+3
source

java.util.Timer and java.util.TimerTask may be useful.

Create a TimerTask that can clear or recreate the HashMap and, through the Timer instance, organize its execution at midnight.

For instance:

 // Public single shared hash map. static Map<String, String> cached_map_ = new HashMap<String, String>(); public class Hash_map_resetter extends TimerTask { // Constructor takes reference to containing // Timer so it can reset itself after execution. Hash_map_resetter(Timer a_timer) { timer_ = a_timer; } // Clear cache. public void run() { cached_map_ = new HashMap<String, String>(); System.out.println("Cached cleared"); timer_.schedule(new Hash_map_resetter(timer_), 100); } // Reference to containing Timer. private final Timer timer_; } // Example initiation. Timer t = new Timer(); t.schedule(new Hash_map_resetter(t), 100); 

The example simply resets it every 100 milliseconds. There are many Timer.schedule() methods, some accept Date and let you specify midnight.

+2
source

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


All Articles