Simple implementation of the sorting function in Redis cache

Source: Internet
Author: User
Tags redis

Edis is the official preferred Java client development package for Redis. In this article we will describe how to use the Sorted set sort collection (Zsets).

Sorted set is the same as a collection, it does not have duplicate values, the biggest difference is that each element in the Sorted set is sorted.

Let's look at some of the commands first:

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 set
     Jedis.zadd (key,100, "Java");//zadd//we could add more than one value in one
     Calling
     map<double, string> scoremembers = new hashmap<double, string> ();
     Scoremembers.put (90d, "Python");
     Scoremembers.put (80d, "Javascript");
     Jedis.zadd (key, scoremembers);
     We could get
     the ' score for a System.out.println ("Number of Java Users:" + jedis.zscore (Key, "Java"));
     We could get the number of elements on the set
     System.out.println ("Number of elements:" + Jedis.zcard (key));//zcard

     }

}

In the example above we see the Zset command, in order to add elements to the Zet, we use the Zadd method, and the difference is that we pass a score value for an element, we can use the Map object to pass many objects at once, and the Zadd method can be used to add and update the scoring value of an existing element.

We can use Zscore to get the score of an element and to get the number of elements by Zcard.

For the following example, we can see other commands from Zsets:

01
Import Java.util.Set;
02

03
Import Redis.clients.jedis.Jedis;
04
Import Redis.clients.jedis.Tuple;
05
public class Testjedis {
06

07
public static void Main (string[] args) {
08
String key = "Mostusedlanguages";
09
Jedis Jedis = new Jedis ("localhost");
10

11
Get all the elements sorted from bottom to top
12
System.out.println (Jedis.zrange (key, 0,-1));
13

14
Get all the elements sorted from top to bottom
15
System.out.println (Jedis.zrevrange (key, 0,-1));
16
We could get the elements with the associated score
17
Set elements = jedis.zrevrangewithscores (key, 0,-1);
18
for (Tuple tuple:elements) {
19
System.out.println (tuple.getelement () + "-" + Tuple.getscore ());
20
}
21st

22
We can increment a score for a element using Zincrby
23
System.out.println ("Score before Zincrby:" + jedis.zscore (Key, "Python"));
24
Incrementing the element score
25
Jedis.zincrby (Key, 1, "Python");
26
System.out.println ("Score after Zincrby:" + jedis.zscore (Key, "Python"));
27
}
28
}
By Zrange we can get the elements of a given range, it will return the sorted list of elements (from the bottom up), or you can get a list of top and bottom elements by Zrevrrange. Redis also allows us to get the element through the associated score, passing the "withscores" argument. Use the Zrevrangewithscores method of the Jedis API to return the collection of objects. Another useful command is that Zincrby can be used to increase the element's scoring value.

There are other commands for

Zsets, and here we are just introducing some basic usage related to the Jedis API. There are also a few introductions to sorting collections.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.