Java中CaffeineCache自定义缓存时间的实现
作者:csdn_freak_dd
1、POM文件依赖
<dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>3.1.0</version> </dependency>
2、声明缓存
@Slf4j public class CaffeineCacheUtils { /** * 声明缓存对象 */ private static final Cache<String, Object> CACHE = Caffeine.newBuilder() // 当key和value都没有引用时驱逐缓存 .weakKeys() .weakValues() // 最大容量 .maximumSize(10_000) // 固定存活时间24小时 .expireAfterWrite(2 * 60 * 60 * 1000L, TimeUnit.MILLISECONDS) .build() // 构建Caffeine缓存 ; }
3、缓存使用
/** * 添加缓存 * * @param key key值 * @param value value值 */ public static void put(String key, Object value) { CACHE.put(key, value); } /** * 获取缓存对象 * * @param key key值 * @return 返回value值 */ public static Object get(String key) { return CACHE.getIfPresent(key); }
4、测试缓存
@Test public void testCache() { CaffeineCacheUtils.put("key", "value"); Object value = CaffeineCacheUtils.get("key"); log.info("value={}", value); }
5、自定义缓存过期时间
expireAfterWrite是指定一个固定的过期时间,如果想要根据不同的key值自己指定过期时间,可以按照下面的方法来实现。
声明一个缓存value值holder对象
@Data public class CacheValueHolder implements Serializable { @Serial private static final long serialVersionUID = 1398783661313031605L; private Object value; private TimeUnit timeUnit; private long expire; }
创建缓存容器
/** * 声明缓存对象 */ private static final Cache<String, CacheValueHolder> CACHE = Caffeine.newBuilder() // 当key和value都没有引用时驱逐缓存 .weakKeys() .weakValues() // 最大容量 .maximumSize(10_000) .expireAfter(new Expiry<String, CacheValueHolder>() { @Override public long expireAfterCreate(String key, CacheValueHolder valueHolder, long currentTime) { log.info("创建缓存对象,key={},value={},currentTime={}", key, JSON.toJSONString(valueHolder), currentTime); return valueHolder.getTimeUnit().toNanos(valueHolder.getExpire()); } @Override public long expireAfterUpdate(String key, CacheValueHolder valueHolder, long currentTime, @NonNegative long currentDuration) { log.info("更新缓存对象,key={},value={},currentTime={},currentDuration={}", key, JSON.toJSONString(valueHolder), currentTime, currentDuration); return valueHolder.getTimeUnit().toNanos(valueHolder.getExpire()); } @Override public long expireAfterRead(String key, CacheValueHolder valueHolder, long currentTime, @NonNegative long currentDuration) { log.info("读取缓存对象,key={},value={},currentTime={},currentDuration={}", key, JSON.toJSONString(valueHolder), currentTime, currentDuration); return currentDuration; } }) // 如果缓存不存在,则同步查询事业部产品列表,同时加入缓存 .build() // 构建Caffeine缓存 ;
修改缓存使用方法
/** * 添加缓存 * * @param key key值 * @param value value值 * @param expire 过期时间 * @param timeUnit 时间类型 */ public static void put(String key, Object value, long expire, TimeUnit timeUnit) { CacheValueHolder valueHolder = new CacheValueHolder(); valueHolder.setValue(value); valueHolder.setExpire(expire); valueHolder.setTimeUnit(timeUnit); CACHE.put(key, valueHolder); } /** * 添加缓存 * * @param key key值 * @param value value值 */ public static void put(String key, Object value) { // 默认缓存存在时间为1000毫秒 put(key, value, 1000, TimeUnit.MILLISECONDS); } /** * 获取缓存对象 * * @param key key值 * @return 返回value值 */ public static Object get(String key) { CacheValueHolder valueHolder = CACHE.getIfPresent(key); if (Objects.isNull(valueHolder)) { return null; } return valueHolder.getValue(); }
6、测试自定义超时时间
@Test public void testCache() throws InterruptedException { CaffeineCacheUtils.put("key1", "value1", 1000L, TimeUnit.MILLISECONDS); CaffeineCacheUtils.put("key2", "value2", 2000L, TimeUnit.MILLISECONDS); Object value1 = CaffeineCacheUtils.get("key1"); Object value2 = CaffeineCacheUtils.get("key2"); log.info("value1={}", value1); log.info("value2={}", value2); TimeUnit.MILLISECONDS.sleep(1000L); Object value11 = CaffeineCacheUtils.get("key1"); Object value22 = CaffeineCacheUtils.get("key2"); log.info("value1={}", value11); log.info("value2={}", value22); }
方法运行后日志为:
23:44:21.398 [main] INFO com.summer.toolkit.util.CaffeineCacheUtils - 创建缓存对象,key=key1,value={"expire":1000,"timeUnit":"MILLISECONDS","value":"value1"},currentTime=8165196876700
23:44:21.401 [main] INFO com.summer.toolkit.util.CaffeineCacheUtils - 创建缓存对象,key=key2,value={"expire":2000,"timeUnit":"MILLISECONDS","value":"value2"},currentTime=8165240503500
23:44:21.401 [main] INFO com.summer.toolkit.util.CaffeineCacheUtils - 读取缓存对象,key=key1,value={"expire":1000,"timeUnit":"MILLISECONDS","value":"value1"},currentTime=8165240686100,currentDuration=956190600
23:44:21.403 [main] INFO com.summer.toolkit.util.CaffeineCacheUtils - 读取缓存对象,key=key2,value={"expire":2000,"timeUnit":"MILLISECONDS","value":"value2"},currentTime=8165242670700,currentDuration=1997832800
23:44:21.403 [main] INFO com.summer.toolkit.service.CaffeineCacheUtilsTest - value1=value1
23:44:21.403 [main] INFO com.summer.toolkit.service.CaffeineCacheUtilsTest - value2=value2
23:44:22.410 [main] INFO com.summer.toolkit.util.CaffeineCacheUtils - 读取缓存对象,key=key2,value={"expire":2000,"timeUnit":"MILLISECONDS","value":"value2"},currentTime=8166249659500,currentDuration=990844000
23:44:22.411 [main] INFO com.summer.toolkit.service.CaffeineCacheUtilsTest - value1=null
23:44:22.412 [main] INFO com.summer.toolkit.service.CaffeineCacheUtilsTest - value2=value2
可以看到同时放入缓存容器中的两个缓存,一个1000毫秒的过期时间,一个2000毫秒的过期时间,当刚放入时都可以获取到,等待1000毫秒后,只有第二个缓存可以获取到,第一个缓存对象已经过期获取不到,实现了用户自定义缓存对象过期时间。
到此这篇关于Java中CaffeineCache自定义缓存时间的实现的文章就介绍到这了,更多相关Java CaffeineCache缓存 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!