Article From:https://www.cnblogs.com/charlypage/p/9063546.html
/**
 * SortedSet(Ordered set)* ZADD, ZCARD, ZCOUNT, ZINCRBY, ZRANGE, ZRANGEBYSCORE, ZRANK, ZREM, ZREMRANGEBYRANK, ZREMRANGEBYSCORE,ZREVRANGE* ZREVRANGEBYSCORE, ZREVRANK, ZSCORE, ZUNIONSTORE, ZINTERSTORE, ZSCAN, ZRANGEBYLEX, ZLEXCOUNT, ZREMRANGEBYLEX*/
public class SortedSetTypeTest {

    private Jedis jedis;

    private static final String KEY = "sorted_set";

    private static final String VALUE = "layman";

    @Before
    public void setUp() {
        this.jedis = new Jedis(new JedisShardInfo("192.168.133.188", 6379));
    }

    /**
     * ZADD key score member [[score member] [score member] ...]
     * One or more member elements and their score values are added to the ordered set key.* if a member is already a member of an ordered set, update the score value of this member and reinsert it.Enter this member element to ensure that the member is in the right place.* the score value can be integer or double precision floating point numbers.* if key does not exist, create an empty ordered set and execute ZADD.Operation.* an error is returned when key exists but is not ordered set type.*/
    @Test
    public void ZADD() {
        Map<String, Double> sourceMember = new HashMap<String, Double>();
        for (int i = 0; i < 3; i++) {
            double score = getRandomInt();
            sourceMember.put(VALUE + score, score);
        }
        jedis.zadd(KEY, sourceMember);
        ZRANGE();
    }

    /**
     * ZCARD key
     * Returns the cardinal number of the ordered set key.*/
    @Test
    public void ZCARD() {
        System.out.println(jedis.zcard(KEY));
    }

    /**
     * ZCOUNT key min max
     * In the ordered set key, the score value is between the min and max (the default includes the number of members of the score value equal to min or max).* detailed usage of parameters min and Max, please.Refer to the ZRANGEBYSCORE command.*/
    @Test
    public void ZCOUNT() {
        System.out.println(jedis.zcount(KEY, "-inf", "+inf"));
        System.out.println(jedis.zcount(KEY, 24, 45));
    }

    /**
     * ZINCRBY key increment member
     * For the ordered set key, the score value of member member is incrementally increased by increment.* by passing a negative value of increment, let score subtract the corresponding value, such as ZINCRB.Y key -5 member is to reduce the score value of member by 5.* when key does not exist, or member is not a member of key, ZINCRBY key increMent member is equivalent to ZADD key increment member.* when key is not an ordered set type, an error is returned.* the score value can be an integer value or a double precision floating-pointNumber.*/
    @Test
    public void ZINCRBY() {
        ZRANGE();
//        jedis.zincrby(KEY, getRandomInt(), VALUE + 10);
//        ZRANGE();
        jedis.zincrby(KEY, 20, VALUE + 10);
        ZRANGE();

    }

    /**
     * ZRANGE key start stop [WITHSCORES]
     * Returns the ordered set key, which specifies the members in the interval.* the positions of members are increased according to the score value (from small to large).* members with the same score value are in dictionary order (lexicographicalOrder) in order to arrange.* if you need members to descend from the score value (from big to small), use the ZREVRANGE command.* subscript parameters start and stop are all at the bottom of 0, too.That is to say, 0 is the first member of an ordered set, and 1 is an ordered set of second members, and so on.* you can also use the negative subscript, with -1 as the last member, -2 as the last second members, and so on.* superOut of range subscript does not cause errors.* for example, when the value of the start is larger than the maximum subscript of the ordered set, or the start > stop, the ZRANGE command simply returns a empty list.*On the other hand, if the stop parameter is larger than the largest subscript of the ordered set, Redis treats stop as the largest subscript.* you can use the WITHSCORES option to allow members and their score.The values are returned together, and the return list is represented in the format of value1, Score1,..., valueN, scoreN.* client libraries may return some more complex data types, such as arrays, tuples, and so on.* <P/>* ZREVRANGE key start stop [WITHSCORES]* returns the ordered set key, specifying members in the interval.* the location of the members is SCThe ore values are diminishing (from large to small) to be arranged.* members with the same score value are arranged in reverse order (reverse lexicographical order).* except for members according to scoreIn the order of decreasing values, the other aspects of the ZREVRANGE command are the same as those of the ZRANGE command.*/
    @Test
    public void ZRANGE() {
//        System.out.println(jedis.zrange(KEY, 0, -1));
        Set<Tuple> tuples = jedis.zrangeWithScores(KEY, 0, -1);
        for (Tuple tuple : tuples) {
            System.out.println(tuple.getElement() + ":" + tuple.getScore());
        }
    }

