redis按积分来排序好友

转载的网友的代码,加了些注释:

connect();
   $uid = 111;//当前用户id
   $fried_uid_list = array(123,456,789,101);//当前用户的好友数组
   
   //增加好友 123,456,789,101 到set集合  111:friend:list
   foreach ($fried_uid_list as $v)
   {
       $redis -> sadd($uid.':friend:list' ,$v);
   }
   
   //uid:sort:123 
   //uid及好友们各自uid所对应的积分增加到set集合中 uid:sort:uid111
   $redis->set('uid:sort:111',9000);
   $redis->set('uid:sort:123',1000);
   $redis->set('uid:sort:456',6000);
   $redis->set('uid:sort:789',100);
   $redis->set('uid:sort:101',5999);
  
   //uid info用户相关信息的数组
   $use_infor_list = array(
       111=>array('uid'=>111, 'name'=>'wgr'),
       123=>array('uid'=>123, 'name'=>'lucy'),
       456=>array('uid'=>456, 'name'=>'marry'),
       789=>array('uid'=>789, 'name'=>'ice'),
       101=>array('uid'=>101, 'name'=>'jack'),
       );
   foreach ($use_infor_list as $v)
   {
     //保存相关信息到set集合, uid:111 uid:123 uid:456 
      $redis->set('uid:'.$v['uid'] , json_encode($v));
   }
   //sort方法默认返回的是数组,但也可以返回到一个集合
   $result = $redis->sort('111:friend:list by uid:sort:* get uid:* ' ); 
      
   var_dump($result);    
?>

最终结果是:

array(4) {
[0]=>
string(24) "{"uid":789,"name":"ice"}"
[1]=>
string(25) "{"uid":123,"name":"lucy"}"
[2]=>
string(25) "{"uid":101,"name":"jack"}"
[3]=>
string(26) "{"uid":456,"name":"marry"}"

相关参考资料:
1、sort方法的详细使用说明:
http://redisdoc.com/key/sort.html
(英)https://redis.io/commands/sort
2、键的通配符的写法说明KEYS pattern:
http://redisdoc.com/key/keys.html
(英)https://redis.io/commands/keys

你可能感兴趣的:(redis按积分来排序好友)