Redis的ZSet数据结构

Redis 有序集合和无序集合一样也是string类型元素的集合,且不允许重复的成员。

不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。

有序集合的成员是唯一的,但分数(score)却可以重复。

public interface ZSetOperations

ZSetOperations提供了一系列方法对有序集合进行操作:

Boolean add(K key, V value, double score);

新增一个有序集合,存在的话为false,不存在的话为true

使用:System.out.println(template.opsForZSet().add("zset1","zset-1",1.0));

结果:true

Long add(K key, Set> tuples);

新增一个有序集合

使用:ZSetOperations.TypedTuple objectTypedTuple1 = new DefaultTypedTuple("zset-5",9.6);

ZSetOperations.TypedTuple objectTypedTuple2 = new DefaultTypedTuple("zset-6",9.9);

Set> tuples = new HashSet>();

tuples.add(objectTypedTuple1);

tuples.add(objectTypedTuple2);

System.out.println(template.opsForZSet().add("zset1",tuples));

System.out.println(template.opsForZSet().range("zset1",0,-1));

结果:[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]

Long remove(K key, Object... values);

从有序集合中移除一个或者多个元素

使用:System.out.println(template.opsForZSet().range("zset1",0,-1));

System.out.println(template.opsForZSet().remove("zset1","zset-6"));

System.out.println(template.opsForZSet().range("zset1",0,-1));

结果:[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]1[zset-1, zset-2, zset-3, zset-4, zset-5]

Double incrementScore(K key, V value, double delta);

增加元素的score值,并返回增加后的值

使用:System.out.println(template.opsForZSet().incrementScore("zset1","zset-1",1.1)); //原为1.1

结果:2.2

Long rank(K key, Object o);

返回有序集中指定成员的排名,其中有序集成员按分数值递增(从小到大)顺序排列

使用:System.out.println(template.opsForZSet().range("zset1",0,-1));

System.out.println(template.opsForZSet().rank("zset1","zset-2"));

结果:[zset-2, zset-1, zset-3, zset-4, zset-5]0 //表明排名第一

Long reverseRank(K key, Object o);

返回有序集中指定成员的排名,其中有序集成员按分数值递减(从大到小)顺序排列

使用:System.out.println(template.opsForZSet().range("zset1",0,-1));

System.out.println(template.opsForZSet().reverseRank("zset1","zset-2"));

结果:[zset-2, zset-1, zset-3, zset-4, zset-5]4 //递减之后排到第五位去了

Set range(K key, long start, long end);

通过索引区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递增(从小到大)顺序排列

使用:System.out.println(template.opsForZSet().range("zset1",0,-1));

结果:[zset-2, zset-1, zset-3, zset-4, zset-5]

Set> rangeWithScores(K key, long start, long end);

通过索引区间返回有序集合成指定区间内的成员对象,其中有序集成员按分数值递增(从小到大)顺序排列

使用:Set> tuples = template.opsForZSet().rangeWithScores("zset1",0,-1);

Iterator> iterator =tuples.iterator();while(iterator.hasNext())

{

ZSetOperations.TypedTuple typedTuple =iterator.next();

System.out.println("value:" + typedTuple.getValue() + "score:" +typedTuple.getScore());

}

结果:value:zset-2score:1.2value:zset-1score:2.2value:zset-3score:2.3value:zset-4score:6.6value:zset-5score:9.6

Set rangeByScore(K key, double min, double max);

通过分数返回有序集合指定区间内的成员,其中有序集成员按分数值递增(从小到大)顺序排列

使用:System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));

结果:[zset-2, zset-1, zset-3]

Set> rangeByScoreWithScores(K key, double min, double max);

通过分数返回有序集合指定区间内的成员对象,其中有序集成员按分数值递增(从小到大)顺序排列

使用:Set> tuples = template.opsForZSet().rangeByScoreWithScores("zset1",0,5);

Iterator> iterator =tuples.iterator();while(iterator.hasNext())

{

ZSetOperations.TypedTuple typedTuple =iterator.next();

System.out.println("value:" + typedTuple.getValue() + "score:" +typedTuple.getScore());

}

结果:value:zset-2score:1.2value:zset-1score:2.2value:zset-3score:2.3

Set rangeByScore(K key, double min, double max, long offset, long count);

通过分数返回有序集合指定区间内的成员,并在索引范围内,其中有序集成员按分数值递增(从小到大)顺序排列

使用: System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));

System.out.println(template.opsForZSet().rangeByScore("zset1",0,5,1,2));

结果:[zset-2, zset-1, zset-3]

