参考资料

  1. 官方网站:https://www.runoob.com/redis/redis-install.html
  2. 参考手册http://redisdoc.com/index.html
  3. 2020 年最新版 68 道 Redis 面试题
  4. https://www.javajike.com/article/1836.html

注意

  1. 如果用 DEL, SET, GETSET 会将 key 对应存储的值替换成新的,命令也会清除掉超时时间

    如果 list 结构中添加一个数据或者改变 hset 数据的一个字段是不会清除超时时间的

    如果想要通过 set 去覆盖值那就必须重新设置 expire

  2. org.springframework.data.redis.serializer.SerializationException: Cannot deserialize;「反序列化失败

    应该找到反序列化的类,实现

删除 key

1
2
3
4
5
6
7
//删除所有Key,可以使用Redis的flushdb和flushall命令

//删除当前数据库中的所有Key
flushdb

//删除所有数据库中的key
flushall

监测命令

查看连接数

1
info clients

查看当前连接的客户端

1
client list

配置

常用配置

1
2
3
4
5
6
7
1. 端口 port 默认 6379
2. 端口加入 防火墙高级设置-入站规则中
3. 绑定访问者 ip bind 默认 127.0.0.1
4. 密码 requirepass 默认无密码
5. 最大内存 maxmemory 不限制
6. 最大连接数 maxclients 默认 10000
7. 超时时间 timeout 18000:Close the connection after a client is idle for N seconds (0 to disable)

密码

查看密码

1
config get requirepass

设置密码

1
config set requirepass 123456

授权密码

1
auth 123456

超时时间「timeout」

客户端超时时间

空闲连接超时断开时间: 单位为秒(s),取值范围为 0-100000。默认值为 0,表示无限制

在业务场景中,一般会由 Redis 客户端进行连接资源管理,例如分配连接、监控连接状态、回收连接池资源等。默认设置下,Redis 不会主动断开连接,即使这个客户端已经空闲了很长时间。但在业务核心应用中,建议配置 timeout 参数以使 Redis 具有主动回收资源的能力。否则,如果客户端出现异常,连接池资源得不到及时回收,可能因空闲连接占满连接池导致服务崩溃。核心应用出现这样的问题可能引发整个业务的混乱,后果严重。

timeout 参数值的单位为秒(s),取值范围为 0-100000。默认值为 0,表示无限制。在实际运行中,为了提高性能,Redis 不一定会精确地按照 timeout 的值规定的时间来断开符合条件的空闲连接,例如设置 timeout 为 10s,但空闲连接可能在 12s 后,服务器中新增很多连接时才会被断开。如需降低这个延迟,可适当增大 hz 参数的值,提高负责断开超时连接的 Redis 定时任务的运行频率。

服务器端超时时间

查看超时时间

1
config get timeout

设置超时时间

1
config set timeout 1800  //1800s 即 30 分钟

Linux 下搭建 Redis 集群

https://mrbird.cc/linux-redis-cluster.html

Docker

参考《Docker》实例-Redis

Windows

安装版

下载

https://github.com/microsoftarchive/redis/releases

安装

  1. 添加到 Path 路径
    UTOOLS1576647853825.png

  2. 配置端口,添加到防火墙白名单
    UTOOLS1576647878429.png

  3. 设置最大内存大小
    UTOOLS1576647895201.png

  4. 运行

    安装完成后,默认以服务形式运行

配置

修改 redis.windows-service.conf 文件

  1. 配置访问密码

    1
    2
    3
    # requirepass foobared
    修改为:
    requirepass 123456
  2. 不限制客户端 ip

    1
    2
    bind 127.0.0.1 修改为:
    bind 0.0.0.0
  3. 重启服务生效

使用

  1. 客户端命令行连接

    1
    redis-cli.exe -h 127.0.0.1 -p 6379   //或者双击 redis-cli.exe
  2. 密码认证

    1
    auth password
  3. 使用

    1
    2
    set myKey abc
    get myKey
  4. 使用 redisclient 可视化工具连接

免安装版

