I want to cache objects in memory. The requirements are as follows:
Security is also a concern here because we are going to cache some sensitive data. This data will be cached in memory. Should I really worry about the security and encrypt the data?
I am looking for a Java class which provides a similar functionality.
Currently I am thinking of extending WeakHashMap
, and implementing various private/public methods to adhere to the requirements.
If you have any other idea please share here.
You don't want to use WeakHashMap
. SoftHashMap
would be closer but is not available in the standard library. If I were you I'll look at Guava's cache classes for hints.
But here are some additional thoughts:
Should scale down when JVM is running out of memory(kind of weak reference).
You mean soft reference
. But anyways, this requirement smells a bit to me. It can be a valid requirement I'd admit, but it's rare that you really need this. If the size of your records can be reasonably well predicted and if you are planning to have a hard limit on number of records you want to cache, chances are that you don't need this complexity.
Third party library shouldn't be use.
Everybody mentioned this, and they're right.
On the security aspect, efficacy of encrypting data cached in-memory is dubious. You'll have to have the encryption key in-memory as well. I bet there are many things to worry more than attackers reading your memory content.