一、使用caffeine
1、引入依赖
<!--本地缓存caffeine-->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
2、配置类
@Configuration
public class CaffeineConfig {
@Bean
public Cache<String,Object> caffeineCache(){
return Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.SECONDS)
.initialCapacity(100)
.build();
}
}
4、测试
@GetMapping("/caffeine")
public String get2(){
Object name = caffeineCache.getIfPresent("name");
if (name == null){
log.info("cache没有");
caffeineCache.put("name","wgh");
log.info("cache 载入");
}else {
log.info("cache命中");
log.info(name.toString());
}
return null;
}
5、浏览器访问接口
6、日志