append:如果key存在,那么直接在value追加,如果Key不存在,建立一个key并设置为空字符串
127.0.0.1:6379> exists mk
(integer) 0
127.0.0.1:6379> append mk "Hello"
(integer) 5
127.0.0.1:6379> append mk " World!"
(integer) 12
127.0.0.1:6379> get mk
"Hello World!"
127.0.0.1:6379> strlen mk
(integer) 12
127.0.0.1:6379> getrange mk 0 4
"Hello"
127.0.0.1:6379> getrange mk 5 12
" World!"
127.0.0.1:6379> setrange mk 6 "gaoquan"
(integer) 12
127.0.0.1:6379> get mk
"Hello gaoquan"
auth:如果配置文件设置了密码,那么客户端想操作redis,需要使用此命令
127.0.0.1:6379> get mk
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth root123
OK
127.0.0.1:6379> get mk
(nil)
bgrewriteaof:重新持久化redis到appendonly.aof文件,aof中冗余的key value会被删除,存储实际的内容
bgsave:保存dump.rdb,进行rdb备份,也是一种持久化方式
bitcount: 计算指定区间的bit数,字母4bit,数字3bit,特殊字符2bit
127.0.0.1:6379> set mykey "$11111"
OK
127.0.0.1:6379> bitcount mykey 0 5
(integer) 17
127.0.0.1:6379> set mykey "$$$$$$"
OK
127.0.0.1:6379> bitcount mykey 0 5
(integer) 12
bitop:对给定值进行按位与或异或操作
127.0.0.1:6379> set key1 "000"
OK
127.0.0.1:6379> set key2 "111"
OK
127.0.0.1:6379> bitop and dest key1 key2
(integer) 3
127.0.0.1:6379> get dest
"000"
127.0.0.1:6379> bitop or dest key1 key2
(integer) 3
127.0.0.1:6379> get dest
"111"
127.0.0.1:6379> bitop xor dest key1 key2
(integer) 3
127.0.0.1:6379> get dest
"\x01\x01\x01"
bitpos:查找到第一个为0或者1的位置
127.0.0.1:6379> SET mykey "\xff\xf0\x00" 注:"\xff\xf0\x00"转换为二进制为111111111111000000000000
OK
127.0.0.1:6379> bitpos mykey 0
(integer) 12
127.0.0.1:6379> bitpos mykey 1
(integer) 0
blpop:弹出遇到列表中的第一个非空列表的第一个元素,当所有列表都为空时,那么会阻塞,此时如果加入新的列表元素,命令继续执行
127.0.0.1:6379> rpush list1 a b
(integer) 2
127.0.0.1:6379> rpush list2 c d
(integer) 2
127.0.0.1:6379> blpop list1 list2 0
1) "list1"
2) "a"
127.0.0.1:6379> blpop list1 list2 0
1) "list1"
2) "b"
127.0.0.1:6379> blpop list1 list2 0
1) "list2"
2) "c"
127.0.0.1:6379> blpop list1 list2 0
1) "list2"
2) "d"
127.0.0.1:6379> blpop list1 list2 0 注:再开一个session执行rpush list1 a,否则命令会一直阻塞
1) "list1"
2) "a"
(6.10s)
brpop:弹出遇到第一个非空列表中的最后一个元素,当所有列表都为空时,那么会阻塞,此时如果加入新的列表元素,命令继续执行
brlpoprpush:从A列表中弹出元素到B列表,当没元素时阻塞。主要用来做安全队列。
(消费者端取到消息的同时把该消息放入一个正在处理中的列表。 当消息被处理了之后,该命令会使用 LREM 命令来移除正在处理中列表中的对应消息。
另外,可以添加一个客户端来监控这个正在处理中列表,如果有某些消息已经在这个列表中存在很长时间了(即超过一定的处理时限), 那么这个客户端会把这些超时消息重新加入到队列中)
127.0.0.1:6379> rpush mylist1 "red" "yellow" "blue"
(integer) 3
127.0.0.1:6379> lrange mylist1 0 -1
1) "red"
2) "yellow"
3) "blue"
127.0.0.1:6379> brpoplpush mylist1 mylist2 0
"blue"
127.0.0.1:6379> brpoplpush mylist1 mylist2 0
"yellow"
127.0.0.1:6379> brpoplpush mylist1 mylist2 0
"red"
127.0.0.1:6379> brpoplpush mylist1 mylist2 0
"black"
(797.37s)
127.0.0.1:6379> lrange mylist1 0 -1
(empty list or set)
127.0.0.1:6379> lrange mylist2 0 -1
1) "black"
2) "red"
3) "yellow"
4) "blue"
client kill:断开客户端与redis-server的链接
127.0.0.1:6379> client list
id=2 addr=127.0.0.1:57613 fd=7 name= age=8933 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
id=3 addr=127.0.0.1:57614 fd=8 name= age=3346 idle=91 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=rpush
127.0.0.1:6379> client kill 127.0.0.1:57614
OK
client list:获得客户端连接列表
127.0.0.1:6379> client list
id=2 addr=127.0.0.1:57613 fd=7 name= age=8933 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
id=3 addr=127.0.0.1:57614 fd=8 name= age=3346 idle=91 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=rpush
client setname:设置当前client的名称
client getname:获取当前client的名称
127.0.0.1:6379> client setname 71
OK
127.0.0.1:6379> client list
id=2 addr=127.0.0.1:57613 fd=7 name= age=9464 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
id=4 addr=127.0.0.1:57615 fd=8 name=71 age=498 idle=95 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=client
127.0.0.1:6379> client GETNAME
"71"
client pause 时间(毫秒):将所有的client暂停执行
127.0.0.1:6379> client pause 10000
OK
client reply {on|off|skip}:3.2才支持
command:列出所有的redis命令
command getkeys:获取key
127.0.0.1:6379> command getkeys mset a b c d e f
1) "a"
2) "c"
3) "e"
command info:查看redis命令的信息
127.0.0.1:6379> command info get
1) 1) "get"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
config get:获取配置文件的参数
127.0.0.1:6379> config get
(error) ERR Wrong number of arguments for CONFIG get
127.0.0.1:6379> config get "*pass*"
1) "requirepass"
2) "root123"
127.0.0.1:6379> config get "*slave*"
1) "repl-ping-slave-period"
2) "10"
3) "slave-priority"
4) "100"
5) "min-slaves-to-write"
6) "0"
7) "min-slaves-max-lag"
8) "10"
9) "cluster-slave-validity-factor"
10) "10"
11) "slave-serve-stale-data"
12) "yes"
13) "slave-read-only"
14) "yes"
15) "slaveof"
16) ""
config rewrite:重写redis配置文件,并生效,不止内存中生效,实际配置文件也生效,include开头的参数不能被重写。
127.0.0.1:6379> auth root123
OK
127.0.0.1:6379> config set requirepass root456
OK
127.0.0.1:6379> config rewrite
OK
127.0.0.1:6379> auth root456
OK
config set:更改内存中的参数,配置文件不跟随生效,重启失效
127.0.0.1:6379> config set requirepass world
OK
127.0.0.1:6379> auth world
OK
config resetstat:充值redis的静态统计信息,具体可先执行info,观察输出信息,然后再使用config resetstat,然后再观察info信息。
dbsize:获取数据库共有多少个key,可通过keys *来验证
127.0.0.1:6379> dbsize
(integer) 11
debug object key:获取key的一些信息
127.0.0.1:6379> debug object mykey
Value at:0x7fdb02035e60 refcount:1 encoding:embstr serializedlength:4 lru:13543108 lru_seconds_idle:90074
debug segfault:执行一个非法的内存指令,从而让redis server崩溃,用于bug调试
127.0.0.1:6379> debug segfault
Could not connect to Redis at 127.0.0.1:6379: Connection refused
(0.80s)
not connected> auth world
Could not connect to Redis at 127.0.0.1:6379: Connection refused
decr key:将key中储存的数字减1,key不存在则将value置为0,类型不是整型则返回一个错误
127.0.0.1:6379> set num 10
OK
127.0.0.1:6379> decr num
(integer) 9
127.0.0.1:6379> get num
"9"
127.0.0.1:6379> decr num1
(integer) -1
127.0.0.1:6379> get num1
"-1"
127.0.0.1:6379> decr num1
(integer) -2
127.0.0.1:6379> get num1
"-2"
127.0.0.1:6379> decr name
(error) ERR value is not an integer or out of range
decrby key decrement:将key所存储的值减去指定的数值
127.0.0.1:6379> get num
"9"
127.0.0.1:6379> decrby num 2
(integer) 7
127.0.0.1:6379> get num
"7"
del key,[key1,key2]:删除key
127.0.0.1:6379> get num
"9"
127.0.0.1:6379> decrby num 2
(integer) 7
127.0.0.1:6379> get num
"7"
discard:取消事务,放弃执行事务块内所有的指令
127.0.0.1:6379> multi
OK
127.0.0.1:6379> incr a
QUEUED
127.0.0.1:6379> incr b
QUEUED
127.0.0.1:6379> exec
1) (error) ERR value is not an integer or out of range
2) (integer) 1
127.0.0.1:6379> multi
OK
127.0.0.1:6379> incr a
QUEUED
127.0.0.1:6379> incr b
QUEUED
127.0.0.1:6379> discard
OK
127.0.0.1:6379> exec
(error) ERR EXEC without MULTI
dump key:序列化给定的key,返回序列化的值
restore key 0 (dump key):反序列化给定的序列化值,并将它和给定的key绑定
127.0.0.1:6379> dump mykey
"\x00\xc0\n\x06\x00\xf8r?\xc5\xfb\xfb_("
127.0.0.1:6379> del mykey
(integer) 1
127.0.0.1:6379> restore mykey 0 "\x00\xc0\n\x06\x00\xf8r?\xc5\xfb\xfb_("
OK
127.0.0.1:6379> get mykey
"10"
echo:显示给定的值
127.0.0.1:6379> echo "helloworld"
"helloworld"
eval:通过内置的 Lua 解释器,可以使用 EVAL 命令对 Lua 脚本进行求值
http://doc.redisfans.com/script/eval.html
evalsha:根据给定的 sha1 校验码,对缓存在服务器中的脚本进行求值
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> eval "return redis.call('get','foo')" 0
"bar"
127.0.0.1:6379> script load "return redis.call('get','foo')"
"6b1bf486c81ceb7edf3c093f4c48582e38c0e791"
127.0.0.1:6379> evalsha 6b1bf486c81ceb7edf3c093f4c48582e38c0e791 0
"bar"
exec:在multi执行所有的命令
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set name "yellow"
QUEUED
127.0.0.1:6379> append name " blue"
QUEUED
127.0.0.1:6379> append name " black"
QUEUED
127.0.0.1:6379> exec
1) OK
2) (integer) 11
3) (integer) 17
127.0.0.1:6379> get name
"yellow blue black"
exists key:判断key是否存在
127.0.0.1:6379> keys *
1) "key2"
2) "dest"
3) "mykey"
4) "num1"
5) "name"
6) "list1"
7) "b"
8) "mylist2"
9) "c"
10) "foo"
11) "key1"
12) "list2"
13) "a"
14) "e"
127.0.0.1:6379> exists name b list5
(integer) 2
127.0.0.1:6379> exists name b list1
(integer) 3
expire key seconds:key生效时间,时间过后回收
127.0.0.1:6379> set name "gaoquan"
OK
127.0.0.1:6379> get name
"gaoquan"
127.0.0.1:6379> expire name 2
(integer) 1
127.0.0.1:6379> get name
(nil)
expireat key timestamp:设置过期时间到一个指定的unix时间戳
[root@mysql57-71 ~]# date -d '2016-02-26 17:16' +%s
1456478160
127.0.0.1:6379> set name "gaoquan"
OK
127.0.0.1:6379> expireat name 1456478160
(integer) 1
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> exists name
(integer) 0
flushall:清除所有数据库信息
flushdb:清除当前数据库的所有key
get key:获取key对应的value,value只能为string
127.0.0.1:6379> get key2
"111"
setbit key offset value:设置指定key偏移量的位置的值,场景主要用作统计活跃用户
getbit key offset:获取指定Key偏移量的位置的值
127.0.0.1:6379> setbit mykey1 5 1
(integer) 0
127.0.0.1:6379> get mykey1
"G"
getrange key start end:获取key的指定区间的子字符串
127.0.0.1:6379> set mykey "hello world!"
OK
127.0.0.1:6379> getrange mykey 0 -1
"hello world!"
127.0.0.1:6379> getrange mykey 0 1
"he"
127.0.0.1:6379>
getset key value:返回key的旧值并将值用指定的value替代
127.0.0.1:6379> set foo "bar"
OK
127.0.0.1:6379> getset foo "run"
"bar"
127.0.0.1:6379> get foo
"run"
转载于:https://blog.51cto.com/gaoquan/1745062