I am working on a web-based medical application and need to create a small in-memory object cache. Here is my use-case.
We need to show list of requests submitted by people who need certain things (Blood, Kidney, etc.) and it's not going to be a huge list as in a given day request for blood or anything else will be a limited one. Please take into account that we do not want to use any caching API as it would be an overkill. The idea is to create a Map and place it in the ApplicationContext.
The moment a new request is being placed by any person, we will update that Map in the Application context and the moment the request expires, we will remove them from the Map. We need to look into the following points additionally.
- Need to set Max Element Limit.
- If Max Limit reached, we should removed entry which was added first.
- Take care of any Synchronized issues.
Please suggest what Data-structure should be used and what things to take care of while implementing this.
If you don't want to add third party libs you could implement one yourself on top of a LinkedHashMap. LinkedHashMap is exactly suitable to be used as a cache and you can configure a policy (remove least recently used or oldest entry) If you configure it accordingly it will remove the oldest entry as you need.
And for thread-safety you can always use Collections#synchronizedMap().
Here is a small example
I believe LinkedHashMap is exactly what you need. You just need to override removeEldestEntry(...) method, and it will automatically remove old entries for you if the maximum capacity is reached. Something like:
You could implement a more sophisticated logic, for example remove very old entries even if the max. capacity is not reached.
If synchronizing atomic map operations is enough for you, you can just wrap the map into Collections.synchronizedMap(...):
If you need more accurate synchronization, for example read the map and update it in one synchronized block, you need to synchronize (all) code blocks that work with the map yourself.
How about google's guava cache? It is pretty simple to implement and very easy to use:
Guava Cache
For the OmniFaces project, we had a similar requirement and there we settled for concurrentlinkedhashmap, with a small wrapper that tracks how long cached items are valid and purges them lazily from the cache whenever that item is requested.
Also see: How would you implement an LRU cache in Java?