简体   繁体   中英

How to Make guava cache value permanent

Is it possible, using Google Guava's Cache, to keep my cached value in the cache permanently?

Below is how I build my cache:

cache = CacheBuilder.newBuilder()               
                .expireAfterWrite(60, TimeUnit.MINUTES)
                .maximumSize(100)
                .build(....);

I want my cache to keep the value permanently (currently it's 60 minutes). Is there any method of doing so?

Just remove expireAfterWrite from builder (it's optional feature):

cache = CacheBuilder.newBuilder()
            .maximumSize(100)
            .build(....);

so that entries will be evicted only when maximumSize is reached.

Guava's Cache is well documented but you should probably read Wiki page too.

PS If by "permanent" you meant "will be there after restart", Guava Cache isn't for you since it's in-memory cache.

Simply change the value in builder:

cache = CacheBuilder.newBuilder()               
            .expireAfterWrite(Long.MAX_VALUE, TimeUnit.DAYS)
            .maximumSize(Long.MAX_VALUE)
            .build(....);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM