redis 常用命令使用
String类型
设置一个键为a1 值为v1 : set
获取键为a1的值:get
127.0.0.1:6379> set a1 123
OK
127.0.0.1:6379> get a1
"123"
127.0.0.1:6379>
一次性设置多个: mset
一次获取多个: mget
127.0.0.1:6379> mset a1 1 a2 2 a3 3
OK
127.0.0.1:6379> mget a1 a2 a3
1) "1"
2) "2"
3) "3"
127.0.0.1:6379>
拼接字符串:append
127.0.0.1:6379> set a abc
OK
127.0.0.1:6379> append a "&&**"
(integer) 7
127.0.0.1:6379> get a
"abc&&**"
127.0.0.1:6379>
清空控制台:clear
127.0.0.1:6379> set a abc
OK
127.0.0.1:6379> append a "&&**"
(integer) 7
127.0.0.1:6379> get a
"abc&&**"
127.0.0.1:6379> clear
执行后:
127.0.0.1:6379>
帮助命令:help
127.0.0.1:6379> help
redis-cli 3.0.503
Type: "help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
127.0.0.1:6379> help get
GET key
summary: Get the value of a key
since: 1.0.0
group: string
127.0.0.1:6379> help @string
自增:incr
增加整数n:incrby n
增加浮点数(可以为负)f : incrbyfloat f
127.0.0.1:6379> set a 10
OK
127.0.0.1:6379> incr a
(integer) 11
127.0.0.1:6379> incrby a 2
(integer) 13
127.0.0.1:6379> incrbyfloat a "1.5"
"14.5"
127.0.0.1:6379> incrbyfloat a "-1.3"
"13.199999999999999"
127.0.0.1:6379>
自减:decr
减少整数n: decrby n
127.0.0.1:6379> set a 10
OK
127.0.0.1:6379> decr a
(integer) 9
127.0.0.1:6379> decrby a 5
(integer) 4
127.0.0.1:6379>
计算键的值字符串长度:strlen
127.0.0.1:6379> set a abcd
OK
127.0.0.1:6379> strlen a
(integer) 4
127.0.0.1:6379>
设置一个有过期时间的键值对:
以秒为单位:setex
以毫秒为单位:psetex
设置键值对(有就不设置,没有就设置):setnx
127.0.0.1:6379> setex a 10 "abc"
OK
127.0.0.1:6379> get a
"abc"
//经过10秒后
127.0.0.1:6379> get a
(nil)
127.0.0.1:6379> psetex a 5000 "abc"
OK
127.0.0.1:6379> get a
"abc"
//经过5秒后
127.0.0.1:6379> get a
(nil)
127.0.0.1:6379> setnx a 123
(integer) 1
127.0.0.1:6379> setnx a 234
(integer) 0
127.0.0.1:6379> get a
"123"
127.0.0.1:6379>
哈希类型
设置一个值为哈希类型的键值对:hset
批量设置:hmset
获取一个值为哈希类型的键:hget
批量获取:hmget
获取全部属性:hgetall
获取全部属性的键:hkeys
获取全部属性的值:hvals
判断哈希值属性的键是否存在:hexists
127.0.0.1:6379> hset user name "zhangsan"
(integer) 0
127.0.0.1:6379> hget user name
"zhangsan"
127.0.0.1:6379> hmset user age 18 addr "shenzhen" gender "nan"
OK
127.0.0.1:6379> hmget user name age addr gender
1) "zhangsan"
2) "18"
3) "shenzhen"
4) "nan"
127.0.0.1:6379> hgetall user
1) "name"
2) "zhangsan"
3) "age"
4) "18"
5) "wight"
6) "4.5"
7) "addr"
8) "shenzhen"
9) "nick"
10) "sandul"
11) "gender"
12) "nan"
127.0.0.1:6379> hkeys user
1) "name"
2) "age"
3) "wight"
4) "addr"
5) "nick"
6) "gender"
127.0.0.1:6379> hvals user
1) "zhangsan"
2) "18"
3) "4.5"
4) "shenzhen"
5) "sandul"
6) "nan"
127.0.0.1:6379> hexists user
//hexists 指令是判断属性键存不存在的
(error) ERR wrong number of arguments for 'hexists' command
127.0.0.1:6379> hexists user name
(integer) 1
127.0.0.1:6379> hexists user abc
(integer) 0
127.0.0.1:6379>
获取哈希值的键属性个数:hlen
删除哈希值的键值对属性:hdel
增加哈希值的属性的值:hincrby n
增加哈希值的属性的值(浮点类型):hincrbyfloat
设置哈希值的属性键值对(存在则不添加,不存在则添加):hsetnx
127.0.0.1:6379> hgetall user
1) "name"
2) "zhangsan"
3) "age"
4) "18"
5) "wight"
6) "4.5"
7) "addr"
8) "shenzhen"
9) "nick"
10) "sandul"
11) "gender"
12) "nan"
127.0.0.1:6379> hlen user
(integer) 6
127.0.0.1:6379> hdel user wight
(integer) 1
127.0.0.1:6379> hgetall user
1) "name"
2) "zhangsan"
3) "age"
4) "18"
5) "addr"
6) "shenzhen"
7) "nick"
8) "sandul"
9) "gender"
10) "nan"
127.0.0.1:6379> hincrby user age 2
(integer) 20
127.0.0.1:6379> hget user age
"20"
127.0.0.1:6379> hincrbyfloat user age "1.5"
"21.5"
127.0.0.1:6379> hget user age
"21.5"
127.0.0.1:6379> hsetnx user name "ahua"
(integer) 0
127.0.0.1:6379> hget user name
"zhangsan"
127.0.0.1:6379>
list类型
左边压入队列:lpush
右边压入队列:rpush
左边开始读取队列:lrange
获取对应索引的值:lindex
获取队列长度:llen
左边弹出并删除:lpop
右边弹出并删除:rpop
等待指定时间,从左边弹出并删除:blpop
等待指定时间,从右边弹出并删除:brpop
删除指定个数的值:lrem
127.0.0.1:6379> lpush lista a
(integer) 1
127.0.0.1:6379> lpush lista b
(integer) 2
127.0.0.1:6379> lpush lista c
(integer) 3
127.0.0.1:6379> lrange lista 0 -1
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> lrange lista 0 2
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> rpush lista 1
(integer) 4
127.0.0.1:6379> rpush lista 2
(integer) 5
127.0.0.1:6379> rpush lista 3
(integer) 6
127.0.0.1:6379> lrange lista 0 -1
1) "c"
2) "b"
3) "a"
4) "1"
5) "2"
6) "3"
127.0.0.1:6379> llen lista
(integer) 6
127.0.0.1:6379> lpop lista
"c"
127.0.0.1:6379> lrange lista 0 -1
1) "b"
2) "a"
3) "1"
4) "2"
5) "3"
127.0.0.1:6379> rpop lista
"3"
127.0.0.1:6379> lrange lista 0 -1
1) "b"
2) "a"
3) "1"
4) "2"
127.0.0.1:6379> blpop lista 3
1) "lista"
2) "b"
127.0.0.1:6379> brpop lista 3
1) "lista"
2) "2"
127.0.0.1:6379> lrange lista 0 -1
1) "a"
2) "1"
127.0.0.1:6379> rpush lista 2 3 4
(integer) 5
127.0.0.1:6379> lrange lista 0 -1
1) "a"
2) "1"
3) "2"
4) "3"
5) "4"
127.0.0.1:6379> rpush lista a a a
(integer) 8
127.0.0.1:6379> lrange lista 0 -1
1) "a"
2) "1"
3) "2"
4) "3"
5) "4"
6) "a"
7) "a"
8) "a"
127.0.0.1:6379> lrem lista 2 a
(integer) 2
127.0.0.1:6379> lrange lista 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "a"
6) "a"
127.0.0.1:6379>
set 类型
添加数据:sadd
获取数据:smembers
删除数据:srem
计算键的值的数量:scard
判断是否是set中的值:sismember
随机取出set中的指定个数,set中的值不变:srandmember
随机弹出set中的一个值并删除:spop
127.0.0.1:6379> sadd set01 123
(integer) 1
127.0.0.1:6379> sadd set01 a b c das bs 12 324 r43
(integer) 8
127.0.0.1:6379> smembers set01
1) "bs"
2) "123"
3) "a"
4) "b"
5) "c"
6) "12"
7) "324"
8) "das"
9) "r43"
127.0.0.1:6379> srem set01 123
(integer) 1
127.0.0.1:6379> smembers set01
1) "c"
2) "12"
3) "324"
4) "das"
5) "bs"
6) "r43"
7) "b"
8) "a"
127.0.0.1:6379> scard set01
(integer) 8
127.0.0.1:6379> sismember r43
(error) ERR wrong number of arguments for 'sismember' command
127.0.0.1:6379> sismember set01 r43
(integer) 1
127.0.0.1:6379> srandmember set01 3
1) "a"
2) "b"
3) "das"
127.0.0.1:6379> smembers set01
1) "12"
2) "324"
3) "das"
4) "bs"
5) "r43"
6) "b"
7) "a"
8) "c"
127.0.0.1:6379> spop set01
"bs"
127.0.0.1:6379> smembers set01
1) "r43"
2) "b"
3) "a"
4) "c"
5) "12"
6) "das"
7) "324"
127.0.0.1:6379>