package com.jiedistest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.tools.JedisDemo;
import redis.clients.jedis.Jedis;
public class JedisZsort {
static JedisDemo jd=new JedisDemo();
;public static void main(String[] args) {
Map map=new HashMap();
Jedis js=jd.getConn();
js.auth("199633");
//清空数据
js.flushDB();
//存放数据
js.zadd("car",1,"路虎" );
js.zadd("car",2,"奔驰" );
js.zadd("car",3,"宝马" );
js.zadd("car",4,"法拉利" );
js.zadd("car",5,"兰博基尼" );
js.zadd("car",6,"宾利" );
js.zadd("car",7,"林肯" );
System.out.println(js.zrange("car", 0, -1));
js.zadd("car",8,"布加迪" );
//返回有序集key中,指定区间内的成员(从小到大)
Set keys=js.zrange("car", 0, 4);
System.out.println(keys);
//返回有序集key中,指定区间内的成员(从大到小)
Set keyss=js.zrevrange("car", 0, 4);
System.out.println(keyss);
//返回元素个数 int
System.out.println(js.zcard("car"));
//返回元素下标 Double
System.out.println(js.zscore("car", "林肯"));
//删除元素
System.out.println(js.zrem("car", "奔驰"));
//score在0-100的总数
System.out.println(js.zcount("car", 0, 100));
//score+指定量
System.out.println(js.zincrby("car", 100, "宝马"));
//score在0-100内的值
System.out.println(js.zrangeByScore("car", 0, 100));
//看排名
System.out.println(js.zrank("car", "宝马"));
//输出整个集合值
System.out.println(js.zrange("car", 0, -1));
js.close();
}
}
package com.jiedistest;
import com.tools.JedisDemo;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.SortingParams;
public class SortDemo {
static JedisDemo jd=new JedisDemo();
static Jedis js=jd.getConn();
public static void main(String[] args) {
js.auth("199633");
js.flushDB();
js.lpush("sort", "777","5555","42","33");
js.lpush("English", "b","g","a","f","c");
js.rpush("rank", "1","3","5","7","9");
js.rpush("rank", "2","4","6","8");
System.out.println(js.lrange("rank", 0, -1));
System.out.println(js.lrange("English", 0, -1));
System.out.println(js.lrange("sort", 0, -1));
SortingParams sort=new SortingParams();
//按升序
System.out.println(js.sort("sort",sort.asc()));
//按降序
System.out.println(js.sort("sort",sort.desc()));
//按英文字母顺序排序
System.out.println(js.sort("English",sort.alpha()));
//LIMIT指定数量降序
System.out.println(js.sort("rank",sort.limit(0, 7).desc()));
js.close();
}
}
还有GET和BY排序,是按照储存的元素进行排序,例如id,level…都可以排序,两者搭配更加直观一点;
如果不想一次一次麻烦的调用排序方法,可以设置生命周期的方式进行保存,避免复杂;