1.去重
package Data; import redis.clients.jedis.Jedis; public class TestRedisUniq { public static Jedis jd = new Jedis("localhost", 6379); public static void main(String[] args) { // TODO Auto-generated method stub jd.sadd("uniqset", "aa"); jd.sadd("uniqset", "a1"); jd.sadd("uniqset", "aa"); jd.sadd("uniqset", "a2"); jd.sadd("uniqset", "aa"); jd.sadd("uniqset", "a2"); System.out.println(jd.smembers("uniqset")); } }
运行结果:
2.获取前n名数据
package Data; import redis.clients.jedis.Jedis; public class TestRedisTopN { public static Jedis jd = new Jedis("localhost", 6379); public static void main(String[] args) { jd.zadd("topn", 1, "m1"); jd.zadd("topn", 2, "m2"); jd.zadd("topn", 11, "m3"); jd.zadd("topn", 12, "m4"); jd.zadd("topn", 14, "m5"); jd.zadd("topn", 15, "m6"); jd.zadd("topn", 151, "m1"); jd.zadd("topn", 66, "m2"); jd.zadd("topn", 3, "m7"); //获取前5名 System.out.println(jd.zrevrange("topn", 0, 4)); } }
运行结果:
3.获取后n条数据
import redis.clients.jedis.Jedis; public class TestRedisLastN { public static Jedis jd = new Jedis("localhost", 6379); public static void main(String[] args) { jd.del("nlist"); lpush("nlist", "a1"); lpush("nlist", "a2"); lpush("nlist", "a3"); lpush("nlist", "a4"); lpush("nlist", "a5"); System.out.println(queryTop100("nlist")); } public static void lpush(String key, String value) { jd.lpush(key, value); jd.ltrim(key, 0, 2); } public static List<String> queryTop100(String key) { return jd.lrange(key, 0, 2); } }
运行结果:
4.实现队列
package Data; import redis.clients.jedis.Jedis; public class TestRedisQueue { public static Jedis jd = new Jedis("localhost", 6379); public static void main(String[] args) { jd.del("queueList"); jd.rpush("queueList", "1"); jd.rpush("queueList", "2"); jd.lpop("queueList"); System.out.println(jd.lrange("queueList", 0, -1)); } }