Redis有序集合简单测试
Spring boot pom 和 application.properties
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
# Redis数据库索引(默认为0)spring.redis.database=2# Redis服务器地址spring.redis.host=localhost# 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=4# 连接池中的最小空闲连接spring.redis.jedis.pool.min-idle=4# 连接超时时间(毫秒)spring.redis.timeout=18000
添加 RedisConfig 配置类,设置key 为String 方便观察
/** * Created by yuyunbo on 2020/5/15. */@Configurationpublic class RedisConfig {@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
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);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
新建 RedisApiService 接口类
** * Created by yuyunbo on 2020/5/15. */@Componentpublic class RedisApiService {@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void zAdd(String key,Object value,double scoure){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add(key,value,scoure);
}
/**
* 有序集合获取
* @param key
* @param minScoure
* @param maxScoure
* @return
*/
public Set<Object> rangeByScore(String key, double minScoure, double maxScoure){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.rangeByScore(key, minScoure, maxScoure);
}
/**
* 获取范围内从指定下标获取,指定长度数据
* @param key
* @param minScoure
* @param maxScoure
* @param offset
* @param count
* @return
*/
public Set<Object> rangeByScore(String key, double minScoure, double maxScoure, long offset, long count){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.rangeByScore(key, minScoure, maxScoure,offset,count);
}
}
新建 RedisZsetTest 测试Controller
/** * Created by yuyunbo on 2020/5/15. */@Controller@RequestMapping("/redisZsetTest")public class RedisZsetTest {
@Autowired
private RedisApiService redisApiService;
private static final String key = "redisZset";
/**
* 循环101 次往redis 中插入 key = "redisZset" 有序集合,按照循环的下标作为排序的 scoure
* @return
*/
@RequestMapping("/addZSetToRedis")
@ResponseBody
public Object addZSetToRedis() {
for (int i = 0; i <= 100; i++) {
String index = String.valueOf(i);
redisApiService.zAdd(key, "value".concat(index), Double.valueOf(index));
}
return "success";
}
/**
* 获取 0,到 10范围的集合
* @return
*/
@RequestMapping("/getZSetFromRedis")
@ResponseBody
public Object getZSetFromRedis() {
return redisApiService.rangeByScore(key, Double.valueOf(0), Double.valueOf(10));
}
/**
* 获取0到50,范围内,从排序分为40的元素,往后读取5个元素(包含起始下标)
* @return
*/
@RequestMapping("/getZSetFromRedisOffset")
@ResponseBody
public Object getZSetFromRedisOffset() {
return redisApiService.rangeByScore(key, Double.valueOf(0), Double.valueOf(50),40,5);
}
}
http://127.0.0.1:8080/yuqiqi/redisZsetTest/addZSetToRedis ,结果如下图
http://127.0.0.1:8080/yuqiqi/redisZsetTest/getZSetFromRedis
http://127.0.0.1:8080/yuqiqi/redisZsetTest/getZSetFromRedis
以上是 Redis有序集合简单测试 的全部内容, 来源链接: utcz.com/z/516456.html