com.github.ben-manes.caffeine
caffeine
2.9.0
顺便写了个工具类配合SpringBoot使用:
package com.ciih.refineinner.cache; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; import java.util.function.Function; / * 缓存之王 * * @author Lenovo */ @Component public class CaffeineService { private Cache
cache; public CaffeineService() { this.cache = Caffeine.newBuilder() .expireAfterWrite(15, TimeUnit.MINUTES) .maximumSize(100) .build(); } / * 存储K-V * * @param key * @param value * @return */ public String put(String key, String value) { cache.put(key, value); return key; } / * 获取K-V * * @param key * @return */ public String getIfPresent(String key) { return cache.getIfPresent(key); } public String get(String key, Function
function) { return cache.get(key, function); } public void remove(String key) { cache.invalidate(key); } }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/201785.html原文链接:https://javaforall.net