[zset-1, zset-3]

Set> rangeByScoreWithScores(K key, double min, double max, long offset, long count);

通过分数返回有序集合指定区间内的成员对象,并在索引范围内,其中有序集成员按分数值递增(从小到大)顺序排列

使用:Set> tuples = template.opsForZSet().rangeByScoreWithScores("zset1",0,5,1,2);

Iterator> iterator =tuples.iterator();while(iterator.hasNext())

{

ZSetOperations.TypedTuple typedTuple =iterator.next();

System.out.println("value:" + typedTuple.getValue() + "score:" +typedTuple.getScore());

}

结果:value:zset-1score:2.2value:zset-3score:2.3

Set reverseRange(K key, long start, long end);

通过索引区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递减(从大到小)顺序排列

使用:System.out.println(template.opsForZSet().reverseRange("zset1",0,-1));

结果:[zset-5, zset-4, zset-3, zset-1, zset-2]

Set> reverseRangeWithScores(K key, long start, long end);

通过索引区间返回有序集合成指定区间内的成员对象,其中有序集成员按分数值递减(从大到小)顺序排列

使用:Set> tuples = template.opsForZSet().reverseRangeWithScores("zset1",0,-1);

Iterator> iterator =tuples.iterator();while(iterator.hasNext())

{

ZSetOperations.TypedTuple typedTuple =iterator.next();

System.out.println("value:" + typedTuple.getValue() + "score:" +typedTuple.getScore());

}

结果:value:zset-5score:9.6value:zset-4score:6.6value:zset-3score:2.3value:zset-1score:2.2value:zset-2score:1.2

Set reverseRangeByScore(K key, double min, double max);

使用:与rangeByScore调用方法一样,其中有序集成员按分数值递减(从大到小)顺序排列

Set> reverseRangeByScoreWithScores(K key, double min, double max);

使用:与rangeByScoreWithScores调用方法一样,其中有序集成员按分数值递减(从大到小)顺序排列

Set reverseRangeByScore(K key, double min, double max, long offset, long count);

使用:与rangeByScore调用方法一样,其中有序集成员按分数值递减(从大到小)顺序排列

Set> reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count);

使用:与rangeByScoreWithScores调用方法一样,其中有序集成员按分数值递减(从大到小)顺序排列

Long count(K key, double min, double max);

通过分数返回有序集合指定区间内的成员个数

使用:System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));

System.out.println(template.opsForZSet().count("zset1",0,5));

结果:[zset-2, zset-1, zset-3]3

Long size(K key);

获取有序集合的成员数,内部调用的就是zCard方法

使用:System.out.println(template.opsForZSet().size("zset1"));

结果:6

Long zCard(K key);

获取有序集合的成员数

使用:System.out.println(template.opsForZSet().zCard("zset1"));

结果:6

Double score(K key, Object o);

获取指定成员的score值

使用:System.out.println(template.opsForZSet().score("zset1","zset-1"));

结果:2.2

Long removeRange(K key, long start, long end);

移除指定索引位置的成员,其中有序集成员按分数值递增(从小到大)顺序排列

使用:System.out.println(template.opsForZSet().range("zset2",0,-1));

System.out.println(template.opsForZSet().removeRange("zset2",1,2));

System.out.println(template.opsForZSet().range("zset2",0,-1));

结果:[zset-1, zset-2, zset-3, zset-4]2[zset-1, zset-4]

Long removeRangeByScore(K key, double min, double max);

根据指定的score值得范围来移除成员

使用://System.out.println(template.opsForZSet().add("zset2","zset-1",1.1));//System.out.println(template.opsForZSet().add("zset2","zset-2",1.2));//System.out.println(template.opsForZSet().add("zset2","zset-3",2.3));//System.out.println(template.opsForZSet().add("zset2","zset-4",6.6));

System.out.println(template.opsForZSet().range("zset2",0,-1));

System.out.println(template.opsForZSet().removeRangeByScore("zset2",2,3));

System.out.println(template.opsForZSet().range("zset2",0,-1));

结果:[zset-1, zset-2, zset-3,zset-4]1[zset-1, zset-2, zset-4]

Long unionAndStore(K key, K otherKey, K destKey);

计算给定的一个有序集的并集,并存储在新的 destKey中,key相同的话会把score值相加

使用:System.out.println(template.opsForZSet().add("zzset1","zset-1",1.0));

System.out.println(template.opsForZSet().add("zzset1","zset-2",2.0));

System.out.println(template.opsForZSet().add("zzset1","zset-3",3.0));

