Spring Cache实现简化缓存功能开发

发布于:2025-08-31 ⋅ 阅读:(23) ⋅ 点赞:(0)

一. 介绍

Spring Cache 是 Spring 框架提供的缓存抽象层,它简化了在应用中添加缓存功能的开发工作。通过 Spring Cache,开发者无需关注具体缓存实现的细节,只需通过注解就能快速实现方法级别的缓存管理。

核心特点

1. 与具体缓存实现解耦:

支持多种缓存技术(如 Caffeine、EhCache、Redis 等),可通过配置灵活切换

2. 注解驱动:

通过简单的注解即可实现缓存的增、删、改、查操作

3. 支持多种缓存操作

包括缓存数据存储、读取、更新和清除

4. 事务感知:

能与 Spring 事务结合,确保缓存与数据的一致性

核心注解说明

注解 作用 关键属性
@Cacheable 缓存方法结果,优先从缓存读取 key:缓存键;condition:条件缓存
@CachePut 执行方法后更新缓存(方法必执行) key:缓存键
@CacheEvict 从缓存中删除数据 key:删除指定键;allEntries=true:删除所有
@Caching 组合多个缓存注解(如同时更新和删除) 包含 cacheableputevict 等属性
@CacheConfig 类级别配置缓存名称、键生成器等 cacheNames:默认缓存名称

二. 使用步骤

1.导入maven坐标

使用的缓存中间件为redis

<!-- Spring Cache 核心依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<!-- Redis 缓存实现(可选,根据需要替换) -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2. 在Spring启动类上添加@EnableCaching注解开启缓存功能

@SpringBootApplication
@Slf4j
@EnableCaching//开启缓存注解功能
public class SkyApplication {
    public static void main(String[] args) {
        SpringApplication.run(SkyApplication.class, args);
        log.info("server started");
    }
}

3. 在业务方法上使用缓存注解

@Service
@CacheConfig(cacheNames = "users") // 类级别统一指定缓存名称
public class UserService {

    // 1. 查询用户:缓存结果,键为用户 ID
    @Cacheable(key = "#id") // 缓存键 = id,如 "users::1"
    public User getUserById(Long id) {
        // 实际业务:查询数据库(仅第一次执行,后续从缓存获取)
        System.out.println("查询数据库,用户 ID:" + id);
        return userRepository.findById(id).orElse(null);
    }

    // 2. 更新用户:更新缓存(方法始终执行,结果存入缓存)
    @CachePut(key = "#user.id") // 缓存键 = user.id
    public User updateUser(User user) {
        // 实际业务:更新数据库
        System.out.println("更新数据库,用户 ID:" + user.getId());
        return userRepository.save(user);
    }

    // 3. 删除用户:清除缓存
    @CacheEvict(key = "#id") // 清除键 = id 的缓存
    public void deleteUser(Long id) {
        // 实际业务:删除数据库记录
        System.out.println("删除数据库,用户 ID:" + id);
        userRepository.deleteById(id);
    }

    // 4. 清除所有缓存(如批量删除场景)
    @CacheEvict(allEntries = true) // 清除 "users" 缓存中所有数据
    public void clearAllUsersCache() {
        // 无需业务逻辑,仅用于清除缓存
    }
}

@Cacheable的执行逻辑:每次调用有这个注解的方法时,先去redis缓存中查询数据,如果有,直接将缓存中的数据返回,不执行方法;如果没有,执行方法,调用数据库,将方法的返回值存入redis中,这样一来,下次执行方法就不用查询数据库,提高了效率

@CacheEvict:在修改员工信息,批量删除员工,启用停用员工等场景下,会使用allEntries = true(清空全部缓存)

现在有一个场景:一个公司内有几个部门,部门下分别有各自的员工,redis中以部门id为键存储员工信息,为什么删除或者新增一个用户就要将员工所在的部门的缓存清空呢,原因是这样的增删操作并不频繁,而精确在redis中操作比较麻烦,且在删除或者新增时,redis中的缓存已经和数据库中的数据不一致,所以需要清空缓存,下次查询操作时又将新的数据缓存到redis


网站公告

今日签到

点亮在社区的每一天
去签到