SpringBoot的数据缓存——基于Redis缓存技术的配置

我们知道Spring支持很多种缓存,针对不同的缓存技术,我们都需要实现不同的CacheManager。但是SpringBoot帮我们自动配置了多个CacheManager的实现,我们只要引用不同的依赖包,SpringBoot会帮我们切换到不同的CacheManager,本篇文章说的是基于Redis的数据缓存。

1.导入依赖包:

  1. org.springframework.boot
  2. spring-boot-starter-cache
  3. org.springframework.boot
  4. spring-boot-starter-data-redis
  5. io.lettuce
  6. lettuce-core
  7. redis.clients
  8. jedis
  9. org.springframework.boot
  10. spring-boot-starter-web

这里我们导入了spring-boot-starter-cache,这个包就是实现了不同的CacheManager。我们去除lettuce,用jedis来操作redis

2.单机的redis配置
单机的RedisCacheManager在springboot中有帮我们实现,我们只要配置信息就行

  1. #缓存的配置
  2. spring.cache.cache-name=c1,c2 #缓存的前置名
  3. spring.cache.redis.time-to-live=1800s #缓存的时间
  4. #redis配置
  5. spring.redis.database=0 #redis有16库
  6. spring.redis.host=l92.168.174.10 #redis的IP
  7. spring.redis.port=6379 #redis的端口
  8. spring.edis.password=l23@123 #redis的密码
  9. spring.redis.jedis.pool.max-active=8 #redis的连接池最大连接数
  10. spring.redis.jedis.pool.max-idle=8 #redis的连接池中最大连接空闲数
  11. spring.redis.jedis.pool.max-wait=-1ms #redis的连接池中最大等待时间,-1,表示没有限制
  12. spring.redis.jedis.pool.min-idle=0 #redis的连接池中最小连接空闲数
  13. 3.Redis集群缓存配置(集群的RedisCacheManager要我们自己实现)
  14. yml文件种配置Redis集群信息
  15. spring:
  16. redis:
  17. cluster:
  18. ports:
  19. -8001
  20. -8002
  21. -8003
  22. -8004
  23. -8005
  24. -8006
  25. host: 192.168.174.10
  26. poolConfig:
  27. max-total: 8
  28. max-idle: 8
  29. max-wait-millis: -1
  30. min-idle: 0

编写集群缓存的配置类(单机的有实现,所以我们只要在配置文件种配置就行,集群的RedisCacheManager配置类要自己编写)

  1. @Configuration
  2. public class RedisCacheConfig {
  3. @Autowired
  4. RedisConnectionFactory connectionFactory;
  5. @Bean
  6. RedisCacheManager redisCacheManager(){
  7. Map<String, RedisCacheConfiguration> configMap=new HashMap<>();
  8. RedisCacheConfiguration redisCacheConfiguration=RedisCacheConfiguration.defaultCacheConfig().prefixKeysWith("sang:")
  9. .disableCachingNullValues().entryTtl(Duration.ofMinutes(30));
  10. configMap.put("c1",redisCacheConfiguration);
  11. RedisCacheWriter cacheWriter=RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
  12. RedisCacheManager redisCacheManager=new RedisCacheManager(cacheWriter,RedisCacheConfiguration.defaultCacheConfig(),configMap);
  13. return redisCacheManager;
  14. }
  15. }

代码说明:

在配直Redis集群时,已经向Spring容器中注册了一个JedisConnectionFactory的实例,里将之注入到RedisCacheConfig直文件中备用(RedisConnectionFactory是JedisConnectionFactory的父类)。配直Redis集群可以参考这篇博客:SpringBoot整合Redis单机和整合Redis集群的完整例子

在RedisCacheConfig中提供RedisCacheManager的实例,该实例的构建需要三个参数,第一个参数是一个cacheWriter,直接通过nonLockingRedisCacheWriter方法构造出来即可;第二个参数是默认的缓存配直;第三个参数是提前定义好的缓存配直。

RedisCacheManager构造方中第三个参数是个提前定义好的缓存参数,它是一个Map类型的数,该Map中的key就是指缓存名字,value就是该名称的缓存所对应的缓存自己直,例如key的前缀、缓存过期时间等,若缓存注解中使用的缓存称不存在于Map中,则使用RedisCacheManager构造方法中第二个参数所定义的缓存策略进行数据缓存。例如如下两个缓存配直:

  1. @Cacheable(value="c1")
  2. @Cacheable(value="c2")

8-9行是一个自定义的缓存配直,设直了key的前缀为“sang:”,禁止缓存一个null,设直缓存的过期时间为30分钟。
4.开启缓存
缓存配置好了后,我们只要在启动类前设置@EnableCaching注解来开启缓存

  1. @Spring BootApplication
  2. @EnableCaching
  3. public class RedisclustercacheApplication {
  4. public static void main (String [] args ) {
  5. SpringApplication.run(RedisclustercacheApplication.class , args);
  6. }
  7. }

5.使用缓存
spring提供了4个注解来声明缓存的使用

注解 说明
@Cacheable 在方法使用先,先看缓存是否有数据,如果有则返回,如果没有则调用方法,结果返回也存入缓存中
@CachePut 不管缓存是否有数据,都会更新,将返回结果都会存入缓存中
@ChcheEvict 将一条或多条数据从缓存中删除
@Caching 组合多条缓存注解在一个方法上
代码:

  1. //先查询缓存名personname是否有数据,如果有则返回,如果没有则调用方法,将结果返回的也存入缓存中
  2. @Cacheable(value="personname",key="#person.name")
  3. public Person save(String name){
  4. Person p=new Person();
  5. Person.setName(name);
  6. return p;
  7. }
  8. //缓存名为personname,新增或者更新把Person的name存入缓存中,也就是不管缓存是否有数据,都会更新
  9. @CachePut (value="personname",key="#person.name")
  10. public Person save(String name){
  11. Person p=new Person();
  12. Person.setName(name);
  13. return p;
  14. }
  15. //删除缓存名为personname的缓存
  16. @ChcheEvict(value="personname")
  17. public void del(String name){
  18. }

如果没有指定key的情况下,会把参数当作key存入缓存中