    /**
     * ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
     * Returns the ordered set key, where all score values are between min and Max, including members equal to min or max. Ordered set members are arranged according to score value (from small to large).* having the same SCMembers of the ore value are arranged in lexicographical order, which is provided by ordered sets and does not require additional computation.* the optional LIMIT parameter specifies the number and interval of the results returned (as in SQL).SELECT LIMIT offset, count), notice that when offset is large, the operation of locating offset may need to traverse the entire ordered set, the worst complexity of this process is the O (N) time.* optionalThe WITHSCORES parameter determines whether the result set is returned to the ordered set alone or returns the ordered set member and its score value together.* this option is available from version Redis 2.* interval and infinity* min and Max can be -inf and +inf, so that you can use ZRANGEBYSCORE such commands without knowing the lowest and maximum score values of the ordered set.* by defaultYou can also use an optional opening interval (less or more) by adding a symbol to the interval (less or less than or equal to or equal to) in a closed interval.* < p/>* ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]* returns the ordered set key, where the score value is between Max and min (default is equal to MA).All members of X or min). Ordered set members are arranged in a sequence of decreasing score values from large to small.* members with the same score value are in reverse order of dictionary order (reverse lexicographical o)Rder) arrangement.* in addition to the ordering of members in descending order of score value, other aspects of ZREVRANGEBYSCORE command are the same as ZRANGEBYSCORE commands.*/
    @Test
    public void ZRANGEBYSCORE() {
        System.out.println(jedis.zrangeByScore(KEY, 0, 100));
        System.out.println(jedis.zrangeByScore(KEY, 0, 100, 8, 15));
        System.out.println(jedis.zrangeByScore(KEY, "-inf", "+inf"));
        System.out.println(jedis.zrangeByScore(KEY, "-inf", "+inf", 8, 1));
        Set<Tuple> tuples = jedis.zrangeByScoreWithScores(KEY, "-inf", "+inf", 8, 1);
        for (Tuple tuple : tuples) {
            System.out.println(tuple.getElement() + "=" + tuple.getScore());
        }
    }

    /**
     * ZRANK key member
     * Returns the ranking of the member member in the ordered set key. Among them, ordered set members are incremented according to score value (from small to large).* ranking is at the bottom of 0, that is to say, the members with the lowest score rank at 0.* use ZREVRANK command to get rank of members descending from score value (from big to small).* < p/>* ZREVRANK key member*Returns the ranking of the member member in the ordered set key. The ordered set members are ranked by score value (from big to small).* the ranking is at the bottom of 0, that is to say, the largest member of score ranks 0.* use the ZRANK command to get members ranking by score value (from small to large).*/
    @Test
    public void ZRANK() {
        System.out.println(jedis.zrank(KEY, "layman56.0"));
    }

    /**
     * ZREM key member [member ...]
     * Remove one or more members in ordered set key, and members that do not exist will be ignored.* an error is returned when key exists but is not ordered set type.*/
    @Test
    public void ZREM() {
        ZRANGE();
        jedis.zrem(KEY, "layman72.0", "layman77.0");
        System.out.println("######################");
        ZRANGE();
    }

    /**
     * ZREMRANGEBYRANK key start stop
     * Remove ordered set key, specifying all members in the rank (rank) interval.* the interval parameters are start and stop respectively, including start and stop.* subscript parameter staBoth RT and stop are based on 0, that is to say, 0 is the first member of an ordered set, and 1 is second sets of ordered sets, and so on.* you can also use the negative subscript to represent the last member in -1, and -2 means the reciprocal.Two members, and so on.*/
    @Test
    public void ZREMRANGEBYRANK() {
        ZRANGE();
        jedis.zremrangeByRank(KEY, 0, 1);
        System.out.println("##################");
        ZRANGE();
    }

