Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a periodically refreshing Cache in Java

My use case is to maintain an in-memory cache over the data stored in a persistent DB.

I use the data to populate a list/map of entries on the UI. At any given time, the data displayed on the UI should be as updated as it is possible (well this can be done by the refresh frequency of the cache).

Major difference between a regular cache implementation and this particular cache is that it needs a bulk refresh of all the elements at regular intervals and hence is pretty different from an LRU kind of cache.

I need to do this implementation in Java and it will be great if there are any existing frameworks which can be utilized to get this built around them.

I have explored the Google Guava cache library but it is more suited to a per entry refresh rather than a bulk refresh. There are no simple APIs which do a refresh on the whole cache.

Any help will be highly appreciated.

Also, if it is possible to incrementally do the refresh, it shall be great because the only limitation which arises while refreshing the whole cache is that if the cache is very big in size, then the memory heap should be atleast twice the size of the cache in order to load the new entries and replace the old map with the new one. If the cache is incremental or there is a chunked refresh (refresh in equal sizes) it will be great.

like image 659
Abhishek Jain Avatar asked Oct 05 '12 13:10

Abhishek Jain


People also ask

How do I refresh the Java cache?

In the Java Control Panel, under the General tab, click Settings under the Temporary Internet Files section. The Temporary Files Settings dialog box appears. Click Delete Files on the Temporary Files Settings dialog. The Delete Files and Applications dialog box appears.

What is caching and how can you implement your cache in Java?

The Java Object Cache provides caching for expensive or frequently used Java objects when the application servers use a Java program to supply their content. Cached Java objects can contain generated pages or can provide support objects within the program to assist in creating new content.

How do you implement caching?

So the standard way to implement cache is to have a data structure, using which we can access value by a given key in constant time. Now all good, we can save key value pairs in memory and retrieve it whenever we need it.


1 Answers

EHCache is a pretty full-featured java caching library. i would imagine they have something which would work for you.

In order to do an incremental reload of a cache (which would work on most any cache), just iterate through the currently loaded entries and force refresh them. (you could run this task on a background scheduler).

As an alternative to forcing the entire cache to reload, EHCache has the ability to specify a "time-to-live" for an entry, so entries will automatically be reloaded if they are too stale.

like image 57
jtahlborn Avatar answered Sep 18 '22 15:09

jtahlborn