java - Asynchronous Cache Update With Spring Cache Abstraction -


using spring's caching abstraction, how can have cache refresh entry asynchronously while still returning old entry?

i trying use spring's caching abstraction create caching system after relatively short "soft" timeout, cache entries made eligible refresh. then, when queried, cached value returned, , asynchronous update operation started refresh entry.

guava's cache builder allows me specify entries in cache should refreshed after amount of time. reload() method of cache loader can overridden asynchronous implementation, allowing stale cached value returned until new 1 retrieved. however, spring caching seems not use cacheloader of underlying guava cache

is possible kind of asynchronous cache refresh using spring's caching abstraction?

edit clarify: guava's cachebuilder can use refreshafterwrite() behaviour want. e.g. guava caches explained:

loadingcache<key, graph> graphs = cachebuilder.newbuilder()    .maximumsize(1000)    .refreshafterwrite(1, timeunit.minutes)    .build(        new cacheloader<key, graph>() {          public graph load(key key) { // no checked exception            return getgraphfromdatabase(key);          }           public listenablefuture<graph> reload(final key key, graph prevgraph) {            if (neverneedsrefresh(key)) {              return futures.immediatefuture(prevgraph);            } else {              // asynchronous!              listenablefuturetask<graph> task = listenablefuturetask.create(new callable<graph>() {                public graph call() {                  return getgraphfromdatabase(key);                }              });              executor.execute(task);              return task;            }          }        }); 

however, cannot see way behaviour of refreshafterwrite() using spring's @cacheable abstraction.

perhaps try like:

1- configure cache:

@configuration @enablecaching public class cacheconfig {      @bean     public cachemanager cachemanager() {      simplecachemanager simplecachemanager = new simplecachemanager();      guavacache chache= new guavacache("cachekey", cachebuilder.newbuilder().build());      simplecachemanager.setcaches(arrays.aslist(cachekey));      return simplecachemanager;     }  } 

2- read value cached suppose string(i use @service example)

@service public class myservice{       @cacheable("cachekey")     public string getstringcache() {        return dosomething();     }      @cacheput("cachekey")     public string refreshstringcache() {        return dosomething();     }     ... } 

both getstringcache() , refreshstringcache() invoke same function in order retreive value cached. controller invoke only getstringcache().

3 - refresh cache scheduled tasks doc

@configuration @enablescheduling public class scheduledtasks {      @autowired     private myservice myservice;      @scheduled(fixeddelay = 30000)     public void iaasstatusrefresh(){        myservice.refreshstringcache();     }   } 

in way scheduled task forces cache refresh every 30s. accessed getstringcache() find updated data cache.


Comments