1.pom.xml 依赖
<!--Redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- cache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Redis链接池 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>
2. 配置redis链接(此处用的jedis,还有一种lettuce可自行尝试)
这边的配置文件用的application.properties(application.yml 配置文件格式自行百度)
# REDIS (RedisProperties)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active= 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait= -1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle= 8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle= 0
# 连接超时时间(毫秒)
spring.redis.timeout= 100
3.创建RedisCacheConfig.java文件
注意 : 注解
@Configuration:
用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器)
@EnableCaching:开启缓存功能
此处的文件,只是配置把数据以json格式进行缓存,看网上还有其他的配置,但没搞懂作用是什么,现只配置数据格式转换。
@Configuration
@EnableCaching
public class RedisCacheConfig {
private Duration timeToLive = Duration.ZERO;
public void setTimeToLive(Duration timeToLive) {
this.timeToLive = timeToLive;
}
@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);
// 配置序列化(解决乱码的问题)
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(timeToLive)
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
4.spring cache 注解的使用(三个注解的详细使用:https://blog.csdn.net/weixin_40251199/article/details/88028009)
@Cacheable 查询后,把返回值进行缓存,第二次查询时,若缓存里有则不进方法
@CachePut 更新后,根据key对缓存数据更新
@CacheEvict 根据key删除对应缓存数据
@Cacheable(value = "role",key = "#roleId")
public Role selectRole(Integer roleId){
Role role = new Role();
role.setId(roleId);
return roleMapper.selectOne(role);
}
@CachePut(value = "role",key = "#role.id",condition = "#role != null")
public Role operateRole(Role role){
Integer count;
if(role.getId() == null){
count = roleMapper.insertSelective(role);
}else{
count = roleMapper.updateByPrimaryKeySelective(role);
}
if(count > 0){
return roleMapper.selectByPrimaryKey(role.getId());
}else{
return null;
}
}
@CacheEvict(value = "role",key = "#roleId")
public Integer deleteRole(Integer roleId){
return roleMapper.deleteByPrimaryKey(roleId);
}
注意:实体类要实现序列化 实现接口 Serializable