I would like to have a HashMap<String, String>, that every day at midnight, the cache will expire.
Please note that it’s J2EE solution so multiple threads can access it.
What is the best way to implement it in Java?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Although the other suggestions can work to time the expiration, it is important to note that :
Expiration of the hashmap can be done lazily
i.e. without any monitor threads !
The simplest way to implement expiration is thus as follows :
1) Extend hash map, and create a local nextMidtime (Long) variable, initialized to System.currentTime….in the constructor. This will be set equal to the Next midnight time, in milliseconds…
2) Add the following snippet to the first line of the the “containsKey” and “get” methods (or any other methods which are responsible for ensuring that data is not stale) as follows :