Spring中使用RedisTemplate操作Redis(spring(11)

使用: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<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("destZset11",0,-1);
      Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
      while (iterator.hasNext())
      {
          ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
          System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
      }
结果:value:zset-1score:2.0
value:zset-2score:4.0
value:zset-3score:6.0
value:zset-5score:7.0
value:zset-4score:12.0

•Long unionAndStore(K key, Collection<K> otherKeys, K destKey);
计算给定的多个有序集的并集,并存储在新的 destKey中

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8f96c330d6318f32beb9e4557005b934.html