search

Home  >  Q&A  >  body text

redis可以对key排序吗

hset user:1 score 100

hset user:2 socre 101

hset user:3 score 200

hset user:4 score 201

要找出socre最高的user,应该如何设计。

高洛峰高洛峰2798 days ago864

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-04-22 09:01:10

    hset cannot be sorted directly. There is an alternative to achieve the equivalent of hset sorting, which is to use the sort function. Sort can generally only sort lists, sets and zsets, but it can be modified. The usage of sort is as follows:
    SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC | DESC] [ALPHA] [STORE destination]

    Pay attention to its by and store parameters. by is used to determine which field to sort by, and store can save the sorting results into a list. You can first save all hash keys into a set, for example, the key name of this set is myhashes, then sort according to the score field of each element in the set (that is, each hash), and finally save the sorting results in In the list with the key name myresult. The sorting result here is of course the key names of all elements in the set (that is, all hash keys). You can get the key names of all elements in the set by adding # to the GET parameter. It is written like this:
    SORT myhashes BY *->score GET # STORE myresult

    SORT will replace the * in the BY parameter with each value in myhashes (that is, each hash key name), and obtain its value based on the fields given after ->, and finally compare each value in myhashes based on these field values. Values ​​are sorted. In this way, myresult contains hash key names that have been sorted according to score.

    Finally, I think there is something wrong with your hash design. Hash is more like a row in a SQL database, with multiple fields, and each field has a corresponding value. If you only have a score field, consider using zset.

    reply
    0
  • 黄舟

    黄舟2017-04-22 09:01:10

    HashSet没法直接排。得单独维护一个SortedSet, only the user ID and corresponding score are stored in it.

    reply
    0
  • PHPz

    PHPz2017-04-22 09:01:10

    I don’t know if that’s what it means
    Just upload the code

    $redis->del("user_id");
    $redis->hmSet("user_1",array("score"=>5,"id"=>1));
    $redis->hmSet("user_2",array("score"=>3,"id"=>2));
    $redis->hmSet("user_3",array("score"=>1,"id"=>3));
    $redis->hmSet("user_4",array("score"=>2,"id"=>4));
    $redis->hmSet("user_5",array("score"=>4,"id"=>5));
    $redis->lPush("user_id",1,2,3,4,5);
    $sortList = $redis->sort("user_id",array(
        'by' => "user_*->score",
        'alpha' => TRUE,
        'get' => array(
            "user_*->id"
        )
    ));
    var_dump($sortList);
    array(5) { [0]=> string(1) "3" [1]=> string(1) "4" [2]=> string(1) "2" [3]=> string(1) "5" [4]=> string(1) "1" } 
    

    Document
    http://redis.cn/commands/sort.html

    reply
    0
  • 黄舟

    黄舟2017-04-22 09:01:10

    Hash cannot be sorted.

    If you need to sort, please use ZSET operation, ZADD SCORES MEMBERS

    The idea is:

    PHP$uid = $redis->ZREVRANGE("u:scores", 0, 0, WITHSCORES);  //PHP-REDIS原生语法貌似这样: ZREVRANGE("u:scores", 0, 0, TRUE);
    
    //$uid is AN ARRAY
    
    $max_uid = array_keys_array($uid)[0]; //这个max uid 就是你要找到scores最大的UID了.
    

    PS it again, if your HASH is only used to store a hashkey SCORES is not recommended to save like this..

    reply
    0
  • Cancelreply