- 简介Spring
Cache是Spring框架提供的一整套缓存解决方案,它不是具体的缓存实现,而是提供了接口、代码规范、配置和注解等,用于整合各种缓存方案如Caffeine、Guava
Cache、Ehcache、Redis等。通过使用Spring
Cache,可以显著提高应用程序的性能,因为它允许将常用数据缓存在内存中,避免了重复的数据库查询或计算,从而减少了响应时间,提高了系统吞吐量。
核心注解
对于缓存声明,Spring的缓存抽象提供了一组Java注解:
@Cacheable: 触发缓存储存
@CacheEvict: 触发缓存回收
@CachePut: 更新缓存,而不会影响方法的执行
@Caching: 将多个缓存操作组合起来应用于一个方法
@CacheConfig: 在类级共享一些常见的缓存相关设置
基本使用开启缓存功能
@EnableCaching
@SpringBootApplication
public class SpringbootCacheApplication {
}
通过@EnableCaching开启缓存功能,也就是上面的注解才能生效。在需要缓存的方法上添加相应注解
@Cacheable(cacheNames = "person")
public Person save(Person person) {
}
更新缓存@CachePut(cacheNames = "person", key="#person.id")
public Person update(Person person){
}
清除缓存
@CacheEvict(cacheNames = "person", key = "#id")
public void deleteById(Long id){
}
指定多个注解
当你需要指定相同类型的多个注解(例如@CacheEvict或@CachePut)。例如,因为条件或键表达式在不同的缓存之间是不同的。
@Caching允许在同一方法上使用多个嵌套的@Cacheable、@CachePut和@CacheEvict。以下是使用两个@CacheEvict注解:
@Caching(evict = {
@CacheEvict(cacheNames = "primary", key = "#name"),
@CacheEvict(cacheNames = "secondary", key = "#id")
})
public void delete(String name, String id) {
// 从用户存储库中删除用户
userRepository.deleteById(id);
}
}
以上是缓存注解的基本使用,接下来将介绍其它高级开发技巧。
自定义注解
首先,我们定义一个自定义缓存注解 @PackCache,它将简化使用 Spring 缓存的代码。这个注解将会封装 @Cacheable 注解的功能。
自定义注解 PackCache
import org.springframework.cache.annotation.Cacheable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Cacheable(cacheNames = "person", key = "#person.id")
public @interface PackCache {}
使用自定义注解
然后,我们可以在服务类中使用这个自定义注解,来简化缓存的使用。
import org.springframework.stereotype.Service;
@Service
public class PersonService {
// 假设这是我们的用户存储库
private final PersonRepository personRepository;
public PersonService(PersonRepository personRepository) {
this.personRepository = personRepository;
}
@PackCache
public Person save(Person person) {
return personRepository.save(person);
}
}
2.2 切换缓存实现
接下来,我们展示如何在 Spring 中配置多个缓存管理器,并在使用缓存时选择特定的缓存管理器。
配置多个 CacheManager
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.cache.redis.RedisCacheConfiguration;
import org.springframework.cache.redis.RedisCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(config)
.build();
}
@Bean
public CacheManager concurrentMapCacheManager() {
return new ConcurrentMapCacheManager("person");
}
}
使用指定的 CacheManager
在服务类中,我们可以通过指定 cacheManager 属性来选择使用的缓存管理器。
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
@Service
public class PersonService {
private final PersonRepository personRepository;
public PersonService(PersonRepository personRepository) {
this.personRepository = personRepository;
}
@PackCache // 使用自定义注解
public Person save(Person person) {
return personRepository.save(person);
}
@Cacheable(cacheNames = "person", cacheManager = "concurrentMapCacheManager", key = "#id")
public Person findById(String id) {
return personRepository.findById(id).orElse(null);
}
@CacheEvict(cacheNames = "person", cacheManager = "redisCacheManager", key = "#id")
public void deleteById(String id) {
personRepository.deleteById(id);
}
}
自定义注解: 通过定义 @PackCache 注解,我们能够简化代码,减少重复的缓存注解使用。
切换缓存实现: 通过配置多个 CacheManager,我们可以灵活地选择使用不同的缓存实现(如 Redis 和 ConcurrentMap),并在需要时指定使用的 CacheManager。
这些示例展示了如何利用 Spring 的缓存机制来提高代码的可读性和灵活性,同时也展现了如何根据需要切换不同的缓存实现。

被折叠的 条评论
为什么被折叠?



