Skip to content

Instantly share code, notes, and snippets.

@winse
Created October 21, 2014 08:18
Show Gist options
  • Save winse/30f9db38a4c41aaf5f9d to your computer and use it in GitHub Desktop.
Save winse/30f9db38a4c41aaf5f9d to your computer and use it in GitHub Desktop.
redis sort
127.0.0.1:8888> sadd mylist 8 13 9 1 4 888 14 143
(integer) 8
127.0.0.1:8888> SCARD mylist
(integer) 8
127.0.0.1:8888> SMEMBERS mylist
1) "1"
2) "4"
3) "8"
4) "9"
5) "13"
6) "14"
7) "143"
8) "888"
127.0.0.1:8888> sort mylist desc
1) "888"
2) "143"
3) "14"
4) "13"
5) "9"
6) "8"
7) "4"
8) "1"
127.0.0.1:8888> sort mylist alpha
1) "1"
2) "13"
3) "14"
4) "143"
5) "4"
6) "8"
7) "888"
8) "9"
127.0.0.1:8888> sort mylist limit 5 2 alpha
1) "8"
2) "888"
127.0.0.1:8888> sort mylist limit 2 2 alpha
1) "14"
2) "143"
127.0.0.1:8888>
使用另外键的值作为排序值
127.0.0.1:8888> mset weight_1 9 weight_888 8 weight_13 7 weight_4 6
OK
127.0.0.1:8888> sort mylist by weight_*
1) "14"
2) "143"
3) "8"
4) "9"
5) "4"
6) "13"
7) "888"
8) "1"
127.0.0.1:8888> sort mylist by weight_* desc
1) "1"
2) "888"
3) "13"
4) "4"
5) "9"
6) "8"
7) "143"
8) "14"
nosort的不存在,这样就不会对mylist进行排序,如果你仅需要获取额外的键,同时不需要付出排序的高额代价。
127.0.0.1:8888> SORT mylist BY nosort
1) "1"
2) "4"
3) "8"
4) "9"
5) "13"
6) "14"
7) "143"
8) "888"
127.0.0.1:8888>
获取额外的值
127.0.0.1:8888> SORT mylist BY nosort get weight_*
1) "9"
2) "6"
3) (nil)
4) (nil)
5) "7"
6) (nil)
7) (nil)
8) "8"
127.0.0.1:8888> SORT mylist BY nosort get weight_* get #
1) "9"
2) "1"
3) "6"
4) "4"
5) (nil)
6) "8"
7) (nil)
8) "9"
9) "7"
10) "13"
11) (nil)
12) "14"
13) (nil)
14) "143"
15) "8"
16) "888"
127.0.0.1:8888>
store权限,把排序的结构保存起来
127.0.0.1:8888> SORT mylist BY weight_* STORE resultkey
(integer) 8
127.0.0.1:8888> llen resultkey
(integer) 8
127.0.0.1:8888> LRANGE resultkey 0 -1
1) "14"
2) "143"
3) "8"
4) "9"
5) "4"
6) "13"
7) "888"
8) "1"
hash值的处理
SORT mylist BY weight_*->fieldname GET object_*->fieldname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment