Hi,

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.

Thanks,
Aravind

Why not just a scheduled clean-up that uses map.remove(key) to remove unwanted entries?

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.

Thanks.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.