How to automatically refresh Cache using Google Guava?

前端 未结 5 1540
滥情空心
滥情空心 2020-12-23 20:39

I am using Google Guava library for caching. For automatic cache refresh we can do as follows:

cache = CacheBuilder.newBuilder()               
                      


        
5条回答
  •  渐次进展
    2020-12-23 20:56

    JAVA 8 version with parallel stream:

    Executors
            .newSingleThreadScheduledExecutor()
            .scheduleWithFixedDelay(() -> configurationCache
                    .asMap()
                    .keySet()
                    .parallelStream()
                    .forEach((key) -> configurationCache.refresh(key)),
                0,
                1, TimeUnit.SECONDS);
    

提交回复
热议问题