目录
String
127.0.0.1:6379> set k1 99
OK
127.0.0.1:6379> get k1
"99"
127.0.0.1:6379> mset k2 2 k3 3
OK
127.0.0.1:6379> mget k1 k2 k3
1) "99"
2) "2"
3) "3"
127.0.0.1:6379> msetnx k4 a k5 b
(integer) 1
127.0.0.1:6379> mget k4 k5
1) "a"
2) "b"
取出所有key
127.0.0.1:6379> keys *
1) "k1"
2) "k5"
3) "k3"
4) "k4"
5) "k2"
模式匹配
127.0.0.1:6379> keys *1
1) "k1"
* 表示任意字符
?表示1个任意字符
value 的类型
127.0.0.1:6379> TYPE k1
string
127.0.0.1:6379> OBJECT encoding k1
"int"
127.0.0.1:6379> OBJECT encoding k5
"raw"
127.0.0.1:6379> STRLEN k1
(integer) 2
127.0.0.1:6379> STRLEN k5
(integer) 1
127.0.0.1:6379> set k5 aaa
OK
127.0.0.1:6379> STRLEN k5
(integer) 3
127.0.0.1:6379> set k5 中
OK
127.0.0.1:6379> STRLEN k5
(integer) 3
字符串的追加
127.0.0.1:6379> set kk abc
OK
127.0.0.1:6379> get kk
abc
127.0.0.1:6379> APPEND kk def
6
127.0.0.1:6379> get kk
abcdef
索引 (有正向索引,负向索引)
127.0.0.1:6379> get kk
abcdef
127.0.0.1:6379> GETRANGE kk 2 3
cd
127.0.0.1:6379> GETRANGE kk -3 -2
de
GETRANGE 是按字节取的
覆盖
127.0.0.1:6379> SETRANGE kk 2 nmsl
6
127.0.0.1:6379> get kk
abnmsl
List
队列:L/R R/L
栈: L/L R/R
同向栈 异向队列
127.0.0.1:6379> LPUSH list1 a b c d e
(integer) 5
127.0.0.1:6379> LPOP list1
"e"
127.0.0.1:6379> LPOP list1
"d"
127.0.0.1:6379> LPOP list1
"c"
127.0.0.1:6379> LPOP list1
"b"
127.0.0.1:6379> LPOP list1
"a"
取0到-1
127.0.0.1:6379> LRANGE list1 0 -1
1) "e"
2) "d"
3) "c"
4) "b"
5) "a"
127.0.0.1:6379> LINDEX list1 1
"d"
127.0.0.1:6379> LINDEX list1 -1
"a"
Hash
hash存储可以减少key的数量
节省内存空间
127.0.0.1:6379> HSET xinye name wmx
(integer) 1
127.0.0.1:6379> HSET xinye age 21
(integer) 1
127.0.0.1:6379> HSET xinye sex man
(integer) 1
127.0.0.1:6379> HSET qx name jqx
(integer) 1
127.0.0.1:6379> HSET qx age 21
(integer) 1
127.0.0.1:6379> HSET qx sex woman
(integer) 1
127.0.0.1:6379> HGETALL xinye
1) "name"
2) "wmx"
3) "age"
4) "21"
5) "sex"
6) "man"
127.0.0.1:6379> HKEYS xinye
1) "name"
2) "age"
3) "sex"
127.0.0.1:6379> HVALS xinye
1) "wmx"
2) "21"
3) "man"
127.0.0.1:6379> HGEt xinye age
"21"
127.0.0.1:6379> HINCRBYfloat xinye age -0.5
"20.5"
127.0.0.1:6379> HGEt xinye age
"20.5"
[微博的好友关注]
用户ID为key,Field为好友ID,Value为关注时间
user:1000 user:606 20150808
[统计数]
包括:关注数、粉丝数、喜欢商品数、发帖数
用户为Key,不同维度为Field,Value为统计数
比如关注了5人
HSET user:100000 follow 5
HINCRBY user:100000 follow 1
Set
127.0.0.1:6379> SADD friend "peter" "jack" "tom" "john" "may" "ben"
(integer) 6
127.0.0.1:6379> SMEMBERS friend
1) "john"
2) "tom"
3) "may"
4) "ben"
5) "peter"
6) "jack"
127.0.0.1:6379> SRANDMEMBER friend 3
1) "tom"
2) "ben"
3) "peter"
127.0.0.1:6379> SRANDMEMBER friend 3
1) "may"
2) "ben"
3) "peter"
127.0.0.1:6379> SRANDMEMBER friend -5 有重复的
1) "may"
2) "john"
3) "may"
4) "john"
5) "tom"
SRANDMEMBER setName 正数 应用场景:
100个人抽中3人领取奖品
SRANDMEMBER setName 3
SRANDMEMBER setName 负数 应用场景:
3个人瓜分100份奖品
SRANDMEMBER setName -100
127.0.0.1:6379> spop friend
"john"
127.0.0.1:6379> spop friend
"tom"
127.0.0.1:6379> spop friend
"jack"
127.0.0.1:6379> spop friend
"may"
127.0.0.1:6379> spop friend
"peter"
127.0.0.1:6379> spop friend
"ben"
随机从集合中移除并返回这个被移除的元素
比如现场抽取幸运观众,你已经被抽中,被移除,不会再被抽中
差集(有方向性)
127.0.0.1:6379> SADD number1 123 456 789
(integer) 3
127.0.0.1:6379> SADD number2 123 456 999
(integer) 3
127.0.0.1:6379> SDIFF number1 number2
1) "789"
交集
127.0.0.1:6379> SINTER number1 number2
1) "123"
2) "456"
并集
127.0.0.1:6379> SUNION number1 number2
1) "123"
2) "456"
3) "789"
4) "999"
SortedSet
127.0.0.1:6379> ZADD fruits 3.2 香蕉
(integer) 1
127.0.0.1:6379> ZADD fruits 2.0 西瓜
(integer) 1
127.0.0.1:6379> ZADD fruits 4.0 番石榴 7.0 梨 6.8 芒果
(integer) 3
127.0.0.1:6379> ZSCORE fruits 西瓜
"2"
127.0.0.1:6379> ZSCORE fruits 芒果
"6.7999999999999998"
返回分值的排名
127.0.0.1:6379> ZRANK fruits 西瓜
(integer) 0
127.0.0.1:6379> ZRANK fruits 芒果
(integer) 3
127.0.0.1:6379> ZRANGEBYSCORE fruits -inf +inf withscores
西瓜
2
香蕉
3.2000000000000002
番石榴
4
芒果
6.7999999999999998
梨
7
sortedset 底层是跳跃表
我们常用来做一些排序如榜单
转载请注明:汪明鑫的个人博客 » Redis 数据结构命令小练
说点什么
您将是第一位评论人!