I have a java class that is being used by multiple people (from a UI).

In the class i have a static HashMap with the userKey as the map key and an arraylist with data pertaining to the user as the value. I am using this kind of like a cache. Because populating the list everytime the user logs in is a very expensive procedure.

Whenever a user logs in and modifies the data i am updating the list for that user in the map.

Now the issue is that I donot want to keep the entries in the HashMap forever. I need to clear the entries for users that have not been logged in for a while to be removed from the hashmap, but I do not want to clear the whole map.

I tried experimenting with WeakHashMap, but it didnt seem to work well with String keys.

Does anyone have any idea on what can be done in such a situation?

Any help is appreciated.


3 Years
Discussion Span
Last Post by aravind326

I was looking for something like an automated garbage collection similar to that in WeakHashMap.

If I have to do a scheduled cleanup, then I need to add more information into the map as to which entries has been recently used and which are not. Which would make stuff more complicated.



I think CacheBuilder would be perfect for my requirement.

Tried a sample class and it seems to work fine.

Now need to integrate with my app. Fingers crossed.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.