    /**
     * ZREMRANGEBYSCORE key min max
     * In order to remove ordered set key, all score values are between min and Max, including members equal to min or max.* since version 2.1.6, score equals to min or max.Members can also be excluded. Please refer to the ZRANGEBYSCORE command for details.*/
    @Test
    public void ZREMRANGEBYSCORE() {
        ZRANGE();
        jedis.zremrangeByScore(KEY, "53", "63");//Delete 53< score< 63
        System.out.println("#####################");
        ZRANGE();
    }

    /**
     * ZSCORE key member
     * Returns the score value of member member in ordered set key.* if the member element is not a member of the ordered set key or key does not exist, it returns nil.*/
    @Test
    public void ZSCORE() {
        System.out.println(jedis.zscore(KEY, "layman84.0"));
    }

    /**
     * ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
     * Compute a given set of one or more ordered sets, where the number of given key must be specified with the numkeys parameter, and the set (result set) is stored to destination.* by default, the SCO of a member in the result set.The re value is the sum of the score value of the member under all given sets.* WEIGHTS* using the WEIGHTS option, you can specify a multiplicative factor for each given ordered set (multiplicaTion factor),* the score value of all members of a given ordered set is multiplied by the factor of the ordered set before passing to the aggregate function (aggregation function).* if there is no fingerThe WEIGHTS option is set, and the multiplication factor is set to 1 by default.* AGGREGATE* using the AGGREGATE option, you can specify the aggregation mode of the result set that is set together.* default parametersSUM, the sum of the score value of a member in all sets can be used as the score value of the member in the result set; using parameter MIN,* the minimum score value of a member in all sets can be used as the SC of the member in the result set.Ore value; and parameter MAX is the maximum score value of a member in all sets as the score value of the member in the result set.*/
    @Test
    public void ZUNIONSTORE() {
        ZADDForKey(KEY + 0);
        ZRANGE();
        System.out.println("###############################");
        System.out.println(jedis.zrange(KEY + 0, 0, -1));
        ZParams zParams = new ZParams();
        zParams.aggregate(ZParams.Aggregate.MAX);
//        zParams.weightsByDouble(1);
        jedis.zunionstore(KEY, zParams, KEY, KEY + 0);
        System.out.println("###############################");
        System.out.println(jedis.zrange(KEY, 0, -1));
    }

    /**
     * ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
     * The intersection of one or more ordered sets is calculated, in which the number of given key must be specified with the numkeys parameter and the intersection (result set) is stored to destination.* by default, the SCO of a member in the result set.The re value is the sum of the score value of the member under all given sets.* for descriptions of WEIGHTS and AGGREGATE options, see ZUNIONSTORE command.*/
    @Test
    public void ZINTERSTORE() {
        System.out.println(jedis.zrange(KEY, 0, -1));
        System.out.println("###############################");
        ZParams zParams = new ZParams();
        zParams.aggregate(ZParams.Aggregate.MIN);
//        zParams.weightsByDouble(1d);
        System.out.println(jedis.zrange(KEY + 0, 0, -1));
        jedis.zinterstore(KEY, zParams, KEY, KEY + 0);
        System.out.println("###############################");
        System.out.println(jedis.zrange(KEY, 0, -1));
    }

