1 Redis 简介
Redis 是一个高性能的 key/value 数据库。它是完全开源免费的,并且遵守 BSD 协议。
- ./redis-conf/redis.conf:/etc/redis/redis.conf - ./redis-data/:/dataredis.conf 是一个默认的配置文件 我们可以根据需要使用自己的配置文件
redis.conf
# Redis configuration file example.
# Note that in order to read the configuration file, Redis must be
# started with the file path as first argument:
# 开始启动时必须如下指定配置文件
# ./redis-server /path/to/redis.conf
# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
# 存储单位如下所示
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
################################## INCLUDES ###################################
# 如果需要使用多配置文件配置redis,请用include
# include /path/to/local.conf
# include /path/to/other.conf
################################## MODULES ##################################### modules
# 手动设置加载模块(当服务无法自动加载时设置)
# loadmodule /path/to/my_module.so
# loadmodule /path/to/other_module.so
################################## NETWORK #####################################
# Examples:
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
# 设置绑定的ip
bind 127.0.0.1
# 保护模式:不允许外部网络连接redis服务
protected-mode yes
# 设置端口号
port 6379
# TCP listen() backlog.
# TCP 连接数,此参数确定了TCP连接中已完成队列(完成三次握手之后)的长度
tcp-backlog 511
# Unix socket.
# 通信协议设置,本机通信使用此协议不适用tcp协议可大大提升性能
# unixsocket /tmp/redis.sock
# unixsocketperm 700
# TCP keepalive.
# 定期检测cli连接是否存活
tcp-keepalive 300
################################# GENERAL #####################################
# 是否守护进程运行(后台运行)
daemonize yes
# 是否通过upstart和systemd管理Redis守护进程
supervised no
# 以后台进程方式运行redis,则需要指定pid 文件
pidfile /var/run/redis_6379.pid
# 日志级别
# 可选项有: # debug(记录大量日志信息,适用于开发、测试阶段); # verbose(较多日志信息); # notice(适量日志信息,使用于生产环境);
# warning(仅有部分重要、关键信息才会被记录)。
loglevel notice
# 日志文件的位置
logfile ""
# 数据库的个数
databases 16
# 是否显示logo
always-show-logo yes
################################ SNAPSHOTTING ################################
# Save the DB on disk:
# 持久化操作设置 900秒内触发一次请求进行持久化,300秒内触发10次请求进行持久化操作,60s内触发10000次请求进行持久化操作
save 900 1
save 300 10
save 60 10000
# 持久化出现错误后,是否依然进行继续进行工作
stop-writes-on-bgsave-error yes
# 使用压缩rdb文件 yes:压缩,但是需要一些cpu的消耗。no:不压缩,需要更多的磁盘空间
rdbcompression yes
# 是否校验rdb文件,更有利于文件的容错性,但是在保存rdb文件的时候,会有大概10%的性能损耗
rdbchecksum yes
# dbfilename的文件名
dbfilename dump.rdb
# dbfilename文件的存放位置
dir ./
################################# REPLICATION #################################
# replicaof 即slaveof 设置主结点的ip和端口
# replicaof <masterip> <masterport>
# 集群节点访问密码
# masterauth <master-password>
# 从结点断开后是否仍然提供数据
replica-serve-stale-data yes
# 设置从节点是否只读
replica-read-only yes
# 是或否创建新进程进行磁盘同步设置
repl-diskless-sync no
# master节点创建子进程前等待的时间
repl-diskless-sync-delay 5
# Replicas发送PING到master的间隔,默认值为10秒。
# repl-ping-replica-period 10
# repl-timeout 60
repl-disable-tcp-nodelay no
# repl-backlog-size 1mb
# repl-backlog-ttl 3600
replica-priority 100
# min-replicas-to-write 3
# min-replicas-max-lag 10
# replica-announce-ip 5.5.5.5
# replica-announce-port 1234
################################## SECURITY ###################################
# 设置连接时密码
# requirepass 123456
################################### CLIENTS ####################################
# 最大连接数
# maxclients 10000
############################## MEMORY MANAGEMENT ################################
# redis配置的最大内存容量
# maxmemory <bytes>
# 内存达到上限的处理策略
# maxmemory-policy noeviction
# 处理策略设置的采样值
# maxmemory-samples 5
# 是否开启 replica 最大内存限制
# replica-ignore-maxmemory yes
############################# LAZY FREEING ####################################
# 惰性删除或延迟释放
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
############################## APPEND ONLY MODE ###############################
# 是否使用AOF持久化方式
appendonly no
# appendfilename的文件名
appendfilename "appendonly.aof"
# 持久化策略
# appendfsync always
appendfsync everysec
# appendfsync no
# 持久化时(RDB的save | aof重写)是否可以运用Appendfsync,用默认no即可,保证数据安全性
no-appendfsync-on-rewrite no
# 设置重写的基准值
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
# 指定当发生AOF文件末尾截断时,加载文件还是报错退出
aof-load-truncated yes
# 开启混合持久化,更快的AOF重写和启动时数据恢复
aof-use-rdb-preamble yes
################################ REDIS CLUSTER ###############################
# 是否开启集群
# cluster-enabled yes
# 集群结点信息文件
# cluster-config-file nodes-6379.conf
# 等待节点回复的时限
# cluster-node-timeout 15000
# 结点重连规则参数
# cluster-replica-validity-factor 10
# cluster-migration-barrier 1
# cluster-require-full-coverage yes
# cluster-replica-no-failover no
查看 redis 启动情况:docker ps
127.0.0.1:6379> hmset account:1 name huanxi password 123456 fav travel
127.0.0.1:6379> hgetall account:1
1) "name"
2) "huanxi"
3) "password"
4) "123456"
5) "fav"
6) "travel"
使用 Redis HMSET, HGETALL 命令, account:1 为键。
2.4 List(列表)
List 是简单的字符串列表,按照插入顺序排序。
3 添加了两次,但最后只存储了一份。
2.6 zset ( sorted set:有序集合 )
zset 和 set 一样也是 string 类型元素的集合。不同的是 zset 中的每个元素都有自己的分数(double 类型),通过分数来对集合中的元素进行排序。这个分数是不重复的。
2.6.1 Redis zadd 命令
zset 添加元素到集合中,如果元素在集合中存在则更新对应分数(score)。
2.6.2 Redis zadd 命令语法格式
zadd key score member
127.0.0.1:6379> zadd zsetTemp 1 1
(integer) 1
127.0.0.1:6379> zadd zsetTemp 2 2
(integer) 1
127.0.0.1:6379> zadd zsetTemp 3 3
(integer) 1
127.0.0.1:6379> zadd zsetTemp 4 1
(integer) 0
zrangebyscore zsetTemp 0 10
1) "2"
2) "3"
3) "1"
1 被添加 2 次,但是最后只存储了一份。
2.7 Redis Bitmap ( 位图 )
Bitmap 通过类似 map 结构存放 0 或 1 ( bit 位 ) 作为值。可以用来统计状态,如 日活,打卡,浏览量等。
2.7.1 setbit 命令
setbit 命令用于设置或者清除一个 bit 位
2.7.2 setbit 命令语法格式
SETBIT key offset value
127.0.0.1:6379> setbit aa 10001 1 # 返回操作之前的数值
(integer) 0
127.0.0.1:6379> setbit aa 10001 2 # 如果值不是0或1就报错
(error) ERR bit is not an integer or out of range
127.0.0.1:6379> setbit aa 10001 0
(integer) 1
127.0.0.1:6379> setbit aa 10001 1
(integer) 0
127.0.0.1:6379> getbit aa 10001
(integer) 1
3 Redis 命令
更多命令请参考: https://redis.io/commands
下表列出了 Redis 键相关的命令