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

使用://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<String> stringList = new ArrayList<String>();
      stringList.add("zzset2");
      stringList.add("zzset3");
      System.out.println(template.opsForZSet().unionAndStore("zzset1",stringList,"destZset22"));

Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("destZset22",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:3.0
value:zset-2score:6.0
value:zset-3score:9.0
value:zset-5score:14.0
value:zset-4score:18.0

•Long intersectAndStore(K key, K otherKey, K destKey);
计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中

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

Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("destZset33",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-4score:12.0

•Long intersectAndStore(K key, Collection<K> otherKeys, K destKey);
计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中

使用:List<String> stringList = new ArrayList<String>();
      stringList.add("zzset2");
      stringList.add("zzset3");
      System.out.println(template.opsForZSet().intersectAndStore("zzset1",stringList,"destZset44"));

Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("destZset44",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:3.0
value:zset-2score:6.0
value:zset-3score:9.0
value:zset-4score:18.0

•Cursor<TypedTuple<V>> scan(K key, ScanOptions options);
遍历zset

使用:    Cursor<ZSetOperations.TypedTuple<Object>> cursor = template.opsForZSet().scan("zzset1", ScanOptions.NONE);
      while (cursor.hasNext()){
          ZSetOperations.TypedTuple<Object> item = cursor.next();
          System.out.println(item.getValue() + ":" + item.getScore());
      }
结果:zset-1:1.0
zset-2:2.0
zset-3:3.0
zset-4:6.0

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

TimeUnit.DAYS //天
TimeUnit.HOURS //小时
TimeUnit.MINUTES //分钟
TimeUnit.SECONDS //秒
TimeUnit.MILLISECONDS //毫秒

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

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

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