    /**
     * ZRANGEBYLEX key min max [LIMIT offset count]
     * When all members of an ordered set have the same value, the elements of the ordered set are sorted according to the lexicographical ordering of the members.* and this command can return the given ordered set key key.In the middle, the value between min and Max is a member.* if the members in the ordered set have different scores, the result returned by the command is unspecified (unspecified).* command will use the m of the C languageThe emcmp () function, a byte - by - byte contrast (byte-by-byte compare) for each member in the set, and returns the sorted member of the set according to the order from low to high.* if there is a part of the two string that is a phaseIn the same way, the command will think that the longer string is shorter than the string.* the optional LIMIT offset count parameter is used to get matching elements in the specified range (like SELECT LIMIT in SQL).Offset count statement). One thing to note is that,* if the value of the offset parameter is very large, then the command needs to traverse to the location specified by offset before returning the result, which will add the command to the command.Up to O (N) complexity.* how to specify a range* the legitimate min and Max parameters must contain (or [], (which represents the open interval (the specified value will not be included in the range).Represents a closed interval (the specified value will be included in the scope).* special value + + - has special meaning in Min parameters and Max parameters, where + means positive infinity, and minus negative. So, to a member of allSending commands with the same ordered set of all values* ZRANGEBYLEX < zset> - +, the command will return all elements in the ordered set.*/
    @Test
    public void ZRANGEBYLEX() {
        /**
         *
         redis> ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g
         (integer) 7
         redis> ZRANGEBYLEX myzset - [c
         1) "a"
         2) "b"
         3) "c"

         redis> ZRANGEBYLEX myzset - (c
         1) "a"
         2) "b"

         redis> ZRANGEBYLEX myzset [aaa (g
         1) "b"
         2) "c"
         3) "d"
         4) "e"
         5) "f"
         */
        ZRANGE();
        System.out.println(jedis.zrangeByLex(KEY, "-", "+"));
        System.out.println(jedis.zrangeByLex(KEY, "-", "+", 1, 2));
    }

    /**
     * ZLEXCOUNT key min max
     * For an ordered set key key that has the same values of all members, the command returns the number of elements within the min and max range.* the meaning of the Min parameter and the max parameter of this command and ZThe Min parameter of the RANGEBYLEX command is the same as that of the max parameter.*/
    @Test
    public void ZLEXCOUNT() {
        System.out.println(jedis.zlexcount(KEY, "-", "+"));
    }

    /**
     * ZREMRANGEBYLEX key min max
     * For an ordered set key key that has the same values of all members, this command removes all elements within the set, which are within the range of min and max.* the meaning of the Min parameter and the max parameter of this command and ZThe Min parameter of the RANGEBYLEX command is the same as that of the max parameter.*/
    @Test
    public void ZREMRANGEBYLEX() {
        ZRANGE();
        jedis.zremrangeByLex(KEY, "-", "+");
        System.out.println("#################");
        ZRANGE();
    }

    private int getRandomInt() {
        return new Random().nextInt(100);
    }

    private void ZADDForKey(String key) {
        if (StringUtils.isEmpty(key)) {
            key = KEY;
        }
        Map<String, Double> sourceMember = new HashMap<String, Double>();
        for (int i = 0; i < 3; i++) {
            double score = getRandomInt();
            sourceMember.put(VALUE + score, score);
        }
        jedis.zadd(key, sourceMember);
    }
}

 

  The page of the front desk needs to be used to interact with Ajax

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>


<html>

<title>< /title>< meta name= "Keywords" content= "one exhibition color value selection" >< script src= "../../js/jquery.min.js "> < /script>< link type= "text/css" rel= "stylesheet" href= "${pageContext.request.co"NtextPath}/css/top50.css ">< script type= "text/javascript" >Function vote (type, modelid){/ / / Alert (type);/ / / Alert (modelid);/ / / var vote_win = window.open ('/jed/toUpiao? Gid=32& type='+type+'& modelid='+modelid);/ / Alert (2);/ / vote_win.focuS ();/ / Alert (3);$.ajax ({Url:'/jed/toupiao',Data: {typE: type, modelid: modelid},DataType:'json',});Window.location.reloAd ();}< /script>< /head>< body>< div class= "w950" >< div class= "s_title" >< h3> < b> a mice color selection < /b> < /h3>< /div>< divClass= "s_block" >< div class= "SS" >< DL class= "piclist2" >< c:forEach items= "${stuList}" var= "stu" varStatus= "status" >< DD>< div class= "divimg" > < img src= "../../img/${stu.num}.jpg" width= "140" height= "213" > < /div>< ul>≪ li> < input type= "button" onclick= "vote (${stu.num}, ${stu.count})" > "${stu.num}"T; ${stu.count}< /em> ticket < /li>< /ul>&lT; div class= "picbg" > < /div>< /dd>< /c:forEach≫< /dl>< /div>< /div>< /div>< /body></html>

  

Leave a Reply

Your email address will not be published. Required fields are marked *