System.out.println(template.opsForZSet().add("zzset1","zset-4",6.0));

System.out.println(template.opsForZSet().add("zzset2","zset-1",1.0));

System.out.println(template.opsForZSet().add("zzset2","zset-2",2.0));

System.out.println(template.opsForZSet().add("zzset2","zset-3",3.0));

System.out.println(template.opsForZSet().add("zzset2","zset-4",6.0));

System.out.println(template.opsForZSet().add("zzset2","zset-5",7.0));

System.out.println(template.opsForZSet().unionAndStore("zzset1","zzset2","destZset11"));

Set> tuples = template.opsForZSet().rangeWithScores("destZset11",0,-1);

Iterator> iterator =tuples.iterator();while(iterator.hasNext())

{

ZSetOperations.TypedTuple typedTuple =iterator.next();

System.out.println("value:" + typedTuple.getValue() + "score:" +typedTuple.getScore());

}

结果:value:zset-1score:2.0value:zset-2score:4.0value:zset-3score:6.0value:zset-5score:7.0value:zset-4score:12.0

Long unionAndStore(K key, Collection otherKeys, K destKey);

计算给定的多个有序集的并集,并存储在新的 destKey中

使用://System.out.println(template.opsForZSet().add("zzset1","zset-1",1.0));//System.out.println(template.opsForZSet().add("zzset1","zset-2",2.0));//System.out.println(template.opsForZSet().add("zzset1","zset-3",3.0));//System.out.println(template.opsForZSet().add("zzset1","zset-4",6.0));//

//System.out.println(template.opsForZSet().add("zzset2","zset-1",1.0));//System.out.println(template.opsForZSet().add("zzset2","zset-2",2.0));//System.out.println(template.opsForZSet().add("zzset2","zset-3",3.0));//System.out.println(template.opsForZSet().add("zzset2","zset-4",6.0));//System.out.println(template.opsForZSet().add("zzset2","zset-5",7.0));

System.out.println(template.opsForZSet().add("zzset3","zset-1",1.0));

System.out.println(template.opsForZSet().add("zzset3","zset-2",2.0));

System.out.println(template.opsForZSet().add("zzset3","zset-3",3.0));

System.out.println(template.opsForZSet().add("zzset3","zset-4",6.0));

System.out.println(template.opsForZSet().add("zzset3","zset-5",7.0));

List stringList = new ArrayList();

stringList.add("zzset2");

stringList.add("zzset3");

System.out.println(template.opsForZSet().unionAndStore("zzset1",stringList,"destZset22"));

Set> tuples = template.opsForZSet().rangeWithScores("destZset22",0,-1);

Iterator> iterator =tuples.iterator();while(iterator.hasNext())

{

ZSetOperations.TypedTuple typedTuple =iterator.next();

System.out.println("value:" + typedTuple.getValue() + "score:" +typedTuple.getScore());

}

结果:value:zset-1score:3.0value:zset-2score:6.0value:zset-3score:9.0value:zset-5score:14.0value:zset-4score:18.0

Long intersectAndStore(K key, K otherKey, K destKey);

计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中

使用:System.out.println(template.opsForZSet().intersectAndStore("zzset1","zzset2","destZset33"));

Set> tuples = template.opsForZSet().rangeWithScores("destZset33",0,-1);

Iterator> iterator =tuples.iterator();while(iterator.hasNext())

{

ZSetOperations.TypedTuple typedTuple =iterator.next();

System.out.println("value:" + typedTuple.getValue() + "score:" +typedTuple.getScore());

}

结果:value:zset-1score:2.0value:zset-2score:4.0value:zset-3score:6.0value:zset-4score:12.0

Long intersectAndStore(K key, Collection otherKeys, K destKey);

计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中

使用:List stringList = new ArrayList();

stringList.add("zzset2");

stringList.add("zzset3");

System.out.println(template.opsForZSet().intersectAndStore("zzset1",stringList,"destZset44"));

Set> tuples = template.opsForZSet().rangeWithScores("destZset44",0,-1);

Iterator> iterator =tuples.iterator();while(iterator.hasNext())

{

ZSetOperations.TypedTuple typedTuple =iterator.next();

System.out.println("value:" + typedTuple.getValue() + "score:" +typedTuple.getScore());

}

结果:value:zset-1score:3.0value:zset-2score:6.0value:zset-3score:9.0value:zset-4score:18.0

Cursor> scan(K key, ScanOptions options);

遍历zset

使用: Cursor> cursor = template.opsForZSet().scan("zzset1", ScanOptions.NONE);while(cursor.hasNext()){

ZSetOperations.TypedTuple item =cursor.next();

System.out.println(item.getValue()+ ":" +item.getScore());

}

