济宁做网站哪家比较好,在线crm,百度官方官网,网站建设价格标准案例jedis使用 api在上一篇文章中#xff0c;我们开始研究Jedis API和Java Redis Client。 在这篇文章中#xff0c;我们将研究Sorted Set#xff08;zsets#xff09;。 Sorted Set的工作方式类似于Set#xff0c;它不允许重复的值。 最大的区别是#xff0c;在“排序集”中… jedis使用 api 在上一篇文章中我们开始研究Jedis API和Java Redis Client。 在这篇文章中我们将研究Sorted Setzsets。 Sorted Set的工作方式类似于Set它不允许重复的值。 最大的区别是在“排序集”中每个元素都有一个分数以便保持元素的排序。 我们可以在下面看到一些命令 import java.util.HashMap;
import java.util.Map;import redis.clients.jedis.Jedis;
public class TestJedis {public static void main(String[] args) {String key mostUsedLanguages;Jedis jedis new Jedis(localhost);//Adding a value with score to the setjedis.zadd(key,100,Java);//ZADD//We could add more than one value in one callingMapDouble, String scoreMembers new HashMapDouble, String();scoreMembers.put(90d, Python);scoreMembers.put(80d, Javascript);jedis.zadd(key, scoreMembers);//We could get the score for a memberSystem.out.println(Number of Java users: jedis.zscore(key, Java));//We could get the number of elements on the setSystem.out.println(Number of elements: jedis.zcard(key));//ZCARD}
} 在上面的示例中我们看到了一些Zset命令。 为了将元素添加到zet中我们设置了zadd方法不同之处在于我们还传递了该元素的得分。 有一个重载版本我们可以使用映射传递许多值。 zadd可用于添加和更新现有元素的分数。 我们可以使用zcard和zcard命令获得给定元素的分数和元素数量。 下面我们可以看到zsets的其他命令 import java.util.Set;import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
public class TestJedis {public static void main(String[] args) {String key mostUsedLanguages;Jedis jedis new Jedis(localhost);//get all the elements sorted from bottom to topSystem.out.println(jedis.zrange(key, 0, -1));//get all the elements sorted from top to bottomSystem.out.println(jedis.zrevrange(key, 0, -1));//We could get the elements with the associated scoreSetTuple elements jedis.zrevrangeWithScores(key, 0, -1);for(Tuple tuple: elements){System.out.println(tuple.getElement() - tuple.getScore());}//We can increment a score for a element using ZINCRBYSystem.out.println(Score before zincrby: jedis.zscore(key, Python));//Incrementing the element scorejedis.zincrby(key, 1, Python);System.out.println(Score after zincrby: jedis.zscore(key, Python));}
} 使用zrange我们可以获取给定范围的元素。 它返回从下到上排序的元素。 我们可以使用zrevrrange方法从上到下获取元素。 Redis还允许我们获取具有相关分数的元素。 在redis中我们传递选项“ withscores ”。 通过Jedis API我们使用方法zrevrangeWithScores返回一个元组对象集。 其他有用的命令是zincrby 我们可以增加集合中某个成员的分数。 zsets还有其他命令本文仅旨在显示Jedis API的一些基本用法。 我们可以在这篇文章中找到排序集的好用例。 下篇再见。 参考 XICO JUNIORS WEBLOG博客中的JCG合作伙伴 Francisco Ribeiro Junior 结合使用带有Jedis API的排序集 。 翻译自: https://www.javacodegeeks.com/2013/11/using-sorted-sets-with-jedis-api.htmljedis使用 api