小编Luk*_*son的帖子

使用Spring Cache Abstraction的异步缓存更新

使用Spring的缓存抽象,如何在仍然返回旧条目的同时异步刷新条目?

我正在尝试使用Spring的缓存抽象来创建一个缓存系统,在相对较短的"软"超时之后,缓存条目可以进行刷新.然后,在查询它们时,返回缓存的值,并启动异步更新操作以刷新条目.我也会

Guava的缓存构建器允许我指定缓存中的条目应在一定时间后刷新.然后可以使用异步实现覆盖缓存加载器的reload()方法,允许返回过时的缓存值,直到检索到新的缓存值.但是,spring缓存似乎不使用底层Guava缓存的CacheLoader

是否可以使用Spring的缓存抽象来进行这种异步缓存刷新?

编辑澄清:使用Guava的CacheBuilder,我可以使用refreshAfterWrite()来获取我想要的行为.例如来自Guava Caches解释:

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;
           }
         }
       });
Run Code Online (Sandbox Code Playgroud)

但是,我看不到使用Spring的@Cacheable抽象来获取refreshAfterWrite()行为的方法.

java spring caching asynchronous spring-cache

7
推荐指数
1
解决办法
8161
查看次数

标签 统计

asynchronous ×1

caching ×1

java ×1

spring ×1

spring-cache ×1