我们知道Spring支持很多种缓存,针对不同的缓存技术,我们都需要实现不同的CacheManager。但是SpringBoot帮我们自动配置了多个CacheManager的实现,我们只要引用不同的依赖包,SpringBoot会帮我们切换到不同的CacheManager,本篇文章说的是基于Redis的数据缓存。
1.导入依赖包:
org.springframework.boot
spring-boot-starter-cache
org.springframework.boot
spring-boot-starter-data-redis
io.lettuce
lettuce-core
redis.clients
jedis
org.springframework.boot
spring-boot-starter-web
这里我们导入了spring-boot-starter-cache,这个包就是实现了不同的CacheManager。我们去除lettuce,用jedis来操作redis
2.单机的redis配置
单机的RedisCacheManager在springboot中有帮我们实现,我们只要配置信息就行
#缓存的配置
spring.cache.cache-name=c1,c2 #缓存的前置名
spring.cache.redis.time-to-live=1800s #缓存的时间
#redis配置
spring.redis.database=0 #redis有16库
spring.redis.host=l92.168.174.10 #redis的IP
spring.redis.port=6379 #redis的端口
spring.edis.password=l23@123 #redis的密码
spring.redis.jedis.pool.max-active=8 #redis的连接池最大连接数
spring.redis.jedis.pool.max-idle=8 #redis的连接池中最大连接空闲数
spring.redis.jedis.pool.max-wait=-1ms #redis的连接池中最大等待时间,-1,表示没有限制
spring.redis.jedis.pool.min-idle=0 #redis的连接池中最小连接空闲数
3.Redis集群缓存配置(集群的RedisCacheManager要我们自己实现)
在yml文件种配置Redis集群信息
spring:
redis:
cluster:
ports:
-8001
-8002
-8003
-8004
-8005
-8006
host: 192.168.174.10
poolConfig:
max-total: 8
max-idle: 8
max-wait-millis: -1
min-idle: 0
编写集群缓存的配置类(单机的有实现,所以我们只要在配置文件种配置就行,集群的RedisCacheManager配置类要自己编写)
@Configuration
public class RedisCacheConfig {
@Autowired
RedisConnectionFactory connectionFactory;
@Bean
RedisCacheManager redisCacheManager(){
Map<String, RedisCacheConfiguration> configMap=new HashMap<>();
RedisCacheConfiguration redisCacheConfiguration=RedisCacheConfiguration.defaultCacheConfig().prefixKeysWith("sang:")
.disableCachingNullValues().entryTtl(Duration.ofMinutes(30));
configMap.put("c1",redisCacheConfiguration);
RedisCacheWriter cacheWriter=RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
RedisCacheManager redisCacheManager=new RedisCacheManager(cacheWriter,RedisCacheConfiguration.defaultCacheConfig(),configMap);
return redisCacheManager;
}
}
代码说明:
在配直Redis集群时,已经向Spring容器中注册了一个JedisConnectionFactory的实例,里将之注入到RedisCacheConfig直文件中备用(RedisConnectionFactory是JedisConnectionFactory的父类)。配直Redis集群可以参考这篇博客:SpringBoot整合Redis单机和整合Redis集群的完整例子
在RedisCacheConfig中提供RedisCacheManager的实例,该实例的构建需要三个参数,第一个参数是一个cacheWriter,直接通过nonLockingRedisCacheWriter方法构造出来即可;第二个参数是默认的缓存配直;第三个参数是提前定义好的缓存配直。
RedisCacheManager构造方中第三个参数是个提前定义好的缓存参数,它是一个Map类型的数,该Map中的key就是指缓存名字,value就是该名称的缓存所对应的缓存自己直,例如key的前缀、缓存过期时间等,若缓存注解中使用的缓存称不存在于Map中,则使用RedisCacheManager构造方法中第二个参数所定义的缓存策略进行数据缓存。例如如下两个缓存配直:
@Cacheable(value="c1")
@Cacheable(value="c2")
8-9行是一个自定义的缓存配直,设直了key的前缀为“sang:”,禁止缓存一个null,设直缓存的过期时间为30分钟。
4.开启缓存
缓存配置好了后,我们只要在启动类前设置@EnableCaching注解来开启缓存
@Spring BootApplication
@EnableCaching
public class RedisclustercacheApplication {
public static void main (String [] args ) {
SpringApplication.run(RedisclustercacheApplication.class , args);
}
}
5.使用缓存
spring提供了4个注解来声明缓存的使用
注解 说明
@Cacheable 在方法使用先,先看缓存是否有数据,如果有则返回,如果没有则调用方法,结果返回也存入缓存中
@CachePut 不管缓存是否有数据,都会更新,将返回结果都会存入缓存中
@ChcheEvict 将一条或多条数据从缓存中删除
@Caching 组合多条缓存注解在一个方法上
代码:
//先查询缓存名personname是否有数据,如果有则返回,如果没有则调用方法,将结果返回的也存入缓存中
@Cacheable(value="personname",key="#person.name")
public Person save(String name){
Person p=new Person();
Person.setName(name);
return p;
}
//缓存名为personname,新增或者更新把Person的name存入缓存中,也就是不管缓存是否有数据,都会更新
@CachePut (value="personname",key="#person.name")
public Person save(String name){
Person p=new Person();
Person.setName(name);
return p;
}
//删除缓存名为personname的缓存
@ChcheEvict(value="personname")
public void del(String name){
}
如果没有指定key的情况下,会把参数当作key存入缓存中
原创来源:滴一盘技术