redis优势
- Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
- Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
- Redis支持数据的备份,即master-slave模式的数据备份。
redis配置
- 示例
127.0.0.1:6379> CONFIG GET *
1) "dbfilename"
2) "dump.rdb"
3) "requirepass"
4) ""
5) "masterauth"
6) ""
7) "cluster-announce-ip"
8) ""
9) "unixsocket"
10) ""
11) "logfile"
12) "/var/log/redis/redis-server.log"
13) "pidfile"
14) "/var/run/redis/redis-server.pid"
15) "slave-announce-ip"
16) ""
17) "maxmemory"
18) "0"
19) "proto-max-bulk-len"
20) "536870912"
21) "client-query-buffer-limit"
22) "1073741824"
23) "maxmemory-samples"
24) "5"
25) "lfu-log-factor"
26) "10"
27) "lfu-decay-time"
28) "1"
29) "timeout"
30) "0"
31) "active-defrag-threshold-lower"
32) "10"
33) "active-defrag-threshold-upper"
34) "100"
35) "active-defrag-ignore-bytes"
36) "104857600"
37) "active-defrag-cycle-min"
38) "25"
39) "active-defrag-cycle-max"
40) "75"
41) "auto-aof-rewrite-percentage"
42) "100"
43) "auto-aof-rewrite-min-size"
44) "67108864"
45) "hash-max-ziplist-entries"
46) "512"
47) "hash-max-ziplist-value"
48) "64"
49) "list-max-ziplist-size"
50) "-2"
51) "list-compress-depth"
52) "0"
53) "set-max-intset-entries"
54) "512"
55) "zset-max-ziplist-entries"
56) "128"
57) "zset-max-ziplist-value"
58) "64"
59) "hll-sparse-max-bytes"
60) "3000"
61) "lua-time-limit"
62) "5000"
63) "slowlog-log-slower-than"
64) "10000"
65) "latency-monitor-threshold"
66) "0"
67) "slowlog-max-len"
68) "128"
69) "port"
70) "6379"
71) "cluster-announce-port"
72) "0"
73) "cluster-announce-bus-port"
74) "0"
75) "tcp-backlog"
76) "511"
77) "databases"
78) "16"
79) "repl-ping-slave-period"
80) "10"
81) "repl-timeout"
82) "60"
83) "repl-backlog-size"
84) "1048576"
85) "repl-backlog-ttl"
86) "3600"
87) "maxclients"
88) "10000"
89) "watchdog-period"
90) "0"
91) "slave-priority"
92) "100"
93) "slave-announce-port"
94) "0"
95) "min-slaves-to-write"
96) "0"
97) "min-slaves-max-lag"
98) "10"
99) "hz"
100) "10"
101) "cluster-node-timeout"
102) "15000"
103) "cluster-migration-barrier"
104) "1"
105) "cluster-slave-validity-factor"
106) "10"
107) "repl-diskless-sync-delay"
108) "5"
109) "tcp-keepalive"
110) "300"
111) "cluster-require-full-coverage"
112) "yes"
113) "cluster-slave-no-failover"
114) "no"
115) "no-appendfsync-on-rewrite"
116) "no"
117) "slave-serve-stale-data"
118) "yes"
119) "slave-read-only"
120) "yes"
121) "stop-writes-on-bgsave-error"
122) "yes"
123) "daemonize"
124) "yes"
125) "rdbcompression"
126) "yes"
127) "rdbchecksum"
128) "yes"
129) "activerehashing"
130) "yes"
131) "activedefrag"
132) "no"
133) "protected-mode"
134) "yes"
135) "repl-disable-tcp-nodelay"
136) "no"
137) "repl-diskless-sync"
138) "no"
139) "aof-rewrite-incremental-fsync"
140) "yes"
141) "aof-load-truncated"
142) "yes"
143) "aof-use-rdb-preamble"
144) "no"
145) "lazyfree-lazy-eviction"
146) "no"
147) "lazyfree-lazy-expire"
148) "no"
149) "lazyfree-lazy-server-del"
150) "no"
151) "slave-lazy-flush"
152) "no"
153) "maxmemory-policy"
154) "noeviction"
155) "loglevel"
156) "notice"
157) "supervised"
158) "no"
159) "appendfsync"
160) "everysec"
161) "syslog-facility"
162) "local0"
163) "appendonly"
164) "no"
165) "dir"
166) "/var/lib/redis"
167) "save"
168) "900 1 300 10 60 10000"
169) "client-output-buffer-limit"
170) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
171) "unixsocketperm"
172) "0"
173) "slaveof"
174) ""
175) "notify-keyspace-events"
176) ""
177) "bind"
178) "127.0.0.1 ::1"
redis.conf 配置项说明如下:
-
Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程
daemonize no
-
当Redis以守护进程方式运行时,Redis默认会把pid写入/var/run/redis.pid文件,可以通过pidfile指定
pidfile /var/run/redis.pid
-
指定Redis监听端口,默认端口为6379,作者在自己的一篇博文中解释了为什么选用6379作为默认端口,因为6379在手机按键上MERZ对应的号码,而MERZ取自意大利歌女Alessia Merz的名字
port 6379
-
绑定的主机地址
bind 127.0.0.1
-
当 客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
timeout 300
-
指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为verbose
loglevel verbose
-
日志记录方式,默认为标准输出,如果配置Redis为守护进程方式运行,而这里又配置为日志记录方式为标准输出,则日志将会发送给/dev/null
logfile stdout
-
设置数据库的数量,默认数据库为0,可以使用SELECT 命令在连接上指定数据库id
databases 16
-
指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合
save
Redis默认配置文件中提供了三个条件:
save 900 1
save 300 10
save 60 10000
分别表示900秒(15分钟)内有1个更改,300秒(5分钟)内有10个更改以及60秒内有10000个更改。
-
指定存储至本地数据库时是否压缩数据,默认为yes,Redis采用LZF压缩,如果为了节省CPU时间,可以关闭该选项,但会导致数据库文件变的巨大
rdbcompression yes
-
指定本地数据库文件名,默认值为dump.rdb
dbfilename dump.rdb
-
指定本地数据库存放目录
dir ./
-
设置当本机为slav服务时,设置master服务的IP地址及端口,在Redis启动时,它会自动从master进行数据同步
slaveof
-
当master服务设置了密码保护时,slav服务连接master的密码
masterauth
-
设置Redis连接密码,如果配置了连接密码,客户端在连接Redis时需要通过AUTH 命令提供密码,默认关闭
requirepass foobared
-
设置同一时间最大客户端连接数,默认无限制,Redis可以同时打开的客户端连接数为Redis进程可以打开的最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis会关闭新的连接并向客户端返回max number of clients reached错误信息
maxclients 128
-
指定Redis最大内存限制,Redis在启动时会把数据加载到内存中,达到最大内存后,Redis会先尝试清除已到期或即将到期的Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis新的vm机制,会把Key存放内存,Value会存放在swap区
maxmemory
-
指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为 redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为no
appendonly no
-
指定更新日志文件名,默认为appendonly.aof
appendfilename appendonly.aof
-
指定更新日志条件,共有3个可选值:
no:表示等操作系统进行数据缓存同步到磁盘(快)
always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全)
everysec:表示每秒同步一次(折中,默认值)appendfsync everysec
-
指定是否启用虚拟内存机制,默认值为no,简单的介绍一下,VM机制将数据分页存放,由Redis将访问量较少的页即冷数据swap到磁盘上,访问多的页面由磁盘自动换出到内存中(在后面的文章我会仔细分析Redis的VM机制)
vm-enabled no
-
虚拟内存文件路径,默认值为/tmp/redis.swap,不可多个Redis实例共享
vm-swap-file /tmp/redis.swap
-
将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多小,所有索引数据都是内存存储的(Redis的索引数据 就是keys),也就是说,当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0
vm-max-memory 0
-
Redis swap文件分成了很多的page,一个对象可以保存在多个page上面,但一个page上不能被多个对象共享,vm-page-size是要根据存储的 数据大小来设定的,作者建议如果存储很多小对象,page大小最好设置为32或者64bytes;如果存储很大大对象,则可以使用更大的page,如果不 确定,就使用默认值
vm-page-size 32
-
设置swap文件中的page数量,由于页表(一种表示页面空闲或使用的bitmap)是在放在内存中的,,在磁盘上每8个pages将消耗1byte的内存。
vm-pages 134217728
-
设置访问swap文件的线程数,最好不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的,可能会造成比较长时间的延迟。默认值为4
vm-max-threads 4
-
设置在向客户端应答时,是否把较小的包合并为一个包发送,默认为开启
glueoutputbuf yes
-
指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法
hash-max-zipmap-entries 64
hash-max-zipmap-value 512
-
指定是否激活重置哈希,默认为开启(后面在介绍Redis的哈希算法时具体介绍)
activerehashing yes
-
指定包含其它的配置文件,可以在同一主机上多个Redis实例之间使用同一份配置文件,而同时各个实例又拥有自己的特定配置文件
include /path/to/local.conf
键命令
- DEL key (删除该键)
127.0.0.1:6379> del ccy
(integer) 1
- DUMP key (获取该键对应值序列化后返回)
127.0.0.1:6379> dump first
"\x00\x05/ne_1\b\x00\"\x01\xa3\xfc\xc8\xdb\xf9/"
- EXISTS key (返回该键是否存在的结果)
127.0.0.1:6379> exists first
(integer) 1
- EXPIRE key second (设置该键的超时时间)
- TTL key (返回该键剩余时间 秒)
- PTTL key (返回该键剩余时间 毫秒)
127.0.0.1:6379> expire first 10000
(integer) 1
127.0.0.1:6379> ttl first
(integer) 9991
127.0.0.1:6379> pttl first
(integer) 9985606
- EXPIREAT key timestamp (设置key的过期时间,参数为时间戳,以秒计)
- PEXPIRE key milliseconds (设置该的超时时间 毫秒)
127.0.0.1:6379> pexpire second 100000000
(integer) 1
127.0.0.1:6379> ttl second
(integer) 99991
127.0.0.1:6379> pttl second
(integer) 99982512
- PEXPIREAT key milliseconds-timestamp (设置key的过期时间,参数为时间戳,以豪秒计)
- PERSIST key
127.0.0.1:6379> expire first 100
(integer) 1
127.0.0.1:6379> persist first
(integer) 1
127.0.0.1:6379> ttl first
(integer) -1
- KEYS pattern (返回符合该正则表达式的所有键)
127.0.0.1:6379> keys *
1) "first"
2) "second"
3) "third"
- MOVE key db (将该键移动到对应数据库)
- TYPE key (返回该键对应值类型)
127.0.0.1:6379> type first
string
- RENAME key newkey (将该key名重命名为newkey,若newkey存在,则覆盖)
127.0.0.1:6379> del first
(integer) 0
127.0.0.1:6379> del second
(integer) 1
127.0.0.1:6379> set first one
OK
127.0.0.1:6379> set second two
OK
127.0.0.1:6379> rename first second
OK
127.0.0.1:6379> exists first
(integer) 0
127.0.0.1:6379> get second
"one"
- RENAMENX key newkey (将该key名重命名为newkey,若newkey存在,则不进行操作)
127.0.0.1:6379> renamenx first_1 first
(integer) 1
127.0.0.1:6379> renamenx first second
(integer) 0
- RANDOMKEY (返回任意键名)
127.0.0.1:6379> randomkey
"second"
字符串命令
- SET key value (设置键值对)
127.0.0.1:6379> set first one
OK
- GET key (获取该键对应值)
127.0.0.1:6379> get first
"one"
- GETRANGE ket start end (获取该键对应支付串的切片)
127.0.0.1:6379> getrange first 0 1
"on"
- GETSET key value (重新设置该键值,并返回旧值)
127.0.0.1:6379> getset first one_1
"one"
127.0.0.1:6379> get first
"one_1"
-
GETBIT key offset ()
-
MGET key1 [key2…] (获取多个值)
127.0.0.1:6379> mget first second
1) "one_1"
2) "two"
- SETBIT key offet value
127.0.0.1:6379> setbit first 1 0
(integer) 1
127.0.0.1:6379> get first
"/ne_1"
- SETEX key second value (设置键值对,同时设置超时时间,秒)
127.0.0.1:6379> setex third 1000 three
OK
127.0.0.1:6379> ttl third
(integer) 931
- PSETEX key millisecond value (设置键值对,同时设置超时时间,毫秒)
127.0.0.1:6379> psetex first 100000000 one
OK
127.0.0.1:6379> pttl first
(integer) 99995360
- SETNX key value (若该键不存在则新建)
127.0.0.1:6379> setnx first aaa
(integer) 0
- SETRANGE key offset value (从offset开始将value替换上原先的值)
127.0.0.1:6379> get first
"one"
127.0.0.1:6379> setrange first 0 1_one
(integer) 5
127.0.0.1:6379> get first
"1_one"
- STRLEN key (返回该键对应值的长度)
127.0.0.1:6379> get first
"1_one"
127.0.0.1:6379> strlen first
(integer) 5
- MSET key value [key value …] (同时设置多个键值对)
127.0.0.1:6379> mset first one second two third three
OK
127.0.0.1:6379> keys *
1) "first"
2) "second"
3) "third"
- MSETNX key value [key value …] (同时设置多个键值对,当且近当所以键都不存在)
127.0.0.1:6379> msetnx first one fourth four
(integer) 0
127.0.0.1:6379> mset this here that there
OK
127.0.0.1:6379> keys *
1) "first"
2) "second"
3) "that"
4) "this"
5) "third"
- INCR key (将该键对应数字值加1)
127.0.0.1:6379> set four 4
OK
127.0.0.1:6379> incr four
(integer) 5
127.0.0.1:6379> get four
"5"
- INCRBY key increment (将该键对应数字值加increment)
127.0.0.1:6379> get five
"5"
127.0.0.1:6379> incrby five 10
(integer) 15
127.0.0.1:6379> get five
"15"
- INCRBYFLOAT key increment (将该键对应数字值加浮点数)
127.0.0.1:6379> get five
"15"
127.0.0.1:6379> incrbyfloat five 0.123
"15.123"
- DECR key (将该键对应数字值减一)
127.0.0.1:6379> set five 5
OK
127.0.0.1:6379> decr five
(integer) 4
- DECRBY key decrement (将该键对应数字值减decrement)
127.0.0.1:6379> decr five
(integer) 4
127.0.0.1:6379> decrby five 1
(integer) 3
127.0.0.1:6379> decrby five 3
(integer) 0
- APPEND key value (在该键对应值后面拼接value)
127.0.0.1:6379> set first one
OK
127.0.0.1:6379> append first _01
(integer) 6
127.0.0.1:6379> get first
"one_01"
哈希命令
- HSET key filed name (对key设置字段及其值)
127.0.0.1:6379> hset person tel 0987654321
(integer) 1
127.0.0.1:6379> hget person tel
"0987654321"
- HSETNX key field value (字段不存在时设置字段)
127.0.0.1:6379> hsetnx person name mos
(integer) 0
- HMSET key field name [field name …]
127.0.0.1:6379> hmset person name ccy age 18 tel 123456789
OK
- HGETALL key (获取一个hash的所有字段值)
127.0.0.1:6379> hgetall person
1) "name"
2) "ccy"
3) "age"
4) "18"
5) "tel"
6) "123456789"
- HDEL key field1 [field2 …] (删除一个hash的一个或多个字段)
127.0.0.1:6379> hdel person tel
(integer) 1
127.0.0.1:6379> hgetall person
1) "name"
2) "ccy"
3) "age"
4) "18"
- hexists key field (检查hash中是否存在该字段)
127.0.0.1:6379> hexists person name
(integer) 1
- hget key field (获取字段值)
127.0.0.1:6379> hmget person name
1) "ccy"
- hmget key field1 [field2 …] (获取一个或多个字段值)
127.0.0.1:6379> hmget person name age
1) "ccy"
2) "20.2"
- hkeys key (获取一个hash的所有字段名)
127.0.0.1:6379> hkeys person
1) "name"
2) "age"
- hlen key (获取字段数)
127.0.0.1:6379> hlen person
(integer) 2
- hincrby key field increment (对象的某一属性曾计算)
127.0.0.1:6379> hincrby person age 2
(integer) 20
- hincrbyfloat key field increment (对象的某一属性增加一个浮点数)
127.0.0.1:6379> hincrbyfloat person age 0.2
"20.2"
- hvals key (列出对象所有的属性值)
127.0.0.1:6379> hvals person
1) "ccy"
2) "20.2"
- hscan key cursor [match pattern] [count count] (需查询详细用法)
列表命令
- LPUSH key value1 [value2 …] (往列表key头部插入数值)
127.0.0.1:6379> lpush list first second third
(integer) 3
//顺序:third->second->first
- RPUSH key value1 [value2 …] (往列表尾部插入一个或多个值)
127.0.0.1:6379> rpush list first second third
(integer) 3
//顺序:first->second->third
- LRANGE key start end (将列表中的从start到end的元素列出)
127.0.0.1:6379> lrange list 0 4
1) "third"
2) "second"
3) "first"
- BLPOP key1 [key2 …] timeout (移除并获取列表的第一个元素,如果列表没有元素,则会阻到超时或发现可弹出元素为止)
127.0.0.1:6379> blpop list 1
1) "list"
2) "third"
127.0.0.1:6379> blpop list 1
1) "list"
2) "second"
127.0.0.1:6379> blpop list 1
1) "list"
2) "first"
127.0.0.1:6379> blpop list 1
(nil)
(1.09s)
- BRPOP key1 [key2 …] timeout (移除并获取列表的最后元素,如果列表没有元素,则会阻到超时或发现可弹出元素为止)
127.0.0.1:6379> brpop list 1
1) "list"
2) "first"
127.0.0.1:6379> brpop list 1
1) "list"
2) "second"
127.0.0.1:6379> brpop list 1
1) "list"
2) "third"
127.0.0.1:6379> brpop list 1
(nil)
(1.04s)
- BRPOPLPUSH source destination timeout
- RPOPLPUSH source destination
- LINDEX key index (获取列表下标为index的元素)
127.0.0.1:6379> LINDEX list 0
"third"
- LINSERT key BEFORE|AFTER pivot value (在制定数值的前或后插入元素)
127.0.0.1:6379> linsert list after first 0
(integer) 4
127.0.0.1:6379> lrange list 0 10
1) "third"
2) "second"
3) "first"
4) "0"
- LLEN key (获取列表长度)
127.0.0.1:6379> llen list
(integer) 4
- LPOP key (移除并获取列表的第一个元素)
127.0.0.1:6379> lpop list
"third"
- RPOP key (移除并获取列表的最后一个元素)
127.0.0.1:6379> lrange list 0 10
1) "..."
2) "four"
3) "three"
4) "two"
127.0.0.1:6379> RPOP list
"two"
- LPUSHX key value (若列表存在则添加值)
127.0.0.1:6379> LPUSHX listq aaa
(integer) 0
127.0.0.1:6379> LPUSHX list four
(integer) 4
- LREM key count value ()
- count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT
- count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值
- count = 0 : 移除表中所有与 VALUE 相等的值
- LSET key index value (设置列表某个索引的对应值)
127.0.0.1:6379> lpush list one two three four five six seven
(integer) 7
127.0.0.1:6379> lset list 2 ...
OK
127.0.0.1:6379> lrange list 0 10
1) "seven"
2) "six"
3) "..."
4) "four"
5) "three"
6) "two"
7) "one"
- LTRIM key start stop (对列表进行切片)
127.0.0.1:6379> LTRIM list 2 5
OK
127.0.0.1:6379> lrange list 0 10
1) "..."
2) "four"
3) "three"
4) "two"