码迷,mamicode.com
首页 > 编程语言 > 详细

Spring RedisTemplate操作-ZSet操作(6)

时间:2017-04-25 11:41:08      阅读:2811      评论:0      收藏:0      [点我收藏+]

标签:void   lte   ever   spring   cut   ops   cep   color   add   

@Autowired
    @Resource(name="redisTemplate")
    private RedisTemplate<String, String> rt;
    
    public void flushdb(){
        rt.execute(new RedisCallback<Object>() {
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                connection.flushDb();
                return "ok";
            }
        });
    }
    
    public void set(){
        flushdb();
        ZSetOperations<String, String> vo = rt.opsForZSet();
        vo.add("book", "a", 1d);
        vo.add("book", "b", 2d);
        Set<TypedTuple<String>> tuples = new HashSet<>();
        TypedTuple<String> tuple0 = new DefaultTypedTuple<String>("c", 3d);  
        tuples.add(tuple0);
        TypedTuple<String> tuple1 = new DefaultTypedTuple<String>("d", 4d);  
        tuples.add(tuple1);
        TypedTuple<String> tuple2 = new DefaultTypedTuple<String>("e", 5d);  
        tuples.add(tuple2);
        
        vo.add("book", tuples);
        
        vo.incrementScore("book", "e", 1d);
        
        vo.add("book", "f", 7);
        vo.add("book", "g", 8);
        vo.add("book", "h", 9);
        
//        vo.remove("book", "g","h");
        vo.removeRange("book", 7, 8);
        vo.removeRangeByScore("book", 8, 9);
    }
    
    public void get(){
        ZSetOperations<String, String> vo = rt.opsForZSet();
        out(vo.score("book", "e"));
        out(vo.rank("book", "e"));
        out(vo.reverseRank("book", "e"));
        out(vo.zCard("book"));
        out(vo.range("book", 0, -1));
//        out(vo.rangeWithScores("book", 0, -1));
        out(vo.count("book", 2, 4)); //包含的
        out(vo.rangeByLex("book", Range.range().lte("f").gte("b")));
        out(vo.rangeByLex("book", Range.range(), Limit.limit().count(2).offset(0)));
        out(vo.rangeByLex("book", Range.range(), Limit.limit().count(2).offset(2)));
        out(vo.rangeByScore("book", 2, 4));
        out(vo.rangeByScore("book", 2, 4, 0, 2));
//        out(vo.rangeByScoreWithScores("book", 2, 4));
//        out(vo.rangeByScoreWithScores("book", 2, 4, 0, 2));
        
        out(vo.reverseRange("book", 0, -1));
        out(vo.reverseRangeByScore("book", 2, 4));
        out(vo.reverseRangeByScore("book", 2, 4, 0, 2));
        
        
    }
    
    public void out(String str){
        System.out.println(str);
    }
    public void out(Collection list){
        System.out.println(list);
    }
    public void out(long num){
        System.out.println(num);
    }
    public void out(boolean num){
        System.out.println(num);
    }
    public void out(DataType num){
        System.out.println(num);
    }
    public void out(Object str){
        System.out.println(str);
    }
    public void out(Set<TypedTuple<String>> set){
        for(TypedTuple<String> t : set){
            System.out.println(t.getValue()+"---"+t.getScore());
        }
    }

 

Spring RedisTemplate操作-ZSet操作(6)

标签:void   lte   ever   spring   cut   ops   cep   color   add   

原文地址:http://www.cnblogs.com/aoeiuv/p/6760759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!