Springboot中Redis的使用
1.Redis介绍
Redis是当前比较热门的NOSQL系统之一,它是一个开源的使用ANSI c语言编写的key-value存储系统(区别于MySQL的二维表格的形式存储。)。和Memcache类似,但很大程度补偿了Memcache的不足。和Memcache一样,Redis数据都是缓存在计算机内存中,不同的是,Memcache只能将数据缓存到内存中,无法自动定期写入硬盘,这就表示,一断电或重启,内存清空,数据丢失。所以Memcache的应用场景适用于缓存无需持久化的数据。而Redis不同的是它会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,实现数据的持久化。
Redis的特点
- Redis读取的速度是110000次/s,写的速度是81000次/s;
- Redis的所有操作都是原子性的,同时Redis还支持对几个操作全并后的原子性执行。
- 支持多种数据结构:string(字符串);list(列表);hash(哈希),set(集合);zset(有序集合)
- 持久化,集群部署
- 支持过期时间,支持事务,消息订阅
Spring Cache 是一个非常优秀的缓存组件。自Spring 3.1起,提供了类似于@Transactional注解事务的注解Cache支持,且提供了Cache抽象,方便切换各种底层Cache(如:redis)
使用Spring Cache的好处:
- 提供基本的Cache抽象,方便切换各种底层Cache;
- 通过注解Cache可以实现类似于事务一样,缓存逻辑透明的应用到我们的业务代码上,且只需要更少的代码就可以完成;
- 提供事务回滚时也自动回滚缓存;
- 支持比较复杂的缓存逻辑;
2.添加Redis配置依赖
2.1 添加依赖包
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
2.2 添加Redis配置类
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.lang.reflect.Method;
import java.time.Duration;
@Configuration
@EnableCaching
public class CacheConfig {
@Value("${cache.default-exp}")
private long exps;
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
/**
* 设置RedisTemplate规则
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
//序列号key value
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
/**
* 设置CacheManager缓存规则
* @param factory
* @return
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间600秒
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(exps))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
2.3 修改application.yml配置文件
cache:
default-exp: 1000 #单位秒,缓存的过期时间,配置类用到
spring:
redis:
# Redis数据库索引(默认为0)
database: 0
# Redis服务器地址
host: 1.15.73.44
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码(默认为空)
password: 123456
lettuce:
pool:
# 连接池最大连接数(使用负值表示没有限制) 默认 8
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
max-wait: -1
# 连接池中的最大空闲连接 默认 8
max-idle: 8
# 连接池中的最小空闲连接 默认 0
min-idle: 0
3.通过注解添加Redis缓存
3.1 @EnableCaching
开启缓存功能,
Redis配置类
中需要加上这个注解,有了这个注解之后,spring才知道你需要使用缓存的功能,其他和缓存相关的注解才会有效,spring中主要是通过aop实现的,通过aop来拦截需要使用缓存的方法,实现缓存的功能。
@Configuration
@EnableCaching
public class CacheConfig {}
3.2 @Cacheable(查询)
属性 | 解释 |
---|---|
value/cacheNames | 缓存名,必填,它指定了你的缓存存放在哪块命名空间redis中的key |
key | 可以通过 key 属性来指定缓存数据所使用的的 key,默认使用的是方法调用传过来的参数作为 key。最终缓存中存储的内容格式为:Entry<key,value> 形式。1.如果请求没有参数:key=new SimpleKey();2.如果请求有一个参数:key=参数的值;3.如果请求有多个参数:key=newSimpleKey(params); (你只要知道 key不会为空就行了) |
condition | 条件判断属性,用来指定符合指定的条件下才可以缓存。也可以通过 SpEL 表达式进行设置。这个配置规则和上面表格中的配置规则是相同的。 |
unless | unless属性,意为"除非"的意思。即只有 unless 指定的条件为 true 时,方法的返回值才不会被缓存。可以在获取到结果后进行判断。 |
sync | 该属性用来指定是否使用异步模式,该属性默认值为 false,默认为同步模式。异步模式指定 sync = true 即可,异步模式下 unless 属性不可用。 |
- 事例
/**
* 注解@Cacheable:查询的时候才使用该注解!
* 注意:在Cacheable注解中支持EL表达式
* redis缓存的key=user_1/2/3....
* redis的缓存雪崩,缓存穿透,缓存预热,缓存更新...
* condition = "#result ne null",条件表达式,当满足某个条件的时候才进行缓存
* unless = "#result eq null":当user对象为空的时候,不进行缓存
*/
@Cacheable(value = "user", key = "#id", unless = "#result eq null")
@Override
public User findById(Long id) {
return userRepository.findById(id).orElse(null);
}
- key属性说明
//如果id传输的是1,在Redis中存储的key为 dict::selectIndexList1
@Cacheable(value = "dict", key = "'selectIndexList'+#id")
List<Dict> findChlidData(Long id);
- condition属性说明
@Cacheable(value = "user",condition = "#a0>1 and #root.methodName eq 'getUser'")//传入的第一个参数的值>1 且 方法名为 getUser 的时候才进行缓存
User getUser(Integer id);
- unless属性说明
@Cacheable(value = "user",unless = "#result == null")//当方法返回值为 null 时,就不缓存
User getUser(Integer id);
@Cacheable(value = "user",unless = "#a0 == 1")//如果第一个参数的值是1,结果不缓存
User getUser(Integer id);
3.3 @CachePut(添加、修改)
@CachePut 也可以声明一个方法支持缓存功能。
与 @Cacheable 不同的是使用 @CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。
使用@CachePut 可以指定的属性跟 @Cacheable 是一样的, @CachePut 适用于缓存更新。
/**
* 注解@CachePut:一般用在添加和修改方法中
* 既往数据库中添加一个新的对象,于此同时也往redis缓存中添加一个对应的缓存.
* 这样可以达到缓存预热的目的.
*/
@CachePut(value = "user", key = "#result.id", unless = "#result eq null")
@Override
public User save(User user) {
return userRepository.save(user);
}
3.4 @CacheEvict(删除)
@CacheEvict 是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。
@CacheEvict支持的属性额外增加了两个:
1、allEntries:是否需要清除缓存中的所有元素。默认为 false ,表示不需要。当指定了 allEntries 为 true 时,Spring Cache将忽略指定的key,删除缓存中所有键;
2、beforeInvocation: 是否在方法执行成功之后触发键删除操作,默认是在对应方法成功执行之后触发的,若此时方法抛出异常而未能成功返回,不会触发清除操作。指定该属性值为 true 时,Spring会在调用该方法之前清除缓存中的指定元素。
/**
* CacheEvict:一般用在删除方法中
*/
@CacheEvict(value = "user", key = "#id")
@Override
public void deleteById(Long id) {
userRepository.deleteById(id);
}
3.5@Caching
@Caching 注解可以在一个方法或者类上同时指定多个Spring Cache相关的注解。
其拥有三个属性:cacheable、put 和 evict,分别用于指定@Cacheable、@CachePut 和 @CacheEvict。对于一个数据变动,更新多个缓存的场景,可以通过 @Caching 来实现
@Caching(cacheable = @Cacheable(cacheNames = "caching", key = "#age"), evict = @CacheEvict(cacheNames = "t4", key = "#age"))
public String caching(int age) {
return "caching: " + age + "-->" + UUID.randomUUID().toString();
}
3.6 @CacheConfig
如果一个类中,多个方法都有同样的 cacheName,keyGenerator,cacheManager 和 cacheResolver,可以直接使用 @CacheConfig 注解在类上声明,这个类中的方法都会使用@CacheConfig 属性设置的相关配置。
@Component
@CacheConfig(cacheNames = "mall_cache")
public class CacheComponent {
@Cacheable(key = "'perm-whitelist-'+#clientId", unless="#result == null")
public List<String> cacheWriteList(String clientId){
...
}
@Cacheable(key = "'perm-cutom-aci-' + #tenantId + '-' + #roleId + '-' + #tenantLevel + '-' + #subType", unless="#result == null")
public List<RequestDto> cacheRequest(Long tenantId,Long roleId,Integer tenantLevel,Integer subType){
...
}
}