标签:包括 覆盖 3.3 res 次数 指定 2.3 star 失败
1.-x
从标准输入读取一个参数,等价于set k v
[root@localhost etc]# echo -en ‘v1‘|redis-cli -a foobared -x set k1
OK
[root@localhost etc]# redis-cli -a foobared get k1
"v1"
2.-r
重复执行一个命令指定次数
[root@localhost etc]# redis-cli -a foobared -r 2 get k1
"v1"
"v1"
3.-i
命令执行间隔
[root@localhost etc]# redis-cli -a foobared -r 2 -i 3 get k1
"v1"
"v1"
4.-rdb
获取指定实例的rdb文件,保存到本地
[root@localhost etc]# redis-cli -a foobared --rdb /tmp/6379.rdb
SYNC sent to master, writing 263 bytes to ‘/tmp/6379.rdb‘
Transfer finished with success.
[root@localhost etc]# ls -l /tmp/6379.rdb
-rw-r--r--. 1 root root 263 Jun 14 13:34 /tmp/6379.rdb
5.-scan
和-pattern
用scan命令扫描redis中的key,pattern是匹配模式,相比keys pattern
模式好处在于不会长时间阻塞redis而导致其他客户端命令请求被阻塞
[root@localhost etc]# redis-cli -a foobared --scan --pattern ‘*1‘
k1
[root@localhost etc]# redis-cli -a foobared --scan --pattern ‘*1*‘
k10
k1
[root@localhost etc]# redis-cli -a foobared --scan --pattern ‘k*‘
k10
k2
k1
6.连接参数
redis-cli -a password -h hostname/IP -p port
redis-cli -s socketfile
7.socket参数默认不开启unixsocket /tmp/redis.sock
,且同样需要输入密码
[root@localhost tmp]# redis-cli -s /tmp/redis.sock
redis /tmp/redis.sock>
redis /tmp/redis.sock> keys k1
(error) NOAUTH Authentication required.
redis /tmp/redis.sock> auth foobared
OK
redis /tmp/redis.sock> keys *
1) "k10"
2) "bit"
3) "k1"
4) "k2"
8.-stat
获取redis诊断数据
[root@localhost tmp]# redis-cli -a foobared --stat
------- data ------ --------------------- load -------------------- - child -
keys mem clients blocked requests connections #
308087 28.90M 51 0 308649 (+0) 58
432618 37.45M 51 0 433641 (+124992) 58
552107 53.65M 51 0 553683 (+120042) 58
673106 57.96M 51 0 675476 (+121793) 58
797567 66.51M 51 0 800856 (+125380) 58
921655 75.03M 51 0 925951 (+125095) 58
995100 77.12M 1 0 1000101 (+74150) 58
995100 77.12M 1 0 1000102 (+1) 58
995100 77.12M 1 0 1000103 (+1) 58
995100 77.12M 1 0 1000104 (+1) 58
1004507 80.71M 51 0 1009652 (+9548) 108
1119911 104.64M 51 0 1126280 (+116628) 108
1227279 112.01M 51 0 1234913 (+108633) 108 #
redis-benchmark -a foobared -h 127.0.0.1 -p 6379 -t set -n 1000000 -r 100000000
9.-bigkeys
对redis中的key进行采样,寻找较大的keys,根据采样得出大概的数据统计
[root@localhost bin]# redis-cli -a foobared --bigkeys
#
# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).
#
[00.00%] Biggest string found so far ‘key:000036007278‘ with 3 bytes
[50.49%] Sampled 1000000 keys so far
[51.97%] Biggest string found so far ‘bit‘ with 1261 bytes
#
-------- summary -------
#
Sampled 1980449 keys in the keyspace!
Total key length in bytes is 31687130 (avg len 16.00)
#
Biggest string found ‘bit‘ has 1261 bytes
#
1980449 strings with 5942603 bytes (100.00% of keys, avg size 3.00)
0 lists with 0 items (00.00% of keys, avg size 0.00)
0 sets with 0 members (00.00% of keys, avg size 0.00)
0 hashs with 0 fields (00.00% of keys, avg size 0.00)
0 zsets with 0 members (00.00% of keys, avg size 0.00)
10.-latency
获取命令的请求响应延迟时间,单位是毫秒
[root@localhost bin]# redis-cli -a foobared --latency
min: 0, max: 16, avg: 1.31 (5697 samples)
values:string、set、list、hash、zset
结构类型 | 结构存储的值 | 结构的读写能力 |
---|---|---|
string | 字符串、整数、浮点数、任何二进制格式的数据 | 对整字符串和部分进行操作,对整数和浮点数执行自增或者自建操作 |
list | 一个链表,链表的每个字节都包含一个字符串 | 链表两端推出或弹出元素;根据偏移量对链表进行修剪;读取单个或者多个元素;根据值查找或者移除元素 |
set | 包含字符串的无序收集器,每个字符串不重复 | 添加、获取、移除单个元素;检查一个元素是否存在;计算交、并、差集;从集合中随机取元素 |
hash | 包含键值对的无序散列表 | 添加、获取、移除多个键值对;获取所有键值对 |
zset | 字符串成员member与浮点数分值score之间的有序映射,元素的排序有分值大小决定 | 获取、添加、移除单个元素;根据分值范围或者成员来获取元素 |
如果成功,返回ok,失败则返回nil
127.0.0.1:6379> set kk1 vv1 EX 2
OK
127.0.0.1:6379> get kk1
"vv1"
127.0.0.1:6379> get kk1
(nil)
127.0.0.1:6379> set kk1 vv1 PX 2000
OK
127.0.0.1:6379> get kk1
"vv1"
127.0.0.1:6379> get kk1
(nil)
127.0.0.1:6379> set kk1 100 NX
OK
127.0.0.1:6379> get kk1
"100"
127.0.0.1:6379> set kk2 100 XX
(nil)
127.0.0.1:6379> set kk1 200 XX
OK
127.0.0.1:6379> get kk1
"200"
127.0.0.1:6379> set kk1 300 NX
(nil)
返回指定key的值列表
127.0.0.1:6379> mget k1 kk1
1) "v1"
2) "200"
127.0.0.1:6379> mget k1
1) "v1"
127.0.0.1:6379> mget k1 kk1 kk2
1) "v1"
2) "200"
3) (nil)
总是返回ok,此操作不会失败
127.0.0.1:6379> mset k1 v1 k2 v2
OK
127.0.0.1:6379> mget k1 k2
1) "v1"
2) "v2"
127.0.0.1:6379> mset k1 v1 k2 v2 k3
(error) ERR wrong number of arguments for MSET
返回1表示成功,0表示失败
127.0.0.1:6379> mset k2 v2 k3 v3
OK
127.0.0.1:6379> msetnx k3 v3 k4 v4
(integer) 0
127.0.0.1:6379> msetnx k4 v4 k5 v5
(integer) 1
返回字符长度
127.0.0.1:6379> get k6
(nil)
127.0.0.1:6379> append k6 123
(integer) 3
127.0.0.1:6379> get k6
"123"
127.0.0.1:6379> append k6 abc
(integer) 6
127.0.0.1:6379> get k6
"123abc"
返回值为加完的结果
127.0.0.1:6379> mset k1 v1 k2 2.2 k3 3
OK
127.0.0.1:6379> incr k1
(error) ERR value is not an integer or out of range
127.0.0.1:6379> incr k2
(error) ERR value is not an integer or out of range
127.0.0.1:6379> incr k3
(integer) 4
返回操作后的结果值
127.0.0.1:6379> mget k1 k2 k3
1) "v1"
2) "2.2"
3) "4"
127.0.0.1:6379> decr k1
(error) ERR value is not an integer or out of range
127.0.0.1:6379> decr k2
(error) ERR value is not an integer or out of range
127.0.0.1:6379> decr k3
(integer) 3
127.0.0.1:6379> mset k01 -1
OK
127.0.0.1:6379> decr k01
(integer) -2
127.0.0.1:6379> decr k100
(integer) -1
127.0.0.1:6379> get 100
(nil)
返回操作后的结果
127.0.0.1:6379> mset k1 10 k2 20
OK
127.0.0.1:6379> decrby k1 2
(integer) 8
127.0.0.1:6379> incrby k2 2
(integer) 22
127.0.0.1:6379> mget k1 k2
1) "8"
2) "22"
返回操作后的结果
127.0.0.1:6379> mset k1 1.1 k2 2.2 k3 3
OK
127.0.0.1:6379> incrbyfloat k1 2.2
"3.3"
127.0.0.1:6379> decrbyfloat k2 1.1
(error) ERR unknown command ‘decrbyfloat‘
127.0.0.1:6379> incrbyfloat k3 1
"4"
对字符串取长
127.0.0.1:6379> strlen k1
(integer) 3
索引为负表示末尾取
127.0.0.1:6379> set k1 abcdefg
OK
127.0.0.1:6379> getrange k1 0 1
"ab"
127.0.0.1:6379> getrange k1 -2 -1
"fg"
获取一个字符串类型key指定位置的二进制位的值(0/1),索引从0开始
127.0.0.1:6379> get k1
"abcdefg"
127.0.0.1:6379> getbit k1 0
(integer) 0
127.0.0.1:6379> getbit k1 1
(integer) 1
127.0.0.1:6379> getbit k1 2
(integer) 1
127.0.0.1:6379> getbit k1 3
(integer) 0
127.0.0.1:6379> getbit k1 4
(integer) 0
设置字符串类型建指定位置的二进制位的值,返回该位置的旧值
127.0.0.1:6379> setbit k1 0 1
(integer) 0
127.0.0.1:6379> getbit k1 0
(integer) 1
取字符串类型键中值时1的二进制位的个数
127.0.0.1:6379> bitcount k1
(integer) 27
127.0.0.1:6379> get k1
"\xe1bcdefg"
返回list的长度
127.0.0.1:6379> get list1
(nil)
127.0.0.1:6379> lpush list1 1 2 3
(integer) 3
127.0.0.1:6379> get list1
(error) WRONGTYPE Operation against a key holding the wrong kind of value
返回list的长度
127.0.0.1:6379> lpush list1 1 2 3
(integer) 3
127.0.0.1:6379> lpush list1 4
(integer) 4
存储顺序如4 3 2 1
返回被弹出元素
127.0.0.1:6379> lpop list1
"4"
127.0.0.1:6379> lpop list1
"3"
127.0.0.1:6379> lpop list1
"2"
127.0.0.1:6379> lpop list1
"1"
127.0.0.1:6379> lpop list1
(nil)
右边弹出,并返回被弹出元素
127.0.0.1:6379> lpush list1 1 2 3
(integer) 3
127.0.0.1:6379> rpop list1
"1"
127.0.0.1:6379> rpop list1
"2"
127.0.0.1:6379> rpop list1
"3"
127.0.0.1:6379> rpop list1
(nil)
以阻塞方式从key中弹出并返回第一个值,只弹出和返回第一个非空的key
127.0.0.1:6379> lpush list1 1 2 3
(integer) 3
127.0.0.1:6379> blpop list1 2
1) "list1"
2) "3"
127.0.0.1:6379> blpop list1 list2 2
1) "list1"
2) "2"
超时时,如果没有值可返回,则返回nil
127.0.0.1:6379> blpop list1 2
(nil)
(3.01s)
127.0.0.1:6379> lpush list1 1 2 3
(integer) 3
127.0.0.1:6379> blpop list1 2
1) "list1"
2) "3"
求列表长度,不存在或者为空,返回0
127.0.0.1:6379> llen list1
(integer) 0
127.0.0.1:6379> lpush list1 1 2 3
(integer) 3
127.0.0.1:6379> llen list1
(integer) 3
返回list长度
127.0.0.1:6379> rpush list1 a b c
(integer) 6
127.0.0.1:6379> rpop list1
"c"
返回list长度
127.0.0.1:6379> rpushx list1 b
(integer) 6
127.0.0.1:6379> rpushx list2 a
(integer) 0
返回被弹出和后插入的数据
127.0.0.1:6379> rpush list1 a b c
(integer) 3
127.0.0.1:6379> rpush list2 1 2 3
(integer) 3
# 两个列表,分别是
# list1 a b c
# list2 1 2 3
127.0.0.1:6379> rpoplpush list1 list2
"c"
# 第一个列表的尾部元素插入第二个列表头部
# list1 a b
# list2 c 1 2 3
127.0.0.1:6379> rpop list2
"3"
0表示第一位,自后一位表示-1
127.0.0.1:6379> rpop list1
(nil)
127.0.0.1:6379> lpush list1 a b c
(integer) 3
127.0.0.1:6379> lindex list1 0
"c"
127.0.0.1:6379> lindex list1 -1
"a"
否则返回插入后list的长度
127.0.0.1:6379> linsert list1 before b value1
(integer) 4
127.0.0.1:6379> lrange list1 0 3
1) "c"
2) "value1"
3) "b"
4) "a"
127.0.0.1:6379> linsert list1 before d value1
(integer) -1
如果stop大于list长度,返回所有元素
127.0.0.1:6379> lrange list1 3 0
(empty list or set)
127.0.0.1:6379> lrange list1 0 100
1) "c"
2) "value1"
3) "b"
4) "a"
如果count等于0,则删除所有值等于value的元素
127.0.0.1:6379> lpush list5 1 2 2 1 3 5
(integer) 6
127.0.0.1:6379> lrange list5 0 100
1) "5"
2) "3"
3) "1"
4) "2"
5) "2"
6) "1"
127.0.0.1:6379> lrem list5 0 5
(integer) 1
127.0.0.1:6379> lrange list5 0 100
1) "3"
2) "1"
3) "2"
4) "2"
5) "1"
127.0.0.1:6379> lrem list5 1 1
(integer) 1
127.0.0.1:6379> lrange list5 0 100
1) "3"
2) "2"
3) "2"
4) "1"
127.0.0.1:6379> lrem list5 -1 2
(integer) 1
127.0.0.1:6379> lrange list5 0 100
1) "3"
2) "2"
3) "1"
当index超过list的长度,将会出错
127.0.0.1:6379> lrange list5 0 100
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> lset list5 0 33
OK
127.0.0.1:6379> lrange list5 0 100
1) "33"
2) "2"
3) "1"
127.0.0.1:6379> lset list5 3 33
(error) ERR index out of range
返回被删除的key个数
127.0.0.1:6379> del list5 list1
(integer) 2
127.0.0.1:6379> lrange list5 0 100
(empty list or set)
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379> set k3 v3
OK
127.0.0.1:6379> del k1
(integer) 1
127.0.0.1:6379> del k2 k3
(integer) 2
key不存在就返回nil
127.0.0.1:6379> set k1 abcd123
OK
127.0.0.1:6379> dump k1
"\x00\aabcd123\b\x00\x1b\xb1\xbc|\rG\xda\xeb"
127.0.0.1:6379> del k1
(integer) 1
127.0.0.1:6379> restore k1 10 "\x00\aabcd123\b\x00\x1b\xb1\xbc|\rG\xda\xeb"
OK
127.0.0.1:6379> get k1
(nil)
127.0.0.1:6379> get k1
(nil)
127.0.0.1:6379> restore k1 0 "\x00\aabcd123\b\x00\x1b\xb1\xbc|\rG\xda\xeb"
OK
127.0.0.1:6379> get k1
"abcd123"
成功返回ok
127.0.0.1:6379> restore k1 0 "\x00\aabcd123\b\x00\x1b\xb1\xbc|\rG\xda\xeb"
(error) BUSYKEY Target key name already exists.
127.0.0.1:6379> restore k1 0 "\x00\aabcd123\b\x00\x1b\xb1\xbc|\rG\xda\xeb" replace
OK
如果key重复,返回值会重复判断和+1
127.0.0.1:6379> get k1
"abcd123"
127.0.0.1:6379> get k2
(nil)
127.0.0.1:6379> exists k1
(integer) 1
127.0.0.1:6379> exists k1
(integer) 1
127.0.0.1:6379> exists k1 k1
(integer) 2
127.0.0.1:6379> exists k2
(integer) 0
设置成功返回1,key不存在或者超时设置失败返回0
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> set k1 v1 10
(error) ERR syntax error
127.0.0.1:6379> expire k1 2
(integer) 1
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> get k1
(nil)
127.0.0.1:6379> set k1 v1 5
(error) ERR syntax error
127.0.0.1:6379> get k1
(nil)
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> expire k1 5
(integer) 1
127.0.0.1:6379> set k1 v2
OK
127.0.0.1:6379> get k1
"v2"
127.0.0.1:6379> get k1
"v2"
127.0.0.1:6379> get k1
"v2"
127.0.0.1:6379> get k1
"v2"
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> expire k1 10
(integer) 1
127.0.0.1:6379> persist k1
(integer) 1
127.0.0.1:6379> get k1
"v1"
1表示成功,0表示key不存在或者key没有设置超时设置
127.0.0.1:6379> persist k100
(integer) 0
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> persist k1
(integer) 0
1表示成功,0表示key不存在或者key没有设置超时设置
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> expireat k1 1528965035
(integer) 1
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> get k1
"v1"
# 1528965035 2018-06-14 16:30:35
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> expireat k1 1528965035
(integer) 1
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> get k1
(nil)
[^e]排除e,[a-d]代表a,b,c,d
127.0.0.1:6379> mset a 1 b 2 c 3 d 4
OK
127.0.0.1:6379> keys [a-c]
1) "a"
2) "c"
3) "b"
(0.58s)
key没有超时设置,返回-1
127.0.0.1:6379> set k 1 ex 10
OK
127.0.0.1:6379> set k2 2
OK
127.0.0.1:6379> ttl k
(integer) 1
127.0.0.1:6379> ttl k2
(integer) -1
127.0.0.1:6379> ttl k3
(integer) -2
返回的是毫秒生存时间
127.0.0.1:6379> set k1 v1 EX 100
OK
127.0.0.1:6379> pttl k1
(integer) 97195
如果迭代过程中,有key被删除了,但是迭代完成前没有被添加进来,则此key并不会出现在迭代中
127.0.0.1:6379> scan 0
1) "5505024"
2) 1) "key:000051260311"
2) "key:000036007278"
3) "key:000059694484"
4) "key:000006587804"
5) "key:000004199401"
6) "key:000094197164"
7) "key:000099600200"
8) "key:000051222239"
9) "key:000075613120"
10) "key:000058266859"
11) "key:000076128079"
127.0.0.1:6379> scan 10
1) "1835018"
2) 1) "key:000075289699"
2) "key:000078347835"
3) "key:000071808967"
4) "key:000066680858"
5) "key:000020401759"
6) "key:000087370174"
7) "key:000089373903"
8) "key:000094725692"
9) "key:000077304879"
10) "key:000019052151"
127.0.0.1:6379> scan 20
1) "7077908"
2) 1) "key:000005041067"
2) "key:000045121774"
3) "key:000095755193"
4) "key:000006008892"
5) "key:000041833711"
6) "key:000064230555"
7) "key:000074550342"
8) "key:000084491604"
9) "key:000096452813"
10) "key:000094200031"
127.0.0.1:6379> scan 0 count 20
1) "1179648"
2) 1) "key:000051260311"
2) "key:000036007278"
3) "key:000059694484"
4) "key:000006587804"
5) "key:000004199401"
6) "key:000094197164"
7) "key:000099600200"
8) "key:000051222239"
9) "key:000075613120"
10) "key:000058266859"
11) "key:000076128079"
12) "key:000051853542"
13) "key:000091156937"
14) "key:000072767835"
15) "key:000026187737"
16) "key:000083167013"
17) "key:000057492750"
18) "key:000027269441"
19) "key:000012211723"
20) "key:000058915500"
127.0.0.1:6379> scan 0 match key:00005126031*
1) "5505024"
2) 1) "key:000051260311"
127.0.0.1:6379> scan 0 match 1*
1) "5505024"
2) (empty list or set)
如果field已经存在,成功更新旧值,返回0
127.0.0.1:6379> hset dict1 name yzw
(integer) 1
127.0.0.1:6379> hset dict1 age 40
(integer) 1
127.0.0.1:6379> hset dict1 age 18
(integer) 0
成功返回OK
127.0.0.1:6379> HMSET map1 name yzw age 40
OK
如果field存在,不做任何操作,返回0
127.0.0.1:6379> hkeys dict1
1) "name"
2) "age"
127.0.0.1:6379> hsetnx dict1 age 18
(integer) 0
127.0.0.1:6379> hsetnx dict1 addr gz
(integer) 1
127.0.0.1:6379>
返回key列表
127.0.0.1:6379> hkeys dict1
1) "name"
2) "age"
3) "addr"
获取key对应的map的filed数量,也就是字典长度或者元素个数
127.0.0.1:6379> hlen dict1
(integer) 3
如果key不存在或者field不存在,返回nil,否则返回值
127.0.0.1:6379> hget dict1 name
"yzw"
127.0.0.1:6379> hget dict1 salary
(nil)
127.0.0.1:6379> hget ddd name
(nil)
不存在返回nil
127.0.0.1:6379> hmget dict1 name age addr salary
1) "yzw"
2) "18"
3) "gz"
4) (nil)
不存在返回0
127.0.0.1:6379> hstrlen dict1 name
(integer) 3
127.0.0.1:6379> hstrlen dict1 salary
(integer) 0
返回删除的值的个数
127.0.0.1:6379> hdel dict1 addr age
(integer) 2
127.0.0.1:6379> hmget dict1 name age addr
1) "yzw"
2) (nil)
3) (nil)
否则返回1
127.0.0.1:6379> hexists dict1 name
(integer) 1
127.0.0.1:6379> hexists dict1 age
(integer) 0
返回key对应的map的所有k/v对
127.0.0.1:6379> hgetall dict1
1) "name"
2) "yzw"
返回增加后的结果,不存在则加入元素
127.0.0.1:6379> hgetall dict1
1) "name"
2) "yzw"
3) "age"
4) "18"
127.0.0.1:6379> hincrby dict1 age 2
(integer) 20
127.0.0.1:6379> hincrby dict1 salary 100
(integer) 100
127.0.0.1:6379> hgetall dict1
1) "name"
2) "yzw"
3) "age"
4) "20"
5) "salary"
6) "100"
浮点
127.0.0.1:6379> hset dict1 salary 100.1
(integer) 0
127.0.0.1:6379> hgetall dict1
1) "name"
2) "yzw"
3) "age"
4) "20"
5) "salary"
6) "100.1"
127.0.0.1:6379> hincrbyfloat dict1 salary 1
"101.1"
127.0.0.1:6379> hincrbyfloat dict1 salary 0.1
"101.2"
迭代hash里面的value值
127.0.0.1:6379> hscan dict1 10
1) "0"
2) 1) "name"
2) "yzw"
3) "age"
4) "20"
5) "salary"
6) "101.2"
127.0.0.1:6379> hscan dict1 10 MATCH n*
1) "0"
2) 1) "name"
2) "yzw"
127.0.0.1:6379> hscan dict1 10 MATCH *a*
1) "0"
2) 1) "name"
2) "yzw"
3) "age"
4) "20"
5) "salary"
6) "101.2"
127.0.0.1:6379> hscan dict1 10 MATCH *a* COUNT 1
1) "0"
2) 1) "name"
2) "yzw"
3) "age"
4) "20"
5) "salary"
6) "101.2"
标签:包括 覆盖 3.3 res 次数 指定 2.3 star 失败
原文地址:https://www.cnblogs.com/jenvid/p/9186601.html