SpringBoot整合Caffeine本地缓存
1、@Cacheable相关注解
1.1 相关依赖
如果要使用@Cacheable
注解,需要引入相关依赖,并在任一配置类文件上添加@EnableCaching
注解
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
1.2 常用注解
- @Cacheable:表示该方法支持缓存。当调用被注解的方法时,如果对应的键已经存在缓存,则不再执行方法体,而从缓存中直接返回。当方法返回null时,将不进行缓存操作。
- @CachePut:表示执行该方法后,其值将作为最新结果更新到缓存中,每次都会执行该方法。
- @CacheEvict:表示执行该方法后,将触发缓存清除操作。
- @Caching:用于组合前三个注解,例如:
@Caching(cacheable = @Cacheable("CacheConstants.GET_USER"), evict = {@CacheEvict("CacheConstants.GET_DYNAMIC",allEntries = true)} public User find(Integer id) { return null; }
1.3 常用注解属性
- cacheNames/value:缓存组件的名字,即cacheManager中缓存的名称。
- key:缓存数据时使用的key。默认使用方法参数值,也可以使用SpEL表达式进行编写。
- keyGenerator:和key二选一使用。
- cacheManager:指定使用的缓存管理器。
- condition:在方法执行开始前检查,在符合condition的情况下,进行缓存
- unless:在方法执行完成后检查,在符合unless的情况下,不进行缓存
- sync:是否使用同步模式。若使用同步模式,在多个线程同时对一个key进行load时,其他线程将被阻塞。
1.4 缓存同步模式
sync开启或关闭,在Cache和LoadingCache中的表现是不一致的:
- Cache中,sync表示是否需要所有线程同步等待
- LoadingCache中,sync表示在读取不存在/已驱逐的key时,是否执行被注解方法
2、实战
2.1 引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency>
2.2 缓存常量CacheConstants
创建缓存常量类,把公共的常量提取一层,复用,这里也可以通过配置文件加载这些数据,例如@ConfigurationProperties
和@Value
public class CacheConstants { /** * 默认过期时间(配置类中我使用的时间单位是秒,所以这里如 3*60 为3分钟) */ public static final int DEFAULT_EXPIRES = 3 * 60; public static final int EXPIRES_5_MIN = 5 * 60; public static final int EXPIRES_10_MIN = 10 * 60; public static final String GET_USER = "GET:USER"; public static final String GET_DYNAMIC = "GET:DYNAMIC"; }
2.3 缓存配置类CacheConfig
package com.plus.config;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.util.concurrent.TimeUnit;
/**
* @program: plus
* @ClassName CaffeineCacheConfig
* @description: 本地缓存配置类
* @author: 黄涛
* @create: 2023-11-09 15:14
* @Version 1.0
**/
@Configuration
@EnableCaching
public class CaffeineCacheConfig {
/**
* cache名称
* 提示:@Primary,用于标识一个Bean(组件)是首选的候选者。当有多个同类型的Bean(组件)时,使用了@Primary注解的Bean将会成为默认选择,
* 如果没有其他限定符(如@Qualifier)指定具体要使用的Bean,则会优先选择带有@Primary注解的Bean。
* @return
*/
@Primary
@Bean("defaultCacheManager")
public CacheManager cacheManagerOne(){
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
//Caffeine配置
Caffeine<Object, Object> caffeine = Caffeine.newBuilder()
//最后一次写入后经过固定时间过期
.expireAfterWrite(10, TimeUnit.SECONDS)
//maximumSize=[long]: 缓存的最大条数
.maximumSize(1000);
cacheManager.setCaffeine(caffeine);
return cacheManager;
}
/**
* cache名称
* 缓存一些零散数据,根据缓存时间不同可能需要配置不同的bean方法
* @return
*/
@Bean("cacheManagertwo")
public CacheManager cacheManageTwo(){
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
//Caffeine配置
Caffeine<Object, Object> caffeine = Caffeine.newBuilder()
//最后一次写入后经过固定时间过期
.expireAfterWrite(30, TimeUnit.SECONDS)
//maximumSize=[long]: 缓存的最大条数
.maximumSize(50);
cacheManager.setCaffeine(caffeine);
return cacheManager;
}
}
//配置说明
//initialCapacity=[integer]: 初始的缓存空间大小
//maximumSize=[long]: 缓存的最大条数
//maximumWeight=[long]: 缓存的最大权重
//expireAfterAccess=[duration]: 最后一次写入或访问后经过固定时间过期
//expireAfterWrite=[duration]: 最后一次写入后经过固定时间过期
//refreshAfterWrite=[duration]: 创建缓存或者最近一次更新缓存后经过固定的时间间隔,刷新缓存
//weakKeys: 打开key的弱引用
//weakValues:打开value的弱引用
//softValues:打开value的软引用
//recordStats:开发统计功能 注意:
//expireAfterWrite和expireAfterAccess同事存在时,以expireAfterWrite为准。
//maximumSize和maximumWeight不可以同时使用
//weakValues和softValues不可以同时使用
//使用
//直接再接口上配置注解接口,示例如下:
//@Cacheable(cacheNames = "user",key = "#dto.userId")
//Cacheable: @Cacheble注解表示这个方法有了缓存的功能,方法的返回值会被缓存下来,下一次调用该方法前,会去检查是否缓存中已经有值,如果有就直接返回,不调用方法
//@CacheEvict(cacheNames = "user",key = "#dto.userId")
//@CacheEvict: @CacheEvict注解的方法,会清空指定缓存。一般用在更新或者删除的方法上。key必须和查询的一样清缓存才能生效。
//@CachePut(cacheNames = "user",key = "#dto.userId")
//@CachePut : @CachePut注解的方法,保证方法被调用,又希望结果被缓存。会把方法的返回值put到缓存里面缓存起来。它通常用在新增方法上。key必须和查询的一样清缓存才能生效。
2.4 调用缓存
这里要注意的是Cache和@Transactional一样也使用了代理,类内调用将失效
package com.plus.controller;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import com.plus.common.R;
import com.plus.utils.Func;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.plus.entity.User;
import com.plus.dto.UserDTO;
import com.plus.service.IUserService;
/**
* 用户表 控制器
*
* @author ht
*/
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/user")
@Api(value = "/api/user", tags = "用户表相关接口")
public class UserController {
@Autowired
private IUserService userService;
@Autowired
@Qualifier(value = "cacheManagertwo")
private CacheManager cacheManager;
/**
* 验证cache添加
*/
@GetMapping("/testCacheAdd")
public R testCacheAdd() {
Cache cache = cacheManager.getCache("cacheManagertwo");
Map<String,Object> map = new HashMap<>();
map.put("id",1);
map.put("name","test");
map.put("age",30);
cache.put("user",map);
return R.data(map);
}
/**
* 验证cache获取
*/
@GetMapping("/testCacheGet")
public R testCacheGet() {
Cache cache = cacheManager.getCache("cacheManagertwo");
Map<String,Object> map = cache.get("user",Map.class);
return R.data(map);
}
/**
* value:缓存key的前缀。
* key:缓存key的后缀。
* sync:设置如果缓存过期是不是只放一个请求去请求数据库,其他请求阻塞,默认是false(根据个人需求)。
* unless:不缓存空值,这里不使用,会报错
* 查询用户信息类
* 如果需要加自定义字符串,需要用单引号
* 如果查询为null,也会被缓存
*/
@Cacheable(value = CacheConstants.GET_USER,key = "'user'+#userId",sync = true)
@CacheEvict
public UserEntity getUserByUserId(Integer userId){
UserEntity userEntity = userMapper.findById(userId);
System.out.println("查询了数据库");
return userEntity;
}
/**
* 分页 用户表
* Cacheable: @Cacheble注解表示这个方法有了缓存的功能,方法的返回值会被缓存下来,下一次调用该方法前,会去检查是否缓存中已经有值,如果有就直接返回,不调用方法
*/
@Cacheable(cacheNames = "userpage",key = "#keyId")
@GetMapping("/page")
@ApiOperation(value = "分页", notes = "传入user")
public R<IPage<User>> page(UserDTO dto,String keyId) {
IPage<User> pages = userService.page(dto);
return R.data(pages);
}
/**
* 不分页 用户表
* Cacheable: @Cacheble注解表示这个方法有了缓存的功能,方法的返回值会被缓存下来,下一次调用该方法前,会去检查是否缓存中已经有值,如果有就直接返回,不调用方法
*
* 注意key是必传的,否则会报错
*
* http://localhost:8899/api/user/page?keyId=2
*/
@Cacheable(cacheNames = "user",key = "#keyId")
@GetMapping("/list")
@ApiOperation(value = "不分页", notes = "传入user")
public R<List<User>> list(UserDTO dto,String keyId) {
List<User> list = userService.list(dto);
return R.data(list);
}
/**
* 修改 用户表
*
* @CacheEvict: @CacheEvict注解的方法,会清空指定缓存。一般用在更新或者删除的方法上。
*
* key必须和查询的一样清缓存才能生效
*
*/
@CacheEvict(cacheNames = "user",key = "#keyId")
@GetMapping("/update")
@ApiOperation(value = "修改", notes = "传入user")
public R update(UserDTO dto,String keyId) {
return R.data(userService.updateById(dto));
}
/**
* 新增 用户表
* @CachePut : @CachePut注解的方法,保证方法被调用,又希望结果被缓存。会把方法的返回值put到缓存里面缓存起来。它通常用在新增方法上。
* key必须和查询的一样清缓存才能生效
*
* http://localhost:8899/api/user/save?userName=%E5%85%AD1%E8%80%81%E5%B8%88&keyId=2
*/
@CachePut(cacheNames = "user",key = "#keyId")
@GetMapping("/save")
@ApiOperation(value = "新增", notes = "传入user")
public R save(UserDTO dto,String keyId) {
return R.data(userService.save(dto));
}
/**
* 删除 用户表
* @CachePut : @CachePut注解的方法,保证方法被调用,又希望结果被缓存。会把方法的返回值put到缓存里面缓存起来。它通常用在新增方法上。
*/
@CacheEvict(cacheNames = "user",key = "#keyId")
@DeleteMapping("/remove")
@ApiOperation(value = "逻辑删除", notes = "传入ids")
public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids,String keyId) {
return R.data(userService.deleteLogic(Func.toIntList(ids)));
}
/**
* 详情
*/
@PostMapping("/detail")
@ApiOperation(value = "详情", notes = "传入user")
public R<User> detail(UserDTO dto) {
User detail = userService.getOne(dto);
return R.data(detail);
}
}
3、驱逐策略
驱逐策略在创建缓存的时候进行指定。常用的有基于容量的驱逐和基于时间的驱逐。
基于容量的驱逐需要指定缓存容量的最大值,当缓存容量达到最大时,Caffeine将使用LRU策略对缓存进行淘汰;基于时间的驱逐策略如字面意思,可以设置在最后访问/写入一个缓存经过指定时间后,自动进行淘汰。
驱逐策略可以组合使用,任意驱逐策略生效后,该缓存条目即被驱逐。
- LRU 最近最少使用,淘汰最长时间没有被使用的页面。
- LFU 最不经常使用,淘汰一段时间内使用次数最少的页面
- FIFO 先进先出
Caffeine有4种缓存淘汰设置
- 大小 (LFU算法进行淘汰)
- 权重 (大小与权重 只能二选一)
- 时间
- 引用 (不常用,本文不介绍)
@Slf4j public class CacheTest { /** * 缓存大小淘汰 */ @Test public void maximumSizeTest() throws InterruptedException { Cache<Integer, Integer> cache = Caffeine.newBuilder() //超过10个后会使用W-TinyLFU算法进行淘汰 .maximumSize(10) .evictionListener((key, val, removalCause) -> { log.info("淘汰缓存:key:{} val:{}", key, val); }) .build(); for (int i = 1; i < 20; i++) { cache.put(i, i); } Thread.sleep(500);//缓存淘汰是异步的 // 打印还没被淘汰的缓存 System.out.println(cache.asMap()); } /** * 权重淘汰 */ @Test public void maximumWeightTest() throws InterruptedException { Cache<Integer, Integer> cache = Caffeine.newBuilder() //限制总权重,若所有缓存的权重加起来>总权重就会淘汰权重小的缓存 .maximumWeight(100) .weigher((Weigher<Integer, Integer>) (key, value) -> key) .evictionListener((key, val, removalCause) -> { log.info("淘汰缓存:key:{} val:{}", key, val); }) .build(); //总权重其实是=所有缓存的权重加起来 int maximumWeight = 0; for (int i = 1; i < 20; i++) { cache.put(i, i); maximumWeight += i; } System.out.println("总权重=" + maximumWeight); Thread.sleep(500);//缓存淘汰是异步的 // 打印还没被淘汰的缓存 System.out.println(cache.asMap()); } /** * 访问后到期(每次访问都会重置时间,也就是说如果一直被访问就不会被淘汰) */ @Test public void expireAfterAccessTest() throws InterruptedException { Cache<Integer, Integer> cache = Caffeine.newBuilder() .expireAfterAccess(1, TimeUnit.SECONDS) //可以指定调度程序来及时删除过期缓存项,而不是等待Caffeine触发定期维护 //若不设置scheduler,则缓存会在下一次调用get的时候才会被动删除 .scheduler(Scheduler.systemScheduler()) .evictionListener((key, val, removalCause) -> { log.info("淘汰缓存:key:{} val:{}", key, val); }) .build(); cache.put(1, 2); System.out.println(cache.getIfPresent(1)); Thread.sleep(3000); System.out.println(cache.getIfPresent(1));//null } /** * 写入后到期 */ @Test public void expireAfterWriteTest() throws InterruptedException { Cache<Integer, Integer> cache = Caffeine.newBuilder() .expireAfterWrite(1, TimeUnit.SECONDS) //可以指定调度程序来及时删除过期缓存项,而不是等待Caffeine触发定期维护 //若不设置scheduler,则缓存会在下一次调用get的时候才会被动删除 .scheduler(Scheduler.systemScheduler()) .evictionListener((key, val, removalCause) -> { log.info("淘汰缓存:key:{} val:{}", key, val); }) .build(); cache.put(1, 2); Thread.sleep(3000); System.out.println(cache.getIfPresent(1));//null } }
4、刷新机制
refreshAfterWrite()
表示x秒后自动刷新缓存的策略可以配合淘汰策略使用,注意的是刷新机制只支持LoadingCache和AsyncLoadingCache
private static int NUM = 0; @Test public void refreshAfterWriteTest() throws InterruptedException { LoadingCache<Integer, Integer> cache = Caffeine.newBuilder() .refreshAfterWrite(1, TimeUnit.SECONDS) //模拟获取数据,每次获取就自增1 .build(integer -> ++NUM); //获取ID=1的值,由于缓存里还没有,所以会自动放入缓存 System.out.println(cache.get(1));// 1 // 延迟2秒后,理论上自动刷新缓存后取到的值是2 // 但其实不是,值还是1,因为refreshAfterWrite并不是设置了n秒后重新获取就会自动刷新 // 而是x秒后&&第二次调用getIfPresent的时候才会被动刷新 Thread.sleep(2000); System.out.println(cache.getIfPresent(1));// 1 //此时才会刷新缓存,而第一次拿到的还是旧值 System.out.println(cache.getIfPresent(1));// 2 }
5、统计
LoadingCache<String, String> cache = Caffeine.newBuilder() //创建缓存或者最近一次更新缓存后经过指定时间间隔,刷新缓存;refreshAfterWrite仅支持LoadingCache .refreshAfterWrite(1, TimeUnit.SECONDS) .expireAfterWrite(1, TimeUnit.SECONDS) .expireAfterAccess(1, TimeUnit.SECONDS) .maximumSize(10) //开启记录缓存命中率等信息 .recordStats() //根据key查询数据库里面的值 .build(key -> { Thread.sleep(1000); return new Date().toString(); }); cache.put("1", "shawn"); cache.get("1"); /* * hitCount :命中的次数 * missCount:未命中次数 * requestCount:请求次数 * hitRate:命中率 * missRate:丢失率 * loadSuccessCount:成功加载新值的次数 * loadExceptionCount:失败加载新值的次数 * totalLoadCount:总条数 * loadExceptionRate:失败加载新值的比率 * totalLoadTime:全部加载时间 * evictionCount:丢失的条数 */ System.out.println(cache.stats());
6、总结
上述一些策略在创建时都可以进行自由组合,一般情况下有两种方法
- 设置
maxSize
、refreshAfterWrite
,不设置expireAfterWrite/expireAfterAccess
,设置expireAfterWrite
当缓存过期时会同步加锁获取缓存,所以设置expireAfterWrite
时性能较好,但是某些时候会取旧数据,适合允许取到旧数据的场景 - 设置
maxSize
、expireAfterWrite/expireAfterAccess
,不设置 refreshAfterWrite 数据一致性好,不会获取到旧数据,但是性能没那么好(对比起来),适合获取数据时不耗时的场景
吾乃代码搬运工,侵联删
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库