I want to implement a lightweight caching data structure in Java, in code. I was primarily thinking about implementing something from scratch with HashMap
, but thought there might be available solutions.
The cache will be between the application and the database layer, and I will probably update it every 5
minutes. The data are all in JSON format, containing series of events with timestamp
. The expiration rule is that I want the cache to only include events whose timestamp
are within say [-10min, +10min]
from current time of insertions, not the time of insertion (I might insert multiple data at the same time to cache, however their timestamp might be different). So basically everytime I grabbed some data from database, I will need to do some JSON processing and insert/update the cache. It can be up to a few thousand JSON files so my scale is not very large.
What would be the best way to implement this cache? What data structure would you recommend to use?
UPDATE: Few things to note:
Data Structure: The data events I grabbed from database are sorted in time (hundreds of timed event streams per each call). So my cache would be actually a time window of events. It keeps events of
[-10min, +10min]
of current time at insertion (every 5 minutes).Insertion: As the data events I grabbed from database are sorted in time. So I'm not sure if I should erase the whole cache and insert my new event streams, or pass the events to the cache and it will take care of updating itself given the timestamps.
Retrieval: To do a retrieval, now I need to retrieve the series of events falling into a
[start_time, end_time]
interval which is a subset of my cache data (so these values are less than10 minutes
). So consider that when suggesting the right data structure.