0
votes

I am using Spring framework and Java CXF services.

My project requires cache to be refreshed after every 15 minutes from the database. Pulling the data from database takes one minute on an average. So, while this one minute operation( cache is refreshing from db), if I try to access cache, it should return previously stored value.

The timeline goes ->

0 minute: Application starts. A trigger to cache refresh.

0.5 minute: Cache is being loaded by querying database

1 minute: Cache loaded (eg: xyz)

5 minute: Cache accessed and xyz returned

15 minute: Cache should refresh (new cache value will be xyw at 16 minute)

15.5 minute: Cache is being loaded by querying database. At this point if cache is accessed, it should return the previous cached value as new cache is now in inconsistent state. (should return xyz)

16 minute: Cache reloaded. If accessed now, xyq should be returned.

I have two questions:

  1. Right now I am having a spring task scheduler to do cache refresh every 15 minutes. Can I do with ehcache itself ?
    My ehcache.xml looks like:
<ehcache>
    <cache name="mycache" 
        maxElementsInMemory="3000"
        eternal="false" 
        timeToIdleSeconds="60000"  
        timeToLiveSeconds="60000"  
        overflowToDisk="true"  
        diskPersistent="false" />
        <persistence strategy="localTempSwap" synchronousWrites="true" />
    </cache>  
</ehcache>
  1. How can I achieve how this cache behaves as 15.5 minute described ?
1

1 Answers

0
votes

Answers:

  1. Ehcache has a scheduled refresh decorator which uses the Quartz scheduler. See this documentation on how to use it.
  2. Ehcache will not offer this behaviour unless you make the refresh process transactional. Otherwise you will hit whatever value is mapped to the key at the moment you get it.