微服务 教程
1085个小节阅读:196.5k
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
Spring-Data-Redis是spring大家族的一部分,通过简单的配置访问Redis服务,对Reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了Redis各种操作、异常处理及序列化,支持发布订阅。
Spring封装了RedisTemplate对象来进行对Redis的各种操作,它支持所有的Redis原生的api。
xxxxxxxxxx
org.springframework.data.redis.core
Class RedisTemplate<K,V>
注意:
- K:模板中的Redis key的类型,模板中的Redis key的类型(通常为String)如:RedisTemplate<String, Object>。
- V:模板中的Redis value的类型
xxxxxxxxxx
redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set
两者的关系是StringRedisTemplate继承RedisTemplate。
两者的数据是不共通的;也就是说StringRedisTemplate只能管理StringRedisTemplate里面的数据,RedisTemplate只能管理RedisTemplate中的数据。
SDR默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略。
StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。
RedisTemplate默认采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
xxxxxxxxxx
#Redis服务器连接地址
spring.redis.host=192.168.56.31
#Redis服务器连接端口
spring.redis.port=6379
#连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.pool.max-idle=8
#连接池中的最小空闲连接
spring.redis.pool.min-idle=0
#连接超时时间(毫秒)
spring.redis.timeout=30000
xxxxxxxxxx
/**
* 自定义序列化方式
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
xxxxxxxxxx
public boolean set(String key,Object value){
try{
redisTemplate.opsForValue().set(key,value);
return true;
}catch (Exception e){
log.error("redis set value exception:{}",e);
return false;
}
}
xxxxxxxxxx
public Object get(String key){
return key == null ? null : redisTemplate.opsForValue().get(key);
}
xxxxxxxxxx
public boolean setex(String key,Object value,long expire){
try{
//TimeUnit.SECONDS指定类型为秒
redisTemplate.opsForValue().set(key,value,expire, TimeUnit.SECONDS);
return true;
}catch (Exception e){
log.error("redis set value and expire exception:{}",e);
return false;
}
}
xxxxxxxxxx
public boolean hset(String key, String field, Object value,long seconds) {
try {
redisTemplate.opsForHash().put(key, field, value);
expire(key,seconds);//调用通用方法设置过期时间
return true;
}catch (Exception e){
log.error("redis hset and expire eror,key:{},field:{},value:{},exception:{}",key,field,value,e);
return false;
}
}
xxxxxxxxxx
public Object hget(String key,String field){
return redisTemplate.opsForHash().get(key,field);
}
xxxxxxxxxx
public long sset(String key ,Object...values){
try {
return redisTemplate.opsForSet().add(key,values);
}catch (Exception e){
log.error("redis sset error,key:{},value:{},values:{},exception:{}",key,values,e);
return 0;
}
}
xxxxxxxxxx
public long sgetSize(String key){
try {
return redisTemplate.opsForSet().size(key);
}catch (Exception e){
log.error("redis sgetSize error,key:{},exception:{}",key,e);
return 0;
}
}
xxxxxxxxxx
public Set<Object> sgetAll(String key){
try {
return redisTemplate.opsForSet().members(key);
}catch (Exception e){
log.error("redis sgetAll error,key:{},exception:{}",key,e);
return null;
}
}
xxxxxxxxxx
public boolean zadd(String key,Object member,double score){
try {
return redisTemplate.opsForZSet().add(key,member,score);
} catch (Exception e) {
log.error("redis zadd error,key:{},value:{},score:{},exception:{}",key,member,score,e);
return false;
}
}
xxxxxxxxxx
public Set<String> zrange(String key,int start,int end){
try {
Set<Object> range = redisTemplate.opsForZSet().
range(key, start, end);
if(range==null||range.size()==0) return null;
return range.stream().
map(o->(String)o).collect(Collectors.toSet());
} catch (Exception e) {
log.error("redis zrange error,key:{},start:{},end:{},exception:{}",
key,start,end,e);
return null;
}
}
xxxxxxxxxx
public boolean lrpush(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
log.error("redis lrpush error,key:{},value:{}exception:{}",key,value,e);
return false;
}
}
xxxxxxxxxx
public List<Object> getList(String key,int start,int end) {
try {
List<Object> o = redisTemplate.opsForList().range(key,start,end);
return o;
} catch (Exception e) {
return null;
}
}