0

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

3
Contributors
4
Replies
28
Views
3 Years
Discussion Span
Last Post by aravind326
Featured Replies
  • 1
    ~s.o.s~ 2,560   3 Years Ago

    The simplest (in terms of effort) solution would be to use the `CacheBuilder` class [provided by Guava (also known as Google collections)](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheBuilder.html) Read More

0

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.

0

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.

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.