Redis缓存实现数据排名

1.编写一个数据使用的次数方法
/**
 * @Author: wangHui
 * @Dscription:
 * @Date: Created in 19:29 2018/6/8
 **/
public class PlayTest {
    public static void main(String[] args) throws InterruptedException {
        Jedis jedis = new Jedis("127.0.0.1",6379);
        Random random = new Random();
        String[] lolHero = {"亚索","刀妹","泰隆","","","小狗"};
        while (true){
            int index = random.nextInt(lolHero.length);
            String hero = lolHero[index];
            Thread.sleep(2000);
            jedis.zincrby("lol:hero:jsv",1,hero);
            System.out.println(hero + "-----出场了-----");
        }
    }


}
2.查看每个数据被使用的次数以及数据的得分排名
 
 
/**
 * @Author: wangHui
 * @Dscription:
 * @Date: Created in 19:50 2018/6/8
 **/
public class RankTest {
    public static void main(String[] args) throws Exception{
        Jedis jedis = new Jedis("127.0.0.1",6379);
        int i = 1;
        while (true){
            Thread.sleep(3000);
            System.out.println(""+i+"次查看");
            Set<Tuple> heroRanks = jedis.zrevrangeWithScores("lol:hero:jsv",0,5);
            for (Tuple t : heroRanks) {
                System.out.println(t.getElement()+"------"+t.getScore());
            }
            i++;
            System.out.println();
            System.out.println();
            System.out.println();
            System.out.println();

        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40584932/article/details/80652772