结果:zset-1:1.0zset-2:2.0zset-3:3.0zset-4:6.0

注:TimeUnit是java.util.concurrent包下面的一个类,表示给定单元粒度的时间段

常用的颗粒度

TimeUnit.DAYS //天

TimeUnit.HOURS //小时

TimeUnit.MINUTES //分钟

TimeUnit.SECONDS //秒

TimeUnit.MILLISECONDS //毫秒

zset 怎么get_如何使用RedisTemplate访问Redis数据结构之Zset相关推荐

  1. 如何使用RedisTemplate访问Redis数据结构-记录

    Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集 ...

  2. redistemplate怎么修改数据_如何使用RedisTemplate访问Redis数据结构?

    在springboot项目中,集成各种框架变得非常容易.下面简单介绍一下如何在springboot项目中集成单机模式redis.集群模式也差不多,这里就不过多介绍了. 首先你得安装redis服务,无论 ...

  3. Redis数据结构之Zset

    文章目录 一.zset数据结构 二.跳表skipList 什么是跳表? 1.跳表的查找 2.跳表的插入 3.跳表的删除 4.跳表的更新 一.zset数据结构 相比于set,sorted set 增加了 ...

  4. 【过程记录】springboot整合redis/分别用redisRepository和redistemplate操作redis

    导入依赖 基本配置 使用RedisTemplate访问redis 使用Redisrepository访问redis 实例: 导入依赖 菜单大部分情况下不会出现变化,我们可以将其放入Redis 加快加载 ...

  5. zset 怎么get_使用redis的zset实现高效分页查询(附完整代码)

    一.需求 移动端系统里有用户和文章,文章可设置权限对部分用户开放.现要实现的功能是,用户浏览自己能看的最新文章,并可以上滑分页查看. 二.数据库表设计 涉及到的数据库表有:用户表TbUser.文章表T ...

  6. RedisTemplate操作redis五大类型用法详解(springboot整合redis版本)

    1.案例说明 springboot整合redis之后,提供了操作redis的简便方式 通过通用对象redisTemplate方式操作String,Hash,List,Set SortSet五大数据类型 ...

  7. Spring中使用RedisTemplate操作Redis(spring-data-redis)

    Redis 数据结构简介 Redis可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集合 ...

  8. spring-data-redis 中使用RedisTemplate操作Redis

    Redis 数据结构简介 Redis可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集合 ...

  9. Springboot2.0访问Redis集群

    Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作高性能的key-value数据库.缓存和消息中间件,掌握它是程序员的必备技能,下面是一个springboot访问redis的 ...

最新文章

  1. Unity协程截图,WWWForm、WWW配合上传
  2. 揭秘企业级web负载均衡完美架构(转载)
  3. Android框架攻击之Fragment注入
  4. java中打印输出数组内容的三种方式
  5. python学习——把计算GC含量的代码封装成函数
  6. 解读鸿蒙轻内核的监控器:异常钩子函数
  7. ubuntu 安装使用多版本opencv
  8. Arcgis创建SDE_Geometry、SDO_Geometry的区别
  9. java常见数据算法_冒泡排序
  10. c 子类对象 访问父类对象受保护成员_面向对象三大特征: 继承
  11. 基于Maven的S2SH(Struts2+Spring+Hibernate)框架搭建
  12. Android开发-仿网易云音乐播放器样式设计与实现
  13. OpenCV50:使用SVM完成OCR手写体识别
  14. 【io】io等待为什么引发cpu过高?
  15. PCB制造常用的13种测试方法,你了解几种?
  16. python能做界面吗_如何使用pyQT做pythonGUI界面|
  17. 待办工作是什么意思?
  18. Login.aspx
  19. 20本英文书点亮你的人生智慧
  20. 如何利用OA优化企业的采购招投标流程

热门文章

  1. 2018年一季度:每卖出4台手机有一台是华为,小米销量三连涨
  2. Open CV入门教程--电子书高清免费下载
  3. 什么是Spring Cloud
  4. axure 如何导入excel,Axure7.0如何用中继器导入Excel信息?
  5. Cesium指南针(含源代码和插件)
  6. 机器学习基础概念及简单框架
  7. c语言拼图验证码编写,java实现拼图验证码
  8. JavaWeb(四)——MyBatis(持久层框架)
  9. OPPO Enco Air真无线耳机不仅内在表现出色 外在也“有点料”
  10. 【python】使用requests多线程爬取论坛文章并存储在sqlite中