常用命令
启动Redis
[root@localhost /]# redis-cli
127.0.0.1:6379>
查看状态
ping
127.0.0.1:6379> ping
PONG
查看当前数据库key的数目
dbsize
127.0.0.1:6379> dbsize
(integer) 1
切换库
select index
127.0.0.1:6379> select 5
OK
127.0.0.1:6379[5]> dbsize
(integer) 0
删除当前数据库的数据
dbsize
127.0.0.1:6379> dbsize
(integer) 1
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> dbsize
(integer) 0
退出连接
quit
exit
key操作
查找所有符合条件的key
keys pattern
*表示多个字符
?表示单个字符
127.0.0.1:6379> keys *
1) "aw"
2) "a"
3) "kl"
127.0.0.1:6379> keys ?l
1) "kl"
查看key是否存在
exists key
127.0.0.1:6379> exists a
(integer) 1
127.0.0.1:6379> exists xiaoming
(integer) 0
设置key的生存时间
expire key second
不成功返回0
127.0.0.1:6379> expire a 3
(integer) 1
127.0.0.1:6379> keys *
1) "aw"
2) "a"
3) "kl"
127.0.0.1:6379> keys *
1) "aw"
2) "kl"
返回key的剩余存在时间
ttl key
否则返回剩余存在时间
查看key的数据类型
type key
127.0.0.1:6379> type aw
string
127.0.0.1:6379> type lll
none
删除指定的key
127.0.0.1:6379> keys *
1) "aw"
2) "kl"
127.0.0.1:6379> del aw
(integer) 1
127.0.0.1:6379> keys *
1) "kl"
string操作
给key设置字符串类型
set key value
127.0.0.1:6379> set a abcd
OK
127.0.0.1:6379> get a
"abcd"
获取key的字符串值
127.0.0.1:6379> get a
"abcd"
对key进行加/减操作
如果 key 不存在,则 key 的值先被初始化为 0 再执行incr操作(只能对数字类型的数据操作)
key加1
incr key
127.0.0.1:6379> set idx 1
OK
127.0.0.1:6379> incr idx
(integer) 2
key减1
decr key
127.0.0.1:6379> decr idx
(integer) 1
key加上指定数
incrby key n
127.0.0.1:6379> set num 1
OK
127.0.0.1:6379> incrby num 5
(integer) 6
key减去指定数
decrby key n
127.0.0.1:6379> decrby num 5
(integer) 1
追加内容
append key value
127.0.0.1:6379> get aw
"acs"
127.0.0.1:6379> append aw 1234
(integer) 7
127.0.0.1:6379> get aw
"acs1234"
返回key的长度
strlen key
key不存在则返回0
127.0.0.1:6379> strlen aw
(integer) 7
截取字符串
getrange key start end
127.0.0.1:6379> get aw
"acs1234"
127.0.0.1:6379> getrange aw 1 3
"cs1"
从尾部截取
127.0.0.1:6379> get aw
"acs1234"
从offset开始用value覆盖key
setrange key offset value
127.0.0.1:6379> get aw
"acs1234"
127.0.0.1:6379> setrange aw 3 78910
(integer) 8
127.0.0.1:6379> get aw
"acs78910"
同时设置多个key-value
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
同时获取多个key存储的值
mget key1 [key2] ...
127.0.0.1:6379> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
list操作
从列表的左边添加元素
lpush list value1 [value2...]
127.0.0.1:6379> lpush list a b c
(integer) 3
查询列表中的全部元素
lrange list 0 -1
127.0.0.1:6379> lrange list 0 -1
1) "c"
2) "b"
3) "a"
从列表的右边添加元素
rpush list value1 [value2...]
127.0.0.1:6379> rpush list d e f
(integer) 6
127.0.0.1:6379> lrange list 0 -1
1) "c"
2) "b"
3) "a"
4) "d"
5) "e"
6) "f"
从左边弹出一个元素并返回
lpop list
127.0.0.1:6379> lpop list
"c"
从右边弹出一个元素并返回
rpop list
127.0.0.1:6379> rpop list
"f"
根据指定下标获取元素
lindex list 下标
127.0.0.1:6379> lrange list 0 -1
1) "b"
2) "a"
3) "d"
4) "e"
127.0.0.1:6379> lindex list 2
"d"
获取列表的长度
llen list
127.0.0.1:6379> llen list
(integer) 4
从左边删除指定个数的元素
lrem list n value
127.0.0.1:6379> lrange list 0 -1
1) "a"
2) "a"
3) "c"
4) "b"
5) "a"
6) "b"
7) "a"
8) "d"
9) "e"
127.0.0.1:6379> lrem list 3 a
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "c"
2) "b"
3) "b"
4) "a"
5) "d"
6) "e"
set操作
添加元素
sadd set value1 [value2]...
127.0.0.1:6379> sadd s1 v1 v2 v3
(integer) 3
查询集合所有元素
smembers set
127.0.0.1:6379> smembers s1
1) "v3"
2) "v1"
3) "v2"
判断集合是否存在某个元素
sismember set value
存在返回1
不存在返回0
127.0.0.1:6379> sismember s1 v1
(integer) 1
127.0.0.1:6379> sismember s1 v4
(integer) 0
返回集合中元素的个数
scard set
127.0.0.1:6379> scard s1
(integer) 3
删除集合中指定的元素
srem set value
127.0.0.1:6379> srem s1 v1
(integer) 1
127.0.0.1:6379> scard s1
(integer) 2
随机弹出集合中某个元素
spop set
127.0.0.1:6379> spop s1
"v2"
随机弹出指定个数的元素,但不会从集合中删除
srandmember set n
127.0.0.1:6379> smembers s1
1) "v4"
2) "v2"
3) "v1"
4) "v5"
5) "v6"
6) "v3"
127.0.0.1:6379> srandmember s1 2
1) "v1"
2) "v4"
127.0.0.1:6379> smembers s1
1) "v1"
2) "v5"
3) "v6"
4) "v2"
5) "v3"
6) "v4"
取交集
sinter set1 set2
127.0.0.1:6379> smembers s1
1) "v1"
2) "v5"
3) "v6"
4) "v2"
5) "v3"
6) "v4"
127.0.0.1:6379> smembers s2
1) "a"
2) "v2"
3) "v1"
4) "b"
5) "c"
6) "d"
127.0.0.1:6379> sinter s1 s2
1) "v1"
2) "v2"
取并集
sunion set1 set2
127.0.0.1:6379> sunion s1 s2
1) "v6"
2) "v2"
3) "v3"
4) "v4"
5) "c"
6) "v5"
7) "a"
8) "v1"
9) "b"
10) "d"
取补集
sdiff set1 set2
127.0.0.1:6379> sdiff s1 s2
1) "v3"
2) "v4"
3) "v5"
4) "v6"
zset操作
添加元素
zadd zset score1 value1 [score2 value2]...
127.0.0.1:6379> zadd stu 99 aw 88 xh 77 xm
(integer) 3
显示集合中的所有值
zrange zset 0 -1
127.0.0.1:6379> zrange stu 0 -1
1) "xm"
2) "xh"
3) "aw"
127.0.0.1:6379> zrange stu 0 -1 withscores
1) "xm"
2) "77"
3) "xh"
4) "88"
5) "aw"
6) "99"
取指定区间的元素
zrangebyscore zset score1 score2
127.0.0.1:6379> zrangebyscore stu 1 100
1) "xm"
2) "xh"
3) "aw"
127.0.0.1:6379> zrangebyscore stu 1 100 withscores
1) "xm"
2) "77"
3) "xh"
4) "88"
5) "aw"
6) "99"
按照分数降序排列
zrevrangebyscore zset score1 score2
127.0.0.1:6379> zrevrangebyscore stu 100 1
1) "aw"
2) "xh"
3) "xm"
给指定的元素添加分数
zincrby stu score value
127.0.0.1:6379> zrange stu 0 -1 withscores
1) "xm"
2) "77"
3) "xh"
4) "88"
5) "aw"
6) "99"
7) "kl"
8) "110"
127.0.0.1:6379> zincrby stu 1 aw
"100"
删除指定的元素
zrem zset value
127.0.0.1:6379> zrem stu xm
(integer) 1
127.0.0.1:6379> zrange stu 0 -1 withscores
1) "xh"
2) "88"
3) "aw"
4) "100"
5) "kl"
6) "110"
统计指定分数区间元素的个数
zcount zset score1 score2
127.0.0.1:6379> zcount stu 1 100
(integer) 2
查询指定元素的排名
zrank zset 元素
127.0.0.1:6379> zrank stu xh
(integer) 0
127.0.0.1:6379> zrank stu aw
(integer) 1
hash操作
向hash中存储数据
hset key field value
127.0.0.1:6379> hset user id 10001
(integer) 1
获取指定的数据
hget key field
127.0.0.1:6379> hget user id
"10001"
批量向hash中添加数据
hmset key field value field value
127.0.0.1:6379> hmset user name kl age 19
OK
批量获取数据
127.0.0.1:6379> hmget user name age
1) "kl"
2) "19"
判断key中是否存在指定的field
存在返回1
不存在返回0
127.0.0.1:6379> hexists user name
(integer) 1
获取所有的field
hkeys key
127.0.0.1:6379> hkeys user
1) "id"
2) "name"
3) "age"
获取所有的value
hvals key
127.0.0.1:6379> hvals user
1) "10001"
2) "kl"
3) "19"
删除指定的field及其对应的value
hdel key field
127.0.0.1:6379> hdel user id
(integer) 1
127.0.0.1:6379> hget user id
(nil)
对指定的value的值自增/自减
hincrby key value 1
hincrby key value -1
127.0.0.1:6379> hget user id
"99"
127.0.0.1:6379> hincrby user id 1
(integer) 100
127.0.0.1:6379> hincrby user id -1
(integer) 99
设置指定field的值
hsetnx key field value
如果field存在则不成功,返回0
如果field不存在则成功,返回1
127.0.0.1:6379> hsetnx user time 6:30
(integer) 1
127.0.0.1:6379> hsetnx user time 8:00
(integer) 0
原文地址:https://blog.csdn.net/m0_72832574/article/details/134626028
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_15487.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。