前言
基于springboot框架进行集成,前提是已经搭建基础的springboot框架和已经建立好Redis集群。
一、maven依赖
springboot的Redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
jedis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、yml配置
集群配置
spring:
# Redis集群相关配置
redis:
cluster:
# 集群节点
nodes: 10.3.1.214:7000,10.3.1.214:7001,10.3.1.214:7002,10.3.1.215:7000,10.3.1.215:7001,10.3.1.215:7002,10.3.1.216:7000,10.3.1.216:7001,10.3.1.216:7002
# 最大重定向次数
max-redirects: 5
#另一种连接池,暂时没用这种
lettuce:
pool:
max-active: 100
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认为-1
max-wait: -1
# 连接池中的最大空闲连接 默认为8
max-idle: 100
# 连接池中的最小空闲连接 默认为 0
min-idle: 50
连接池相关配置
redis:
timeout: 10000 #客户端超时时间单位是毫秒 默认是2000
maxIdle: 300 #最大空闲数
maxTotal: 1000 #控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性
maxWaitMillis: 1000 #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
minEvictableIdleTimeMillis: 300000 #连接的最小空闲时间 默认1800000毫秒(30分钟)
numTestsPerEvictionRun: 1024 #每次释放连接的最大数目,默认3
timeBetweenEvictionRunsMillis: 30000 #逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
testOnBorrow: true #是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
testWhileIdle: true #在空闲时检查有效性, 默认false
password: xxxxxx #密码
三、配置类编写
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set;
/**
* @description: redis配置类
* @author ZXS
* @date 2022/9/29 16:59
* @version 1.0
*/
@Configuration
@Slf4j
public class RedisConfig {
/** 集群基础配置信息 **/
@Value("${spring.redis.cluster.nodes}")
private String clusterNodes;
@Value("${spring.redis.cluster.max-redirects}")
private int maxRedirects;
/** 连接池基础配置信息 **/
@Value("${redis.password}")
private String password;
@Value("${redis.timeout}")
private int timeout;
@Value("${redis.maxIdle}")
private int maxIdle;
@Value("${redis.maxTotal}")
private int maxTotal;
@Value("${redis.maxWaitMillis}")
private int maxWaitMillis;
@Value("${redis.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${redis.numTestsPerEvictionRun}")
private int numTestsPerEvictionRun;
@Value("${redis.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${redis.testOnBorrow}")
private boolean testOnBorrow;
@Value("${redis.testWhileIdle}")
private boolean testWhileIdle;
/**
* @description: Redis连接池的配置
* @param:
* @return: redis.clients.jedis.JedisPoolConfig
* @author ZXS
* @date: 2022/9/29 17:00
*/
@Bean
public JedisPoolConfig getJedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 最大空闲数
jedisPoolConfig.setMaxIdle(maxIdle);
// 连接池的最大数据库连接数
jedisPoolConfig.setMaxTotal(maxTotal);
// 最大建立连接等待时间
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
// 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
// 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
// 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
// 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
jedisPoolConfig.setTestOnBorrow(testOnBorrow);
// 在空闲时检查有效性, 默认false
jedisPoolConfig.setTestWhileIdle(testWhileIdle);
return jedisPoolConfig;
}
/**
* @description: Redis集群的配置
* @param:
* @return: org.springframework.data.redis.connection.RedisClusterConfiguration
* @author ZXS
* @date: 2022/9/29 17:00
*/
@Bean
public RedisClusterConfiguration redisClusterConfiguration() {
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
// Set<RedisNode> clusterNodes
String[] serverArray = clusterNodes.split(",");
Set<RedisNode> nodes = new HashSet<RedisNode>();
for (String ipPort : serverArray) {
String[] ipAndPort = ipPort.split(":");
nodes.add(new RedisNode(ipAndPort[0].trim(), Integer.parseInt(ipAndPort[1])));
}
redisClusterConfiguration.setClusterNodes(nodes);
redisClusterConfiguration.setMaxRedirects(maxRedirects);
redisClusterConfiguration.setPassword(RedisPassword.of(password));
return redisClusterConfiguration;
}
/**
* @description: redis连接工厂类
* @param:
* @return: org.springframework.data.redis.connection.jedis.JedisConnectionFactory
* @author ZXS
* @date: 2022/9/29 17:00
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
// 集群模式
JedisConnectionFactory factory = new JedisConnectionFactory(redisClusterConfiguration(), getJedisPoolConfig());
return factory;
}
/**
* @description: 实例化 RedisTemplate 对象
* @param:
* @return: org.springframework.data.redis.core.RedisTemplate<java.lang.String,java.lang.Object>
* @author ZXS
* @date: 2022/9/29 17:00
*/
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// Template初始化
initDomainRedisTemplate(redisTemplate);
return redisTemplate;
}
/**
* @description: 设置数据存入 redis 的序列化方式 使用默认的序列化会导致key乱码
* @param: redisTemplate
* @return: void
* @author ZXS
* @date: 2022/9/29 17:00
*/
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
// 开启redis数据库事务的支持
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.setConnectionFactory(jedisConnectionFactory());
// 如果不配置Serializer,那么存储的时候缺省使用String,如果用对象类型存储,那么会提示错误对象类型 can't cast to String!
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
redisTemplate.setHashKeySerializer(stringRedisSerializer);
// jackson序列化对象设置(要与存储时采用的序列化方式保持一致,所以下面的序列化方式不行,
// 改用了自定义采用阿里fastJSON实现的序列化器成功实现反序列化)
/*Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
jackson2JsonRedisSerializer.setObjectMapper(om);*/
FastJson2JsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJson2JsonRedisSerializer<>(Object.class);
// value序列化方式采用fastJson
redisTemplate.setValueSerializer(fastJsonRedisSerializer);
// hash的value序列化方式采用fastJson
redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
redisTemplate.afterPropertiesSet();
}
}
说明:Redis的坑点一直都是在序列化这里,一定要确定存入和写出的数据序列化和反序列化保持一致。
四、服务类的编写
像非集群一样直接使用RedisTemplate进行各种操作就行了。
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @description: redis服务类,用于对Redis集群的各种操作
* @author ZXS
* @date 2022/9/29 14:12
* @version 1.0
*/
@Service
public class RedisService {
@Resource
private RedisTemplate<String, Object> redisTemplate;
// ============================Common=============================
/**
* @description: 判断是否过期
* @param: key键,time过期时间
* @return: boolean
* @author ZXS
* @date: 2022/9/29 14:13
*/
public boolean expire(String key, long time) {
try {
if (time >= 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description: 根据key获取过期时间
* @param: key键
* @return: long
* @author ZXS
* @date: 2022/9/29 14:14
*/
public long getExpire(String key) {
return Optional.ofNullable(redisTemplate.getExpire(key, TimeUnit.SECONDS)).orElse(0L);
}
/**
* @description: 判断key是否存在
* @param: key 键
* @return: boolean
* @author ZXS
* @date: 2022/9/29 14:15
*/
public boolean hasKey(String key) {
try {
return Optional.ofNullable(redisTemplate.hasKey(key)).orElse(false);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description: 删除缓存
* @param: keys keys 可以传一个值 或多个
* @return: boolean
* @author ZXS
* @date: 2022/9/29 14:58
*/
public boolean del(List<String> keys) {
if (keys.size() > 0) {
final Long aLong = Optional.ofNullable(redisTemplate.delete(keys)).orElse(0L);
return aLong > 0;
}
return false;
}
/**
* @description: 根据key模糊查询所有数据的key
* @param: key
* @return: java.util.Set<java.lang.String>
* @author ZXS
* @date: 2022/10/10 11:26
*/
public Set<String> getKeys(String key){
return redisTemplate.keys(key + "*");
}
// ============================String=============================
/**
* @description: 获取字符串类型数据
* @param: key 键
* @return: java.lang.Object
* @author ZXS
* @date: 2022/9/29 14:59
*/
public Object getString(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* @description: 存入字符串类型数据
* @param: key键,value值
* @return: boolean
* @author ZXS
* @date: 2022/9/29 15:00
*/
public boolean setString(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description:
* @param: key键,value值,time过期时间
* @return: boolean
* @author ZXS
* @date: 2022/9/29 15:00
*/
public boolean setStringWithTime(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
setString(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description: 递增
* @param: key键,delta 要增加几(大于0)
* @return: long
* @author ZXS
* @date: 2022/9/29 15:03
*/
public long incrString(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return Optional.ofNullable(redisTemplate.opsForValue().increment(key, delta)).orElse(0L);
}
/**
* @description: 递减
* @param: key键,delta 要减少几(小于0)
* @return: long
* @author ZXS
* @date: 2022/9/29 15:03
*/
public long decrString(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return Optional.ofNullable(redisTemplate.opsForValue().increment(key, -delta)).orElse(0L);
}
/**
* @description: 递减默认
* @param: key键
* @return: long
* @author ZXS
* @date: 2022/9/29 15:04
*/
public long decrString(String key) {
return Optional.ofNullable(redisTemplate.opsForValue().increment(key)).orElse(0L);
}
// ================================hash=================================
/**
* @description: Hash类型获取
* @param: key键,item项 均不能为null
* @return: java.lang.Object
* @author ZXS
* @date: 2022/9/29 15:04
*/
public Object getHash(String key, String item) {
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate.opsForHash().get(key, item);
}
/**
* @description: 获取hashKey对应的所有键值
* @param: key 键
* @return: java.util.Map<java.lang.Object,java.lang.Object> 对应的多个键值
* @author ZXS
* @date: 2022/9/29 15:06
*/
public Map<Object, Object> getHashList(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* @description: 获取hashKey的长度
* @param: key 键
* @return: java.lang.Long
* @author ZXS
* @date: 2022/9/29 15:06
*/
public Long hashLength(String key) {
Long size = 0L;
try {
size = Optional.of(redisTemplate.opsForHash()).map(o -> o.size(key)).orElse(0L);
} catch (Exception e) {
e.printStackTrace();
return 0L;
}
return size;
}
/**
* @description: 存储hash类型数据并设置过期时间
* @param: key键,map类型数据,time过期时间
* @return: boolean
* @author ZXS
* @date: 2022/9/29 15:08
*/
public boolean setHashWithTime(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description: 向一张hash表中放入多条数据,如果不存在将创建
* @param: key键,map对应多个键值
* @return: boolean true 成功 false 失败
* @author ZXS
* @date: 2022/9/29 15:09
*/
public boolean setHash(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description: 向一张hash表中放入数据,如果不存在将创建
* @param: key键,item项,value值
* @return: boolean
* @author ZXS
* @date: 2022/9/29 15:11
*/
public boolean setHash(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description: 向一张hash表中放入数据,如果不存在将创建
* @param: key键,item项,value值,time 过期时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return: boolean
* @author ZXS
* @date: 2022/9/29 15:14
*/
public boolean setHashWithTime(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description: 删除hash表中的值
* @param: key键不能为null,item项可以使多个不能为null
* @return: void
* @author ZXS
* @date: 2022/9/29 15:12
*/
public void delHash(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* @description: 判断hash表中是否有该项的值
* @param: key键 不能为null,item 项 不能为null
* @return: boolean
* @author ZXS
* @date: 2022/9/29 15:16
*/
public boolean isExistHash(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* @description: hash递增 如果不存在,就会创建一个 并把新增后的值返回
* @param: key键,item项,by要增加几(大于0)
* @return: double
* @author ZXS
* @date: 2022/9/29 15:18
*/
public double incrHash(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* @description: hash递减
* @param: key键,item项,by要减少记(小于0)
* @return: double
* @author ZXS
* @date: 2022/9/29 15:19
*/
public double decrHash(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
// ===============================list=================================
/**
* @description: 通过索引 获取list中的值
* @param: key键,index 索引 index>=0时,0表头,1第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
* @return: java.lang.Object
* @author ZXS
* @date: 2022/9/29 15:19
*/
public Object getList(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* @description: 获取list指定范围的内容
* @param: key键,start开始,end结束0到-1代表所有值
* @return: java.util.List<java.lang.Object>
* @author ZXS
* @date: 2022/9/29 15:20
*/
public List<Object> getRangeList(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* @description: 获取list缓存的长度
* @param: key 键
* @return: long
* @author ZXS
* @date: 2022/9/29 15:21
*/
public long listLength(String key) {
try {
return Optional.ofNullable(redisTemplate.opsForList().size(key)).orElse(0L);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* @description: 将list放入缓存
* @param: key键,value值
* @return: boolean
* @author ZXS
* @date: 2022/9/29 15:22
*/
public boolean setList(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description: 将数据放入list缓存并设置过期时间
* @param: key键,value值,time过期时间
* @return: boolean
* @author ZXS
* @date: 2022/9/29 15:22
*/
public boolean setListWithTime(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description: 将list放入list缓存
* @param: key键,value值
* @return: boolean
* @author ZXS
* @date: 2022/9/29 15:25
*/
public boolean setListWithList(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description: 将list放入list缓存并设置过期时间
* @param: key键,value值,time过期时间(秒)
* @return: boolean
* @author ZXS
* @date: 2022/9/29 15:26
*/
public boolean setListWithList(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description: 根据索引修改list中的某条数据
* @param: key键,index索引,value值
* @return: boolean
* @author ZXS
* @date: 2022/9/29 15:27
*/
public boolean updateList(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @description: 移除N个值为value
* @param: key键,count移除多少个,value值
* @return: long 移除的个数
* @author ZXS
* @date: 2022/9/29 15:27
*/
public long delList(String key, long count, Object value) {
try {
return Optional.ofNullable(redisTemplate.opsForList().remove(key, count, value)).orElse(0L);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
// ============================set=============================
/**
* @description: 根据key获取Set中的所有值
* @param: key键
* @return: java.util.Set<java.lang.Object>
* @author ZXS
* @date: 2022/9/29 15:29
*/
public Set<Object> getSet(String key) {
return redisTemplate.opsForSet().members(key);
}
/**
* @description: 根据value从一个set中查询,是否存在
* @param: key键,value值
* @return: boolean true存在 false不存在
* @author ZXS
* @date: 2022/9/29 15:29
*/
public boolean isExistSet(String key, Object value) {
return Optional.ofNullable(redisTemplate.opsForSet().isMember(key, value)).orElse(false);
}
/**
* @description: 将数据放入set缓存
* @param: key键,values值,可以是多个
* @return: long
* @author ZXS
* @date: 2022/9/29 15:30
*/
public long setSet(String key, Object... values) {
return Optional.ofNullable(redisTemplate.opsForSet().add(key, values)).orElse(0L);
}
/**
* @description: 将set数据放入缓存
* @param: key键,time时间(秒),values值 可以是多个
* @return: long 成功个数
* @author ZXS
* @date: 2022/9/29 15:30
*/
public long setSetWithTime(String key, long time, Object... values) {
try {
if (time > 0) {
expire(key, time);
}
return Optional.ofNullable(redisTemplate.opsForSet().add(key, values)).orElse(0L);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* @description: 随机移除并返回一个元素
* @param: key键
* @return: java.lang.Object 被移除的元素
* @author ZXS
* @date: 2022/9/29 15:31
*/
public Object delSet(String key) {
return redisTemplate.opsForSet().pop(key);
}
/**
* @description: 随机返回指定长度的count个元素
* @param: key键,count返回元素的个数
* @return: java.util.List<java.lang.Object>
* @author ZXS
* @date: 2022/9/29 15:31
*/
public List<Object> getRandCountSet(String key, long count) {
return redisTemplate.opsForSet().randomMembers(key, count);
}
/**
* @description: 两个set集合求差集
* @param: key1 其中一个set的key,key2 另一个set的key
* @return: java.util.Set<java.lang.Object>
* @author ZXS
* @date: 2022/9/29 15:32
*/
public Set<Object> diffSet(String key1, String key2) {
return redisTemplate.opsForSet().difference(key1, key2);
}
/**
* @description: 两个set集合求交集
* @param: key1 其中一个set的key,key2 另一个set的key
* @return: java.util.Set<java.lang.Object>
* @author ZXS
* @date: 2022/9/29 15:32
*/
public Set<Object> interSet(String key1, String key2) {
return redisTemplate.opsForSet().intersect(key1, key2);
}
/**
* @description: 两个set集合求并集
* @param: key1 其中一个set的key,key2 另一个set的key
* @return: java.util.Set<java.lang.Object>
* @author ZXS
* @date: 2022/9/29 15:32
*/
public Set<Object> unionSet(String key1, String key2) {
return redisTemplate.opsForSet().union(key1, key2);
}
/**
* @description: 获取set缓存的长度
* @param: key 键
* @return: long
* @author ZXS
* @date: 2022/9/29 15:33
*/
public long setLength(String key) {
return Optional.ofNullable(redisTemplate.opsForSet().size(key)).orElse(0L);
}
/**
* @description: 移除值为value的set
* @param: key键,values值 可以是多个
* @return: long 移除的个数
* @author ZXS
* @date: 2022/9/29 15:33
*/
public long delSet(String key, Object... values) {
return Optional.ofNullable(redisTemplate.opsForSet().remove(key, values)).orElse(0L);
}
// ============================sorted set(有序集合)=============================
/**
* @description: 添加元素到zSet集合当中去
* @param: key键,value值,score分数
* @return: boolean
* @author ZXS
* @date: 2022/9/29 15:34
*/
public boolean setZSet(String key, Object value, double score) {
return Optional.ofNullable(redisTemplate.opsForZSet().add(key, value, score)).orElse(false);
}
/**
* @description: 返回指定分数范围的数据
* @param: key键,start起始位置,end结束位置
* @return: java.util.Set<java.lang.Object> 指定分数范围内的zSet
* @author ZXS
* @date: 2022/9/29 15:34
*/
public Set<Object> getRangeZSet(String key,long start,long end){
return redisTemplate.opsForZSet().range(key,start,end);
}
}