因为Spring Boot 的自动化配置以及整合封装,开发者只需要在项目中引入Spring Data Redis 依赖,然后在配置文件中配置redis相关的基本信息,系统就会提供RedisTemplate和StringRedisTemplate供开发者使用。Cache是Spring3.1版本中引入的,在Spring Boot 中,Spring Cache相当于规范,而Redis是Spring Cache的实现,从而实现数据的缓存。
1. 创建项目,添加缓存依赖
对应的依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.8.0</version>
</dependency>
2.缓存配置
工程创建好之后,在配置文件中配置Redis的基本信息以及缓存配置
#Cache配置
#缓存名称
spring.cache.cache-names=c1,c2
#Redis中key的过期时间
spring.cache.redis.time-to-live=1800s
#Redis配置
spring.redis.host=192.168.92.128
spring.redis.port=6379
spring.redis.password=123456
spring.redis.database=0
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
spring.redis.lettuce.pool.max-wait=-1s
3.开启缓存
在启动类上添加@EnableCaching注解,表示开启缓存。
@SpringBootApplication
@EnableCaching
public class SpringbootCacheRedisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootCacheRedisApplication.class, args);
}
}
完成了这些配置之后,Spring Boot 就会自动帮我们在后台配置一个 RedisCacheManager,相关的配置是在org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration 类中完成的。部分源码如下:
@Configuration
@ConditionalOnClass(RedisConnectionFactory.class)
@AutoConfigureAfter(RedisAutoConfiguration.class)
@ConditionalOnBean(RedisConnectionFactory.class)
@ConditionalOnMissingBean(CacheManager.class)
@Conditional(CacheCondition.class)
class RedisCacheConfiguration {
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory,
ResourceLoader resourceLoader) {
RedisCacheManagerBuilder builder = RedisCacheManager
.builder(redisConnectionFactory)
.cacheDefaults(determineConfiguration(resourceLoader.getClassLoader()));
List<String> cacheNames = this.cacheProperties.getCacheNames();
if (!cacheNames.isEmpty()) {
builder.initialCacheNames(new LinkedHashSet<>(cacheNames));
}
return this.customizerInvoker.customize(builder.build());
}
}
系统会自动提供一个 RedisCacheManager 的 Bean,这个 RedisCacheManager 间接实现了 Spring 中的 Cache 接口,有了这个 Bean,我们就可以直接使用 Spring 中的缓存注解和接口了,而缓存数据则会被自动存储到 Redis 上。
4.缓存的使用
@CacheConfig注解指明使用缓存的名字,若不使用该注解,则可直接在@Cacheable注解中指明缓存的名字。
@Service
@CacheConfig(cacheNames = "c1,c2")
public class BookService {
}
@Cacheable注解表示将一个方法的返回值缓存起来,默认情况下,缓存的 key 就是方法的参数,缓存的 value 就是方法的返回值。
@Cacheable(key = "#id")
public User getUserById(Integer id,String username) {
System.out.println("getUserById");
return getUserFromDBById(id);
}
当有多个参数时,默认就使用多个参数来做 key,如果只需要其中某一个参数做 key,则可以在 @Cacheable 注解中,通过 key 属性来指定 key,如上代码就表示只使用 id 作为缓存的 key,如果对 key 有复杂的要求,可以自定义 keyGenerator。
@Cacheable(keyGenerator = "myKeyGenerator")
public Book getBookById(Integer id){
System.out.println("getBookById>>>>>>>>>"+id);
Book book = new Book();
book.setId(1);
book.setName("三国演义");
book.setAuthor("罗贯中");
return book;
}
@Component
public class myKeyGenerator implements KeyGenerator {
/**
* @param o 当前对象
* @param method 当前请求的方法
* @param objects 当前请求方法的参数
* @return
*/
@Override
public Object generate(Object o, Method method, Object... objects) {
return Arrays.asList(objects);
}
}
当然,Spring Cache 中提供了 root 对象,可以在不定义 keyGenerator 的情况下实现一些复杂的效果:
@CachePut注解一般用于数据更新上,添加了@CachePut注解的方法每次在执行时不会去检查缓存中是否有数据,而是直接执行方法。然后将方法执行的结果保存起来,若该Key对应的数据已经被缓存了,就会覆盖之前的数据,这样即可避免再次加载数据时获取到的脏数据。
@CachePut(key = "#book.id")
public Book updateBookById(Book book){
System.out.println("updateBookById>>>>>>>>>");
book.setId(1);
book.setName("水浒传");
book.setAuthor("施耐庵");
return book;
}
@CacheEvict注解一般用于删除方法上,当数据库中的数据删除后,相关的缓存数据也要自动清除,该注解在使用的时候也可以配置按照某种条件删除(condition 属性)或者或者配置清除所有缓存(allEntries 属性)
@CacheEvict(key = "#id")
public void deleteBookById(Integer id){
System.out.println("deleteBookById>>>>>>>>>");
//在这里执行删除操作, 删除是去数据库中删除
}
总结
在Spring Boot中,Redis实现缓存既可以使用Spring Cache 提供的规范,利用Redis或者是Ehcache来实现这一规范,也可以使用RedisTemplate自身来实现缓存。其中@EnableCaching,@Cacheable,@CachePut,@CacheEvict注解都由Spring Cache 提供。