I realize there are similar questions/answers to this, but I can't seem to find exactly what I'm looking for.
I'm looking to implement an in-memory cache that caches the result of a call to the DB, which is doing an existence check. This existence check is quite expensive and once the object exists in the DB it will never be removed, so a very simple cache in-memory (in-process even) is all I need. Once the DB call is made the process remembers the result of the existence check for that ID and shouldn't call the DB again.
(Maybe another thing to mention is if the object doesn't exist in the DB, it will be created).
I'm using a HashSet (java) for this and just adding the ID to the set when the check/create is done, however this is a highly concurrent environment and I am unsure about the implications of HashSet's lack-of-threadsafeness.
The code only ever uses the add() and contains() methods (no iteration).
I don't really care about a cache miss (and a resulting extra DB call) here and there, but what I'm wondering is if this pattern of add() and contains() being called on the set in concurrent threads could lead to more catastrophic errors.