My question is somewhat related to Caching, Suppose some threads are accessing my information every 2 sec, and it is getting updated in the database after every 3 minutes, So it is better for me to make a hashmap of the information that is fetched by those threads and update it after every 3 minutes and all threads are getting the data from hashmap. My question is “How will I make sure that exactly after 3 minutes my hashmap will be updated by some other thread, and all the time thread should get the latest information”.
My question is somewhat related to Caching, Suppose some threads are accessing my information
Share
You can use logic like this to access the data:
1) Acquire the lock that protects the data.
2) Check when was the last time the data was updated.
3) If the data is current enough, copy the data you need, release the lock, and stop.
4) Update the data.
5) Update the timestamp that is checked in step 2.
6) Copy out the new data.
7) Release the lock.
Alternatively, you can have a dedicated thread just to update the data as needed.