redis常用命令
目录
Redis数据结构介绍
redis是一种高级得key-value得存储系统,其中value支持物种数据类型
1、字符串(String)
2、哈希(hash)
3、字符串列表(list)
4、字符串集合(set)
5、有序字符串集合(sorted set)
存储String
常用命令
赋值、取值
127.0.0.1:6379> set name dage
OK
127.0.0.1:6379> get name
"dage"
删除
127.0.0.1:6379> del name
(integer) 1
数值增加、减少
//增加
127.0.0.1:6379> set num 5
OK
127.0.0.1:6379> incr num
(integer) 6
127.0.0.1:6379> incrby num 3
(integer) 9
127.0.0.1:6379> get num
"9"
//减少
127.0.0.1:6379> decr num
(integer) 8
127.0.0.1:6379> decrby num 4
(integer) 4
127.0.0.1:6379> get num
"4"
字符串追加
127.0.0.1:6379> set name dage
OK
127.0.0.1:6379> append name 666
(integer) 7
127.0.0.1:6379> get name
"dage666"
存储hash
常用命令
赋值
//一次赋值一个
127.0.0.1:6379> hset myhash name dage
(integer) 0
//一次赋值多个
127.0.0.1:6379> hmset myhash age 18 addr China
OK
取值
//取其中得一个值
127.0.0.1:6379> hget myhash name
"dage"
//取某几个值
127.0.0.1:6379> hmget myhash name age
1) "dage"
2) "18"
//取所有得值
127.0.0.1:6379> hgetall myhash
1) "name"
2) "dage"
3) "age"
4) "18"
5) "addr"
6) "China"
删除
//删除其中得一个
127.0.0.1:6379> del myhash addr
(integer) 1
//删除所有的值
127.0.0.1:6379> del myhash
(integer) 0
127.0.0.1:6379> hgetall myhash
(empty list or set)
增加减少数字
127.0.0.1:6379> hset myhash age 18
(integer) 1
//增加数值
127.0.0.1:6379> hincrby myhash age 2
(integer) 20
减少数值
127.0.0.1:6379> hincrby myhash age -3
(integer) 17
127.0.0.1:6379> hget myhash age
"17"
(了解)
判断某个key中是否存在某个filed
127.0.0.1:6379> hset myhash age 18
(integer) 0
127.0.0.1:6379> hset myhash name dage
(integer) 1
//存在为1 不存在为0
127.0.0.1:6379> hexists myhash name
(integer) 1
127.0.0.1:6379> hexists myhash addr
(integer) 0
统计某key中总的filed总量
127.0.0.1:6379> hlen myhash
(integer) 2
取得所有的key、取得所有的value
127.0.0.1:6379> hkeys myhash
1) "age"
2) "name"
127.0.0.1:6379> hvals myhash
1) "18"
2) "dage"
存储list
常用命令
两端添加
//添加到头部
127.0.0.1:6379> lpush mylist a b c
(integer) 3
127.0.0.1:6379> lpush mylist 1 2 3
(integer) 6
//添加到末尾
127.0.0.1:6379> rpush mylist2 1 2 3 4
(integer) 4
127.0.0.1:6379> rpush mylist2 a b c d
(integer) 8
查看列表
//开头是0 结尾是-1
127.0.0.1:6379> lrange mylist 0 -1
1) "3"
2) "2"
3) "1"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> lrange mylist2 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "a"
6) "b"
7) "c"
8) "d"
弹出首、尾元素
127.0.0.1:6379> lrange mylist 0 -1
1) "3"
2) "2"
3) "1"
4) "c"
5) "b"
6) "a"
//弹出首元素
127.0.0.1:6379> lpop mylist
"3"
//弹出尾元素
127.0.0.1:6379> rpop mylist
"a"
获取链表中元素的个数
127.0.0.1:6379> lrange mylist 0 -1
1) "2"
2) "1"
3) "c"
4) "b"
127.0.0.1:6379> llen mylist
(integer) 4
(了解)
仅当key存在时,才向链表中的头或尾添加元素
127.0.0.1:6379> lrange mylist 0 -1
1) "2"
2) "1"
3) "c"
4) "b"
//可以插入 存在mylist该key
127.0.0.1:6379> lpushx mylist 3
(integer) 5
127.0.0.1:6379> rpushx mylist a
(integer) 6
//不能插入 不存在mylist3 key
127.0.0.1:6379> lpushx mylist3 a
(integer) 0
127.0.0.1:6379> rpushx mylist3 a
(integer) 0
//查看插入结果
127.0.0.1:6379> lrange mylist 0 -1
1) "3"
2) "2"
3) "1"
4) "c"
5) "b"
6) "a"
删除多个值为value的元素
lrem key count value:删除count个值为value的元素。
如果count大于0,从头向尾遍历并删除count个值为value的元素。
如果count小于0,则从尾向头删除。
如果count等于0,则删除链表中所有等于value的元素。
//初始化链表
127.0.0.1:6379> del mylist
(integer) 1
127.0.0.1:6379> lpush mylist 1 2 3
(integer) 3
127.0.0.1:6379> lpush mylist 1 2 3
(integer) 6
127.0.0.1:6379> lpush mylist 1 2 3
(integer) 9
//从头删除2个值为3的元素
127.0.0.1:6379> lrange mylist 0 -1
1) "3" --》删除
2) "2"
3) "1"
4) "3" --》删除
5) "2"
6) "1"
7) "3"
8) "2"
9) "1"
127.0.0.1:6379> lrem mylist 2 3
(integer) 2
//从头尾删除2个值为1的元素
127.0.0.1:6379> lrange mylist 0 -1
1) "2"
2) "1"
3) "2"
4) "1" --》删除
5) "3"
6) "2"
7) "1" --》删除
127.0.0.1:6379> lrem mylist -2 1
(integer) 2
//删除所有值等于2的元素
127.0.0.1:6379> lrange mylist 0 -1
1) "2"
2) "1"
3) "2"
4) "3"
5) "2"
127.0.0.1:6379> lrem mylist 0 2
(integer) 3
//最后结果查询
127.0.0.1:6379> lrange mylist 0 -1
1) "1"
2) "3"
修改某个角标的元素值
127.0.0.1:6379> lrange mylist 0 -1
1) "1"
2) "3"
//修改角标为1的值为aaa
127.0.0.1:6379> lset mylist 1 aaa
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "1"
2) "aaa"
//修改不存在的角标元素报错
127.0.0.1:6379> lset mylist 2 bbb
(error) ERR index out of range
在某个元素的前后插入值
应用场景(消息队列)
127.0.0.1:6379> lrange mylist 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
//在元素前插入值
127.0.0.1:6379> linsert mylist before 3 aaa
(integer) 5
//在元素后插入值
127.0.0.1:6379> linsert mylist after 3 bbb
(integer) 6
127.0.0.1:6379> lrange mylist 0 -1
1) "1"
2) "2"
3) "aaa"
4) "3"
5) "bbb"
6) "4"
从一个链表的尾弹出一个元素放到另一个链表头部
127.0.0.1:6379> lrange mylist 0 -1
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> lrange mylist2 0 -1
1) "a"
2) "b"
3) "c"
//弹出mylist2的最后一个元素放到mylist的头部
127.0.0.1:6379> rpoplpush mylist2 mylist
"c"
127.0.0.1:6379> lrange mylist 0 -1
1) "c"
2) "1"
3) "2"
4) "3"
127.0.0.1:6379> lrange mylist2 0 -1
1) "a"
2) "b"
存储set
常用命令
添加元素
127.0.0.1:6379> sadd myset a b c
(integer) 3
//存在的元素不会被添加
127.0.0.1:6379> sadd myset a
(integer) 0
查看所有元素
127.0.0.1:6379> smembers myset
1) "c"
2) "b"
3) "a"
删除元素
127.0.0.1:6379> srem myset a b
(integer) 2
127.0.0.1:6379> smembers myset
1) "c"
差集A-B
127.0.0.1:6379> smembers myset1
1) "1"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> smembers myset2
1) "2"
2) "1"
3) "a"
4) "3"
127.0.0.1:6379> sdiff myset1 myset2
1) "c"
2) "b"
交集A∩B
127.0.0.1:6379> smembers myset1
1) "1"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> smembers myset2
1) "2"
2) "1"
3) "a"
4) "3"
127.0.0.1:6379> sinter myset1 myset2
1) "1"
2) "a"
并集A∪B
127.0.0.1:6379> smembers myset1
1) "1"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> smembers myset2
1) "2"
2) "1"
3) "a"
4) "3"
127.0.0.1:6379> sunion myset1 myset2
1) "1"
2) "c"
3) "2"
4) "b"
5) "a"
6) "3"
(了解)
统计成员数量
127.0.0.1:6379> smembers myset
1) "a"
2) "3"
3) "c"
4) "1"
5) "2"
6) "b"
127.0.0.1:6379> scard myset
(integer) 6
随机返回成员
127.0.0.1:6379> srandmember myset
"3"
127.0.0.1:6379> srandmember myset
"3"
127.0.0.1:6379> srandmember myset
"c"
127.0.0.1:6379> srandmember myset
"b"
127.0.0.1:6379> srandmember myset 3
1) "c"
2) "1"
3) "2"
127.0.0.1:6379> srandmember myset 3
1) "c"
2) "2"
3) "3"
127.0.0.1:6379>
差集存到另一个set集合
注:另一个集合的原来数据会被删除
//两个集合
127.0.0.1:6379> smembers myset1
1) "1"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> smembers myset2
1) "2"
2) "1"
3) "a"
4) "3"
//保存差集
127.0.0.1:6379> sdiffstore myset3 myset1 myset2
(integer) 2
127.0.0.1:6379> smembers myset3
1) "c"
2) "b"
127.0.0.1:6379>
交存到另一个set集合
//保存交集
127.0.0.1:6379> sinterstore myset3 myset1 myset2
(integer) 2
127.0.0.1:6379> smembers myset3
1) "1"
2) "a"
127.0.0.1:6379>
并存到另一个set集合
127.0.0.1:6379> sunionstore myset3 myset1 myset2
(integer) 6
127.0.0.1:6379> smembers myset3
1) "1"
2) "c"
3) "2"
4) "b"
5) "a"
6) "3"
127.0.0.1:6379>
使用场景
存储sortedset
常用命令
添加元素
127.0.0.1:6379> zadd mysort 70 zhangsan 80 lisi 90 wangwu
(integer) 3
//存在就替换分数
127.0.0.1:6379> zadd mysort 100 zhangsan
(integer) 0
//添加新的成员
127.0.0.1:6379> zadd mysort 50 jack
(integer) 1
127.0.0.1:6379>
获取元素
//获取元素的分数
127.0.0.1:6379> zscore mysort zhangsan
"100"
获取元素的数量
127.0.0.1:6379> zcard mysort
(integer) 4
127.0.0.1:6379>
删除元素
127.0.0.1:6379> zrem mysort jack lisi
(integer) 2
127.0.0.1:6379>
范围查询
//按照分数从小到大
127.0.0.1:6379> zrange mysort 0 -1
1) "jack"
2) "lisi"
3) "wangwu"
4) "zhangsan"
//显示分数
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "jack"
2) "50"
3) "lisi"
4) "80"
5) "wangwu"
6) "90"
7) "zhangsan"
8) "100"
//按照分数从大到小
127.0.0.1:6379> zrevrange mysort 0 -1
1) "zhangsan"
2) "wangwu"
3) "lisi"
4) "jack"
//显示分数
127.0.0.1:6379> zrevrange mysort 0 -1 withscores
1) "zhangsan"
2) "100"
3) "wangwu"
4) "90"
5) "lisi"
6) "80"
7) "jack"
8) "50"
按照排名删除元素
(分数从小到大)
127.0.0.1:6379> zremrangebyrank mysort 0 0
(integer) 1
127.0.0.1:6379> zrange mysort 0 -1
1) "lisi"
2) "wangwu"
3) "zhangsan"
127.0.0.1:6379>
按照分数范围删除
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "lisi"
2) "80"
3) "wangwu"
4) "90"
5) "zhangsan"
6) "100"
127.0.0.1:6379> zremrangebyscore mysort 60 80
(integer) 1
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "wangwu"
2) "90"
3) "zhangsan"
4) "100"
127.0.0.1:6379>
返回分数范围内的成员
127.0.0.1:6379> zrange mysort 0 -1
1) "jack"
2) "lisi"
3) "wangwu"
4) "zhangsan"
127.0.0.1:6379> zrangebyscore mysort 50 90
1) "jack"
2) "lisi"
3) "wangwu"
//我只要两个
127.0.0.1:6379> zrangebyscore mysort 50 100 withscores limit 0 2
1) "jack"
2) "50"
3) "lisi"
4) "80"
127.0.0.1:6379>
增加某个成员的分数
127.0.0.1:6379> zincrby mysort 2 lisi
"82"
127.0.0.1:6379> zscore mysort lisi
"82"
127.0.0.1:6379>
统计分数区间的个数
127.0.0.1:6379> zcount mysort 0 100
(integer) 4
127.0.0.1:6379>
返回成员的排名
127.0.0.1:6379> zrange mysort 0 -1
1) "jack"
2) "lisi"
3) "wangwu"
4) "zhangsan"
127.0.0.1:6379> zrank mysort lisi
(integer) 1
127.0.0.1:6379> zrevrange mysort 0 -1
1) "zhangsan"
2) "wangwu"
3) "lisi"
4) "jack"
127.0.0.1:6379> zrevrank mysort lisi
(integer) 2
应用场景
---------------
我每一次回头,都感觉自己不够努力,所以我不再回头。
---------------