Spring Cache 是 Spring 框架提供的一个抽象缓存层,它允许开发者以声明式的方式将缓存功能集成到 Spring 应用程序中,从而提高应用程序的性能和响应速度。以下是关于 Spring Cache 的详细使用介绍。
1. 添加依赖
如果你使用的是 Maven 项目,在 pom.xml
中添加以下依赖:
<dependencies><!-- Spring Boot Starter Cache --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><!-- 可以根据需要选择具体的缓存实现,这里以 Caffeine 为例 --><dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId></dependency>
</dependencies>
2. 启用缓存
在 Spring Boot 主应用类上添加 @EnableCaching
注解来启用缓存功能:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
3. 配置缓存管理器
Spring Cache 支持多种缓存实现,如 Caffeine、Redis 等。这里以 Caffeine 为例进行配置:
import com.github.ben-manes.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 java.util.concurrent.TimeUnit;@Configuration
@EnableCaching
public class CacheConfig {@Beanpublic CacheManager cacheManager() {CaffeineCacheManager cacheManager = new CaffeineCacheManager();cacheManager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(100));return cacheManager;}
}
上述配置中,我们创建了一个 Caffeine 缓存管理器,设置了缓存项的过期时间为 10 分钟,最大缓存项数量为 100。
4. 使用缓存注解
Spring Cache 提供了多个注解来实现缓存的操作,以下是一些常用注解的使用示例:
@Cacheable
该注解用于标记一个方法的返回值应该被缓存。如果缓存中已经存在该方法的返回值,则直接从缓存中获取,否则执行方法并将结果存入缓存。
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class UserService {@Cacheable("users")public User getUserById(Long id) {// 模拟从数据库中查询用户信息System.out.println("Fetching user with id: " + id);return new User(id, "User" + id);}
}class User {private Long id;private String name;public User(Long id, String name) {this.id = id;this.name = name;}public Long getId() {return id;}public String getName() {return name;}
}
在上述示例中,getUserById
方法被 @Cacheable
注解标记,缓存名称为 users
。当多次调用该方法并传入相同的 id
时,只有第一次会执行方法体,后续调用会直接从缓存中获取结果。
@Cacheable(value = "setmealCache",key = "#categoryId") // key= setmealCache::categoryIdpublic Result<List<Setmeal>> querySetMealList(String categoryId){List<Setmeal> setmealList = setMealService.querySetMealList(categoryId);return Result.success(setmealList);}
querySetMealList方法根据传入的 categoryId 获取套餐列表。由于使用了 @Cacheable
注解,第一次调用该方法时,会执行方法体并将结果存入 “setmealCache” 缓存中,以 categoryId 作为键。后续再次调用该方法且传入相同的 categoryId 时,会直接从缓存中获取结果,而不会再次执行方法体。
@CachePut
该注解用于更新缓存中的数据。无论缓存中是否存在该方法的返回值,都会执行方法并将结果存入缓存。
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;@Service
public class UserService {@CachePut("users")public User updateUser(User user) {// 模拟更新数据库中的用户信息System.out.println("Updating user with id: " + user.getId());return user;}
}
在上述示例中,updateUser
方法被 @CachePut
注解标记,每次调用该方法时,都会执行方法体并将更新后的用户信息存入缓存。
@CacheEvict
该注解用于清除缓存中的数据。可以指定清除特定缓存项或整个缓存。
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;@Service
public class UserService {@CacheEvict("users")public void deleteUser(Long id) {// 模拟从数据库中删除用户信息System.out.println("Deleting user with id: " + id);}@CacheEvict(value = "users", allEntries = true)public void clearAllUsersCache() {// 清除所有用户缓存System.out.println("Clearing all users cache");}
}
在上述示例中,deleteUser
方法被 @CacheEvict
注解标记,调用该方法时会清除缓存中指定 id
的用户信息;clearAllUsersCache
方法使用 allEntries = true
表示清除整个 users
缓存。
@CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId") // key=setmealCache.categoryIdpublic Result<String> saveSetMeal(@RequestBody SetmealDTO setmealDTO) {setMealService.saveSetMeal(setmealDTO);return Result.success();}
@CacheEvict
注解
cacheNames
属性:指定要清除的缓存名称,这里是 “setmealCache”,意味着要清除该名称下的缓存项。
key
属性:使用 SpEL(Spring Expression Language)表达式 #setmealDTO.categoryId 来确定要清除的具体缓存项的键。#setmealDTO 引用了方法参数 setmealDTO,categoryId 是 setmealDTO 对象的一个属性,也就是说,会清除 setmealCache 缓存中键为 setmealDTO 对象的 categoryId 属性值的缓存项。
5. 测试缓存功能
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class Application implements CommandLineRunner {@Autowiredprivate UserService userService;public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Overridepublic void run(String... args) throws Exception {// 第一次调用,会执行方法体并将结果存入缓存User user1 = userService.getUserById(1L);System.out.println("User 1: " + user1.getName());// 第二次调用,会直接从缓存中获取结果User user2 = userService.getUserById(1L);System.out.println("User 2: " + user2.getName());// 更新用户信息并更新缓存User updatedUser = new User(1L, "UpdatedUser1");userService.updateUser(updatedUser);// 再次获取用户信息,会从更新后的缓存中获取User user3 = userService.getUserById(1L);System.out.println("User 3: " + user3.getName());// 删除用户信息并清除缓存userService.deleteUser(1L);// 再次获取用户信息,会再次执行方法体User user4 = userService.getUserById(1L);System.out.println("User 4: " + user4.getName());// 清除所有用户缓存userService.clearAllUsersCache();}
}
总结
通过以上步骤,你可以在 Spring 应用程序中使用 Spring Cache 实现缓存功能。使用缓存注解可以方便地对方法的返回值进行缓存、更新和清除操作,从而提高应用程序的性能和响应速度。