I am managing a project in Java that stores user data. Users can be online, or offline. When users are online, their data is loaded into the data object for easy accessibility, and offloaded when they log off.
However, for offline users, in the interests of preventing data loss from concurrent access of data from multiple commands manipulating the data at the same time, I store a weakhashmap as a cache of the loaded user data objects. Should anything need to access an offline data object to modify it, the system will check the cache first before loading it from file.
The only thing that I can think of how to store them is by a string key, which represents the users username. But because of how java works, that does not always seem to work due to the VM's string caching system.
Originally I thought of using a string wrapper, but again because of how hashmaps work (by use of hashcode), creating a new string wrapper would not get me the value i need, and if i stored the string wrappers, that would defeat the purpose by storing a strong reference to the key at all times (preventing the removal of the key from the weakhashmap).
Maybe I'm just not understanding how the weakhashmap is supposed to be used :S If this isn't how the weakhashmap is supposed to be used, I'm open to accept other ideas of how to do what I wish.