1、在springboot 配置文件application.yml中添加redis配置
<!-- redis
-->
spring
:
# Redis 配置
redis
:
database
: 0 # Redis数据库索引(默认为
0)
host
: 127.0.0.1 # Redis服务器地址
port
: 6379 # Redis服务器连接端口
password
: # Redis服务器连接密码(默认为空)
# jedis可无,按需求添加,目前还没研究透
jedis
:
pool
:
max
-active
: 8 # 连接池最大连接数(使用负值表示没有限制)
max
-wait
: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max
-idle
: 8 # 连接池中的最大空闲连接
min
-idle
: 0 # 连接池中的最小空闲连接
timeout
: 1200 # 连接超时时间(毫秒)
2、在pom.xml中添加redis依赖
<!-- redis
-->
<dependency
>
<groupId
>org
.springframework
.boot
</groupId
>
<artifactId
>spring
-boot
-starter
-data
-redis
</artifactId
>
</dependency
>
3、添加配置类
package com
.gree
.config
;
import com
.fasterxml
.jackson
.annotation
.JsonAutoDetect
;
import com
.fasterxml
.jackson
.annotation
.PropertyAccessor
;
import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
import org
.springframework
.cache
.CacheManager
;
import org
.springframework
.cache
.annotation
.CachingConfigurerSupport
;
import org
.springframework
.cache
.annotation
.EnableCaching
;
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
.cache
.RedisCacheWriter
;
import org
.springframework
.data
.redis
.connection
.RedisConnectionFactory
;
import org
.springframework
.data
.redis
.core
.*;
import org
.springframework
.data
.redis
.serializer
.Jackson2JsonRedisSerializer
;
import org
.springframework
.data
.redis
.serializer
.StringRedisSerializer
;
import java
.time
.Duration
;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public CacheManager
cacheManager(RedisConnectionFactory redisConnectionFactory
) {
RedisCacheConfiguration redisCacheConfiguration
= RedisCacheConfiguration
.defaultCacheConfig()
.entryTtl(Duration
.ofHours(1));
return RedisCacheManager
.builder(RedisCacheWriter
.nonLockingRedisCacheWriter(redisConnectionFactory
))
.cacheDefaults(redisCacheConfiguration
).build();
}
@Bean
public RedisTemplate
<String
, Object
> redisTemplate(RedisConnectionFactory factory
) {
RedisTemplate
<String
, Object
> template
= new RedisTemplate<>();
template
.setConnectionFactory(factory
);
Jackson2JsonRedisSerializer jacksonSeial
= new Jackson2JsonRedisSerializer(Object
.class);
ObjectMapper om
= new ObjectMapper();
om
.setVisibility(PropertyAccessor
.ALL, JsonAutoDetect
.Visibility
.ANY);
om
.enableDefaultTyping(ObjectMapper
.DefaultTyping
.NON_FINAL);
jacksonSeial
.setObjectMapper(om
);
template
.setValueSerializer(jacksonSeial
);
template
.setKeySerializer(new StringRedisSerializer());
template
.setHashKeySerializer(new StringRedisSerializer());
template
.setHashValueSerializer(jacksonSeial
);
template
.afterPropertiesSet();
return template
;
}
@Bean
public HashOperations
<String
, String
, Object
> hashOperations(RedisTemplate
<String
, Object
> redisTemplate
) {
return redisTemplate
.opsForHash();
}
@Bean
public ValueOperations
<String
, Object
> valueOperations(RedisTemplate
<String
, Object
> redisTemplate
) {
return redisTemplate
.opsForValue();
}
@Bean
public ListOperations
<String
, Object
> listOperations(RedisTemplate
<String
, Object
> redisTemplate
) {
return redisTemplate
.opsForList();
}
@Bean
public SetOperations
<String
, Object
> setOperations(RedisTemplate
<String
, Object
> redisTemplate
) {
return redisTemplate
.opsForSet();
}
@Bean
public ZSetOperations
<String
, Object
> zSetOperations(RedisTemplate
<String
, Object
> redisTemplate
) {
return redisTemplate
.opsForZSet();
}
}
4、使用方法(持续更新----)
在使用地方注入
@Autowired
private RedisTemplate redisTemplate
;
获取缓存
ValueOperations
<String
, User
> operations
= redisTemplate
.opsForValue();
Object object
= operations
.get(key
);
写入缓存
ValueOperations
<String
, User
> operations
= redisTemplate
.opsForValue();
operations
.set(key
, object
, 5, TimeUnit
.HOURS);
删除缓存(先判断key是否存在)
ValueOperations
<String
, User
> operations
= redisTemplate
.opsForValue();
boolean haskey
= redisTemplate
.hasKey(key
);
if (haskey
) {
redisTemplate
.delete(key
);
System
.out
.println("删除缓存中的key-----------> " + key
);
}
更新缓存(先删除,再写入)
ValueOperations
<String
, User
> operations
= redisTemplate
.opsForValue();
boolean haskey
= redisTemplate
.hasKey(key
);
if (haskey
) {
redisTemplate
.delete(key
);
System
.out
.println("删除缓存中的key-----------> " + key
);
}
operations
.set(key
, object
, 3, TimeUnit
.HOURS);
判断redis中是否有键为key的缓存
boolean hasKey
= redisTemplate
.hasKey(key
);
if (hasKey
) {
System
.out
.println("--------------缓存存在--------------------");
} else{
System
.out
.println("-------------不存在-----------------------");
}