Redis-x64-3.2.100

  • 配置文件redis.windows-service.conf默认配置项

  • 端口 6379port 6379

  • 访问密码 123456requirepass 123456

  • 不限制访问者 ip:bind 0.0.0.0

  • 手动配置

    • 将端口 6379 加入防火墙-高级设置-入站规则中
  • 使用

    1. service_install.bat添加到系统服务
    2. service_start.bat 启动服务
    3. service_stop.bat停止服务
    4. service_uninstall.bat 卸载服务
  1. 安装 redis 服务

    1
    redis-server --service-install redis.windows.conf --service-name redis6379 --loglevel verbose
  2. 通过 service-name 启动 redis 服务

    1
    redis-server --service-start --service-name redis6379
  3. 通过 service-name 停止 redis 服务

    1
    redis-server --service-stop --service-name redis6379

Linux

  1. 下载地址

    https://redis.io/download

  2. 安装

    1
    2
    3
    4
    $ wget http://download.redis.io/releases/redis-5.0.4.tar.gz
    $ tar xzf redis-5.0.4.tar.gz
    $ cd redis-5.0.4
    $ make
  3. 启动

    1
    2
    $ cd src
    $ ./redis-server ../redis.conf
  4. 使用

    1
    2
    3
    4
    5
    6
    $ cd src
    $ ./redis-cli
    redis> set foo bar
    OK
    redis> get foo
    "bar"

Mac

安装使用

Mac通过brew安装Redis和启动Redis服务,超简单安装一目了然_brew 启动redis_spoliedchild的博客-CSDN博客

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
$ brew install redis

# 添加环境变量,执行如下:
echo 'export PATH="/usr/local/opt/redis/bin:$PATH"' >> ~/.zshrc

# 安装位置
/usr/local/opt/redis

# redis 配置文件的位置
/usr/local/etc/redis.conf

# 后台启动
brew services start redis
# 重新启动
brew services restart redis

# 启动 redis
$ redis-server

# 使用配置文件启动 redis server
$ redis-server /usr/local/etc/redis.conf

本地测试 redis server 是否启动
$ redis-cli ping

本地连接
$ redis-cli

关闭 redis 服务
redis 127.0.0.1:6379> shutdown

远程连接 redis 服务
$ redis-cli -h ip -p 端口
示例:$ redis-cli -h 127.0.0.1 -p 6379

键入授权登录密码
127.0.0.1:6379> auth 123456(密码)

退出本次会话
127.0.0.1:6379> quit

命令总结

home-brew安装

1
brew install redis

开机启动 redis 命令

1
ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents

停止 redis server 的自启动

1
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

启动

  • brew services 启动

    1
    brew services start redis
  • 使用配置文件启动 redis server

    1
    redis-server /usr/local/etc/redis.conf

停止

  • brew

    1
    brew services stop redis
  • 无密码

    1
    redis-cli shutdown
  • 有密码

    1
    2
    3
    redis-cli
    auth password
    shutdown

重启

1
brew services restart redis

redis 配置文件的位置

1
/usr/local/etc/redis.conf

卸载 redis 和它的文件

1
brewuninstallredis rm ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

测试 redis server 是否启动

1
redis-cli ping

配置认证密码

  1. 通过文件配置——重启生效

    /usr/local/etc/redis.conf

    1
    2
    3
    4
    5
    默认密码配置
    #requirepass foobared

    //修改配置,去掉前面#,并把 foobared 修改想要设置的密码,比如:123456
    requirepass 123456
  2. 通过命令配置——即时生效,但重启服务后以文件中配置为准

    1
    2
    3
    4
    5
    redis 127.0.0.1:6379[1]> config set requirepass 123456
    OK
    redis 127.0.0.1:6379[1]> config get requirepass
    1) "requirepass"
    2) "123456"

可视化客户端工具

  • Medis
  • RedisPlus

Jedis、Lettuce

  • Jedis 在实现上是直连 Redis 服务,多线程环境下非线程安全,除非使用连接池,为每个 RedisConnection 实例增加物理连接。

  • Lettuce 是一种可伸缩,线程安全,完全非阻塞的 Redis 客户端,多个线程可以共享一个 RedisConnection,它利用 Netty NIO 框架来高效地管理多个连接,从而提供了异步和同步数据访问方式,用于构建非阻塞的反应性应用程序。