SpringBoot 缓存注解:@Cacheable/@CacheEvict 使用

张开发
2026/4/6 11:07:36 15 分钟阅读

分享文章

SpringBoot 缓存注解:@Cacheable/@CacheEvict 使用
在 SpringBoot 里做缓存除了手动操作 RedisTem)plate更优雅、更常用的方式就是Spring 自带的缓存注解。不用写重复的缓存逻辑只需要在方法上加个注解就能自动实现缓存读写代码干净又好维护。本篇文章就来讲讲最核心的两个注解•Cacheable查询时自动缓存•CacheEvict更新/删除时自动清理缓存一、为什么要用缓存注解• 无侵入业务代码和缓存代码分离不污染逻辑• 极简开发一行注解替代一堆get/set缓存代码• 统一管理过期时间、缓存名称、key 规则集中配置• 适配多种缓存Redis、Caffeine、内存缓存都支持适合• 查询多、修改少的接口• 商品详情、用户信息、字典数据、配置列表• 不想写重复缓存模板代码的场景二、基础环境准备1. 引入依赖!-- Redis 缓存 -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-redis/artifactId /dependency2. 启动类开启缓存SpringBootApplication EnableCaching // 必须加 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }3. application.yml 缓存配置spring: cache: type: redis redis: time-to-live: 3600000 # 默认1小时过期 cache-null-values: false # 不缓存null三、核心注解 1Cacheable查询 缓存作用第一次执行方法 → 查库 → 结果存入缓存后续请求 → 直接走缓存不执行方法体1. 基础用法Cacheable(value userCache, key #userId) public User getUserById(Long userId) { return userMapper.selectById(userId); }•value / cacheNames缓存名区分不同业务•key缓存 key支持 SpEL 表达式最终 Redis keyuserCache::10012. 常用 key 写法// 单个参数 Cacheable(value user, key #id) // 对象参数取id Cacheable(value user, key #user.id) // 方法名当key Cacheable(value dict, key #root.methodName) // 组合key Cacheable(value order, key uid:#userId:type:#type)3. 条件缓存满足才缓存// 只缓存成年用户 Cacheable(value user, key #userId, condition #result.age 18)// 结果不为null才缓存 Cacheable(value user, key #userId, unless #result null)四、核心注解 2CacheEvict删除缓存作用数据更新/删除后清理旧缓存保证数据一致。1. 根据 key 删除CacheEvict(value userCache, key #user.id) public void updateUser(User user) { userMapper.updateById(user); }2. 删除整个缓存名下所有 keyCacheEvict(value userCache, allEntries true) public void refreshAllUser() { }3. 方法执行前删除CacheEvict(value user, key #userId, beforeInvocation true)五、另外两个常用注解1. CachePut强制更新缓存每次都会执行方法体适合实时更新缓存。CachePut(value user, key #user.id) public User updateUser(User user) { userMapper.updateById(user); return user; }2. Caching组合多个缓存操作Caching( evict { CacheEvict(value user, key #userId), CacheEvict(value userOrder, key #userId) } ) public void deleteUser(Long userId) { }六、模拟场景场景 1用户详情典型查询缓存Cacheable(value userInfo, key #userId, unless #result null) public User getUser(Long userId) { return userMapper.selectById(userId); }场景 2修改用户 → 清理缓存CacheEvict(value userInfo, key #user.id) public void updateUser(User user) { userMapper.updateById(user); }场景 3删除用户 → 清理缓存CacheEvict(value userInfo, key #userId) public void deleteUser(Long userId) { userMapper.deleteById(userId); }场景 4商品列表缓存Cacheable(value productList, key #categoryId) public ListProduct getProductList(Integer categoryId) { return productMapper.selectByCategory(categoryId); }场景 5批量刷新商品缓存CacheEvict(value productList, allEntries true) public void refreshProduct() { }场景 6字典/配置几乎不变长期缓存Cacheable(value dictCache, key #dictType) public ListDict getDict(String dictType) { return dictMapper.selectByType(dictType); }七、注意事项1.同类方法内调用注解失效因为走了代理同类内部方法调用不经过 AOP。解决抽取到独立 Service 或自己注入自己。2.缓存 key 冲突不同业务一定要用不同value/cacheNames。3.缓存与数据库不一致增删改必须配合CacheEvict或CachePut。4.大数据量列表缓存列表缓存容易占内存建议设置更短过期时间 分页缓存。5.null 值被缓存用unless #result null避免。6.事务与缓存顺序问题建议事务提交后再清缓存否则会出现“脏缓存”。八、Cacheable vs CachePut vs CacheEvict 总结注解作用执行方法体典型场景Cacheable查 缓存缓存不存在才执行查询接口CachePut强制更新缓存每次都执行实时同步CacheEvict删除缓存每次都执行增删改九、总结SpringBoot 缓存注解是后端最实用的简化技巧之一• 查询用Cacheable• 更新删除用CacheEvict• 实时同步用CachePut你在项目里更喜欢用注解还是手动操作 Redis有没有遇到过缓存不一致、注解不生效的坑欢迎在评论区留言交流关注我

更多文章