1. 程式人生 > >redis排序和計數應用

redis排序和計數應用

  System.out.println("測試");
  System.out.println("----------------Tests main-----------------");
  Jedis jedis = new Jedis("127.0.0.1",6379);
  jedis.auth("123456");
  //統計計數
  jedis.decr("newId");
  Long i=jedis.incr("newId");
  i=jedis.incrBy("newId",1);
  System.out.println(i);
  i=jedis.decr("newId");
  System.out.println(i);

  // 有序集合排序 字串型別 單個
  zAdd("news",99,"張三",jedis);
  jedis.zadd("news",100,"李四");
  jedis.zadd("news",30,"王五");
  jedis.zadd("news",10,"趙劉");
  System.out.println(jedis.zrevrange("news",0,2));
  System.out.println(jedis.zrank("news","王五"));
  System.out.println(jedis.zrevrange("news",jedis.zrank("news","王五"),jedis.zrank("news","王五")+2));

  // 陣列轉字串
  People people=new People("張三",23,98);
  new CacheTimer().zAddClass("ne",98,people,jedis);
  //people.setX(new Integer(98));
  People people2=new People("李四",29,97);
  People people3=new People("王五",28,100);
  People people4=new People("李六",43,67);
  People people5=new People("老七",34,53);
  People people6=new People("老八",21,87);

  //Object o= ObjectPeople;
//  jedis.zadd("ne".getBytes(),people.getGrade(),JSONObject.toJSON(people).toString().getBytes());
  jedis.zadd("ne".getBytes(),people2.getGrade(),JSONObject.toJSON(people2).toString().getBytes());
  jedis.zadd("ne".getBytes(),people3.getGrade(),JSONObject.toJSON(people3).toString().getBytes());
  jedis.zadd("ne".getBytes(),people4.getGrade(),JSONObject.toJSON(people4).toString().getBytes());
  jedis.zadd("ne".getBytes(),people5.getGrade(),JSONObject.toJSON(people5).toString().getBytes());
  jedis.zadd("ne".getBytes(),people6.getGrade(),JSONObject.toJSON(people6).toString().getBytes());

 // jedis.zrevrange("ne",0,2);

  System.out.println(jedis.zrevrange("ne",0,2));
  Set<String> strings=jedis.zrevrange("ne",0,2);
  List<People> peopleList=new ArrayList<People>();
  for (String string:strings){
      System.out.println(string);
     People people1= JSONObject.parseObject(string,CacheTimer.People.class)  ;
     peopleList.add(people1);
  }

  for (People p1:peopleList){
      System.out.println(p1.getName());
  }
  jedis.close();

}

public  void zAddClass(String key,int score, Object t, Jedis jedis){
    jedis.zadd(key.getBytes(),score, JSONObject.toJSON(t).toString().getBytes());
}
public static void zAdd(String key,int score,String t, Jedis jedis){
    jedis.zadd(key,score, t);
}
public List<T> zrevrange(String key,long start,long end,T t,Jedis jedis){
    List<T> tList=new ArrayList<T>();
    Set<byte[]> strings=jedis.zrevrange(key.getBytes(),start,end);
    for (byte[] string:strings){
        T t1= JSONObject.parseObject( new String(string),t.getClass())  ;
        tList.add(t1);
    }
    return tList;
}
static class People  implements Serializable{
     private String name;
     private Integer age;
     private  Integer grade;



    public People() {}

    public People(String name, Integer age, Integer grade) {
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public Integer getAge() {
         return age;
     }

     public void setAge(Integer age) {
         this.age = age;
     }

     public Integer getGrade() {
         return grade;
     }

     public void setGrade(Integer grade) {
         this.grade = grade;
     }

   /* @Override
    public String toString() {
        return "name"+name+" ,age"+age+",grade"+grade;
    }*/
}