1.1 Redis简介

1.1.1 介绍

1.1.2 软件获取和帮助

1.1.3 Redis特性

 高速读写,数据类型丰富

1.1.4 企业缓存数据库解决方案对比

Memcached:

Redis:

Tair****<span style=“font-family: ‘微软雅黑’,sans-serif; courier new"4courier new”; background: yellow;">: 官方网站:http://tair.taobao.org

1.1.5 Redis应用场景

数据高速缓存,web会话缓存(Session Cache

排行榜应用

消息队列,发布订阅

   附录 - Redis的企业应用

1.2 Redis简单部署

1.2.1 典型安装-单实例

系统环境说明

安装redis

   <span style=“font-family: ‘微软雅黑’,sans-serif; courier new"4courier new”;color: red; background: yellow;">至此redis<span style=“font-family: ‘微软雅黑’,sans-serif; courier new"4courier new”; color: red; background: yellow;">就安装完成

1.2.2 启动第一个redis实例

创建客户端软连接

   配置文件说明:https://www.cnblogs.com/zhang-ke/p/5981108.html

   3、编写启动脚本(适用于CentOS 6.X

<p>
  &nbsp;&nbsp; <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; color: red;">注意:自编写脚本注意执行权限。</span></strong>
</p>

<h3>
  <span id="123_Redis">1.2.3 Redis<span style="font-family: '微软雅黑',sans-serif;">多实例配置</span></span>
</h3>

<p style="margin-left: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; background: yellow;">注意:本次多实例配置基于单实例配置完成后</span>
</p>

<p style="margin-left: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif;">创建并进入程序目录</span>
</p>

<div class="cnblogs_code">
  <pre>[root@Redis redis]<span style="color: #008000;">#</span><span style="color: #008000;"> mkdir /application/redis  -p</span>

[root@Redis redis]# cd /application/redis/

<p>
  &nbsp;&nbsp; <span style="font-family: '微软雅黑',sans-serif;">修改配置文件</span>
</p>

<div class="cnblogs_code">
  <pre><span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> 0 1 2<span style="color: #000000;">

do
# 创建多实例(端口命名)目录 mkdir -p 638$i # 复制启动程序到各实例 \cp /usr/local/redis/src/redis-server /application/redis/638$i/ # 复制配置文件。注意:此处基于单实例配置完成 \cp /etc/redis.conf /application/redis/638$i/ # 修改程序存储目录 sed -i "/dir/s#.*#dir /application/redis/638$i/#g" /application/redis/638$i/redis.conf # 修改其他端口信息 sed -i "s#6379#638$i#g" /application/redis/638$i/redis.conf # 允许远程连接redis sed -i /protected-mode/s#yes#no#g /application/redis/638$i/redis.conf done

<p>
  # <span style="font-family: '微软雅黑',sans-serif;">启动实例</span>
</p>

<div class="cnblogs_code">
  <pre><span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> 0 1 2<span style="color: #000000;"> 

do /application/redis/638$i/redis-server /application/redis/638$i/redis.conf done

<p>
  &nbsp;&nbsp; <span style="font-family: '微软雅黑',sans-serif;">连接</span>redis
</p>

<div class="cnblogs_code">
  <pre>[root@Redis redis]<span style="color: #008000;">#</span><span style="color: #008000;"> redis-cli -h 10.0.0.186 -p 6379</span>

10.0.0.186:6379>

<h3>
  <span id="124_redisconf">1.2.4 redis.conf<span style="font-family: '微软雅黑',sans-serif;">配置说明</span></span>
</h3>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">是否后台运行:</span>
</p>

<div class="cnblogs_code">
  <pre>daemonize no/yes</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">默认端口:</span>
</p>

<div class="cnblogs_code">
  <pre>port 6379</pre>
</div>

<p>
  AOF<span style="font-family: '微软雅黑',sans-serif;">日志开关是否打开:</span>
</p>

<div class="cnblogs_code">
  <pre>appendonly no/yes</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">日志文件位置</span>
</p>

<div class="cnblogs_code">
  <pre>logfile /var/log/redis.log</pre>
</div>

<p>
  RDB<span style="font-family: '微软雅黑',sans-serif;">持久化数据文件</span>:
</p>

<div class="cnblogs_code">
  <pre>dbfilename dump.rdb</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">指定</span>IP<span style="font-family: '微软雅黑',sans-serif;">进行监听</span>
</p>

<div class="cnblogs_code">
  <pre>bind 10.0.0.51 ip2 ip3 ip4</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">禁止</span>protected-mode
</p>

<div class="cnblogs_code">
  <pre>protected-mode yes/no (保护模式,是否只允许本地访问)</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">增加</span>requirepass {password}
</p>

<div class="cnblogs_code">
  <pre>requirepass root</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">在</span>redis-cli<span style="font-family: '微软雅黑',sans-serif;">中使用</span>
</p>

<div class="cnblogs_code">
  <pre>auth {password} 进行认证</pre>
</div>

<h3>
  <span id="125">1.2.5 <span style="font-family: '微软雅黑',sans-serif;">在线变更配置</span></span>
</h3>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">获取当前配置</span>
</p>

<div class="cnblogs_code">
  <pre>CONFIG GET *</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">变更运行配置</span>
</p>

<div class="cnblogs_code">
  <pre>CONFIG SET loglevel <span style="color: #800000;">"</span><span style="color: #800000;">notice</span><span style="color: #800000;">"</span></pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">修改密码为空</span>
</p>

<div class="cnblogs_code">
  <pre>10.0.0.186:6379> config set requirepass <span style="color: #800000;">""</span>

10.0.0.186:6379> exit 10.0.0.186:6379> config get dir 1) "dir" 2) "/usr/local/redis/data"

<h2>
  <span id="13_Redis">1.3 Redis<span style="font-family: '微软雅黑',sans-serif;">数据持久化</span></span>
</h2>

<h3>
  <span id="131">1.3.1 <span style="font-family: '微软雅黑',sans-serif;">持久化策略</span></span>
</h3>

<p>
  redis <span style="font-family: '微软雅黑',sans-serif;">提供了多种不同级别的持久化方式</span>:<span style="font-family: '微软雅黑',sans-serif;">一种是</span>RDB,<span style="font-family: '微软雅黑',sans-serif;">另一种是</span>AOF.
</p>

<p style="text-indent: 21.0pt;">
  <strong>RDB </strong><strong><span style="font-family: '微软雅黑',sans-serif;">持久化</span></strong>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">可以在指定的时间间隔内生成数据集的时间点快照(</span>point-in-time snapshot<span style="font-family: '微软雅黑',sans-serif;">)。</span>
</p>

<p style="text-indent: 21.0pt;">
  <strong>AOF </strong><strong><span style="font-family: '微软雅黑',sans-serif;">持久化</span></strong>
</p>

<p style="margin-left: 42.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">记录服务器执行的所有写操作命令,并在服务器启动时,通过重新执行这些命令来还原数据集。</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件中的命令全部以</span> Redis <span style="font-family: '微软雅黑',sans-serif;">协议的格式来保存,新命令会被追加到文件的末尾。</span>
</p>

<p style="text-indent: 21.0pt;">
  Redis <span style="font-family: '微软雅黑',sans-serif;">还可以在后台对</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件进行重写(</span>rewrite<span style="font-family: '微软雅黑',sans-serif;">),使得</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件的体积不会超出保存数据集状态所需的实际大小。</span>Redis <span style="font-family: '微软雅黑',sans-serif;">还可以同时使用</span> AOF <span style="font-family: '微软雅黑',sans-serif;">持久化和</span> RDB <span style="font-family: '微软雅黑',sans-serif;">持久化。</span> <span style="font-family: '微软雅黑',sans-serif;">在这种情况下,</span> <span style="font-family: '微软雅黑',sans-serif;">当</span> Redis <span style="font-family: '微软雅黑',sans-serif;">重启时,</span> <span style="font-family: '微软雅黑',sans-serif;">它会优先使用</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件来还原数据集,</span> <span style="font-family: '微软雅黑',sans-serif;">因为</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件保存的数据集通常比</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件所保存的数据集更完整。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">你甚至可以关闭持久化功能,让数据只在服务器运行时存在。</span>
</p>

<h3>
  <span id="132_RDB">1.3.2 RDB <span style="font-family: '微软雅黑',sans-serif;">持久化</span></span>
</h3>

<p>
  <strong><span style="background: lime;">RDB</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">的优点</span></strong>
</p>

<p style="margin-left: 24.0pt; text-indent: -24.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">⚔</span> RDB <span style="font-family: '微软雅黑',sans-serif;">是一个非常紧凑(</span>compact<span style="font-family: '微软雅黑',sans-serif;">)的文件,它保存了</span> Redis <span style="font-family: '微软雅黑',sans-serif;">在某个时间点上的数据集。</span> <span style="font-family: '微软雅黑',sans-serif;">这种文件非常适合用于进行备份:</span> <span style="font-family: '微软雅黑',sans-serif;">比如说,你可以在最近的</span> 24 <span style="font-family: '微软雅黑',sans-serif;">小时内,每小时备份一次</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件,并且在每个月的每一天,也备份一个</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件。</span> <span style="font-family: '微软雅黑',sans-serif;">这样的话,即使遇上问题,也可以随时将数据集还原到不同的版本。</span>
</p>

<p style="margin-left: 24.0pt; text-indent: -24.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">⚔</span> RDB <span style="font-family: '微软雅黑',sans-serif;">非常适用于灾难恢复(</span>disaster recovery<span style="font-family: '微软雅黑',sans-serif;">):它只有一个文件,并且内容都非常紧凑,可以(在加密后)将它传送到别的数据中心,或者亚马逊</span> S3 <span style="font-family: '微软雅黑',sans-serif;">中。</span>
</p>

<p style="margin-left: 24.0pt; text-indent: -24.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">⚔</span> RDB <span style="font-family: '微软雅黑',sans-serif;">可以最大化</span> Redis <span style="font-family: '微软雅黑',sans-serif;">的性能:父进程在保存</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件时唯一要做的就是</span> fork <span style="font-family: '微软雅黑',sans-serif;">出一个子进程,然后这个子进程就会处理接下来的所有保存工作,父进程无须执行任何磁盘</span> I/O <span style="font-family: '微软雅黑',sans-serif;">操作。</span>
</p>

<p style="margin-left: 24.0pt; text-indent: -24.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">⚔</span> RDB <span style="font-family: '微软雅黑',sans-serif;">在恢复大数据集时的速度比</span> AOF <span style="font-family: '微软雅黑',sans-serif;">的恢复速度要快。</span>
</p>

<p>
  <strong><span style="background: lime;">RDB</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">的缺点</span></strong>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">如果你需要尽量避免在服务器故障时丢失数据,那么</span> RDB <span style="font-family: '微软雅黑',sans-serif;">不适合你。</span>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">虽然</span> Redis <span style="font-family: '微软雅黑',sans-serif;">允许你设置不同的保存点(</span><em><span style="text-decoration: underline;">save point</span></em><span style="font-family: '微软雅黑',sans-serif;">)来控制保存</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件的频率,</span> <span style="font-family: '微软雅黑',sans-serif;">但是,</span> <span style="font-family: '微软雅黑',sans-serif;">因为</span>RDB <span style="font-family: '微软雅黑',sans-serif;">文件需要保存整个数据集的状态,</span> <span style="font-family: '微软雅黑',sans-serif;">所以它并不是一个轻松的操作。</span> <span style="font-family: '微软雅黑',sans-serif;">因此你可能会至少</span> 5 <span style="font-family: '微软雅黑',sans-serif;">分钟才保存一次</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件。</span> <span style="font-family: '微软雅黑',sans-serif;">在这种情况下,</span> <span style="font-family: '微软雅黑',sans-serif;">一旦发生故障停机,</span> <span style="font-family: '微软雅黑',sans-serif;">你就<em><span style="background: yellow;">可能会丢失好几分钟的数据</span></em>。</span>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">每次保存</span> RDB <span style="font-family: '微软雅黑',sans-serif;">的时候,</span>Redis <span style="font-family: '微软雅黑',sans-serif;">都要</span> fork() <span style="font-family: '微软雅黑',sans-serif;">出一个子进程,并由子进程来进行实际的持久化工作。</span> <span style="font-family: '微软雅黑',sans-serif;">在数据集比较庞大时,</span> fork() <span style="font-family: '微软雅黑',sans-serif;">可能会非常耗时,造成服务器在某某毫秒内停止处理客户端;</span><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">如果数据集非常巨大,并且</span> CPU </span><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">时间非常紧张的话,那么这种停止时间甚至可能会长达整整一秒。</span></span> <span style="font-family: '微软雅黑',sans-serif;">虽然</span> AOF <span style="font-family: '微软雅黑',sans-serif;">重写也需要进行</span> fork() <span style="font-family: '微软雅黑',sans-serif;">,但无论</span> AOF <span style="font-family: '微软雅黑',sans-serif;">重写的执行间隔有多长,数据的耐久性都不会有任何损失。</span>
</p>

<h3>
  <span id="133_AOF">1.3.3 AOF <span style="font-family: '微软雅黑',sans-serif;">持久化</span></span>
</h3>

<p>
  <strong><span style="background: lime;">AOF </span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">优点</span></strong>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">使用</span><em><span style="text-decoration: underline;">AOF </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">会让你的</span>Redis</span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">更加耐久</span></span></em>: <span style="font-family: '微软雅黑',sans-serif;">你可以使用不同的</span>fsync<span style="font-family: '微软雅黑',sans-serif;">策略:无</span>fsync,<span style="font-family: '微软雅黑',sans-serif;">每秒</span>fsync,<span style="font-family: '微软雅黑',sans-serif;">每次写的时候</span>fsync.<span style="font-family: '微软雅黑',sans-serif;">使用默认的每秒</span>fsync<span style="font-family: '微软雅黑',sans-serif;">策略</span>,Redis<span style="font-family: '微软雅黑',sans-serif;">的性能依然很好</span>(fsync<span style="font-family: '微软雅黑',sans-serif;">是由后台线程进行处理的</span>,<span style="font-family: '微软雅黑',sans-serif;">主线程会尽力处理客户端请求</span>),<span style="font-family: '微软雅黑',sans-serif;">一旦出现故障,你最多丢失</span>1<span style="font-family: '微软雅黑',sans-serif;">秒的数据</span>.
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  Redis <span style="font-family: '微软雅黑',sans-serif;">可以在</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件体积变得过大时,<em><span style="text-decoration: underline;">自动地在后台对</span></em></span><em><span style="text-decoration: underline;"> AOF </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">进行重写</span></span></em><span style="font-family: '微软雅黑',sans-serif;">:</span> <span style="font-family: '微软雅黑',sans-serif;">重写后的新</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件包含了恢复当前数据集所需的最小命令集合。</span> <span style="font-family: '微软雅黑',sans-serif;">整个重写操作是绝对安全的,因为</span> Redis <span style="font-family: '微软雅黑',sans-serif;">在创建新</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件的过程中,会继续将命令追加到现有的</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件里面,即使重写过程中发生停机,现有的</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件也不会丢失。</span>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">一旦新</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件创建完毕,</span>Redis <span style="font-family: '微软雅黑',sans-serif;">就会从旧</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件切换到新</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件,并开始对新</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件进行追加操作。</span>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  AOF <span style="font-family: '微软雅黑',sans-serif;">文件有序地保存了对数据库执行的所有写入操作,</span> <span style="font-family: '微软雅黑',sans-serif;">这些写入操作以</span> Redis <span style="font-family: '微软雅黑',sans-serif;">协议的格式保存,</span> <span style="font-family: '微软雅黑',sans-serif;">因此</span> <em><span style="text-decoration: underline;">AOF </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">文件的内容非常容易被人读懂</span></span></em><span style="font-family: '微软雅黑',sans-serif;">,</span> <span style="font-family: '微软雅黑',sans-serif;">对文件进行分析(</span>parse<span style="font-family: '微软雅黑',sans-serif;">)也很轻松。</span> <span style="font-family: '微软雅黑',sans-serif;">导出(</span>export<span style="font-family: '微软雅黑',sans-serif;">)</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件也非常简单:</span> <span style="font-family: '微软雅黑',sans-serif;">举个例子,</span> <span style="font-family: '微软雅黑',sans-serif;">如果你不小心执行了</span> FLUSHALL <span style="font-family: '微软雅黑',sans-serif;">命令,</span> <span style="font-family: '微软雅黑',sans-serif;">但只要</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件未被重写,</span> <span style="font-family: '微软雅黑',sans-serif;">那么只要停止服务器,</span> <span style="font-family: '微软雅黑',sans-serif;">移除</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件末尾的</span> FLUSHALL <span style="font-family: '微软雅黑',sans-serif;">命令,</span> <span style="font-family: '微软雅黑',sans-serif;">并重启</span> Redis <span style="font-family: '微软雅黑',sans-serif;">,</span> <span style="font-family: '微软雅黑',sans-serif;">就可以将数据集恢复到</span> FLUSHALL <span style="font-family: '微软雅黑',sans-serif;">执行之前的状态。</span>
</p>

<p>
  <strong><span style="background: lime;">AOF </span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">缺点</span></strong>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">对于相同的数据集来说,</span><em><span style="text-decoration: underline;">AOF </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">文件的体积通常要大于</span> RDB </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">文件的体积</span></span></em><span style="font-family: '微软雅黑',sans-serif;">。根据所使用的</span> fsync <span style="font-family: '微软雅黑',sans-serif;">策略,</span>AOF <span style="font-family: '微软雅黑',sans-serif;">的速度可能会慢于</span> RDB <span style="font-family: '微软雅黑',sans-serif;">。</span>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">在一般情况下,</span> <span style="font-family: '微软雅黑',sans-serif;">每秒</span> fsync <span style="font-family: '微软雅黑',sans-serif;">的性能依然非常高,</span> <span style="font-family: '微软雅黑',sans-serif;">而<em><span style="text-decoration: underline;">关闭</span></em></span><em><span style="text-decoration: underline;"> fsync </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">可以让</span> AOF </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">的速度和</span> RDB </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">一样快</span></span></em><span style="font-family: '微软雅黑',sans-serif;">,</span> <span style="font-family: '微软雅黑',sans-serif;">即使在高负荷之下也是如此。</span> <span style="font-family: '微软雅黑',sans-serif;">不过在处理巨大的写入载入时,</span>RDB <span style="font-family: '微软雅黑',sans-serif;">可以提供更有保证的最大延迟时间(</span>latency<span style="font-family: '微软雅黑',sans-serif;">)。</span>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  AOF <span style="font-family: '微软雅黑',sans-serif;">在过去曾经发生过这样的</span> bug <span style="font-family: '微软雅黑',sans-serif;">:因为个别命令的原因,导致</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件在重新载入时,无法将数据集恢复成保存时的原样。</span> <span style="font-family: '微软雅黑',sans-serif;">(举个例子,阻塞命令</span> BRPOPLPUSH <span style="font-family: '微软雅黑',sans-serif;">就曾经引起过这样的</span> bug <span style="font-family: '微软雅黑',sans-serif;">。)</span> <span style="font-family: '微软雅黑',sans-serif;">测试套件里为这种情况添加了测试:</span> <span style="font-family: '微软雅黑',sans-serif;">它们会自动生成随机的、复杂的数据集,</span> <span style="font-family: '微软雅黑',sans-serif;">并通过重新载入这些数据来确保一切正常。</span>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">虽然这种</span> bug <span style="font-family: '微软雅黑',sans-serif;">在</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件中并不常见,</span> <span style="font-family: '微软雅黑',sans-serif;">但是对比来说,</span> RDB <span style="font-family: '微软雅黑',sans-serif;">几乎是不可能出现这种</span> bug <span style="font-family: '微软雅黑',sans-serif;">的。</span>
</p>

<h3>
  <span id="134">1.3.4 <span style="font-family: '微软雅黑',sans-serif;">如何选择使用哪种持久化方式</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">一般来说,</span> <span style="font-family: '微软雅黑',sans-serif;">如果想达到足以媲美</span> PostgreSQL <span style="font-family: '微软雅黑',sans-serif;">的数据安全性,</span> <span style="font-family: '微软雅黑',sans-serif;">你应该同时使用两种持久化功能。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">如果你非常关心你的数据,</span> <span style="font-family: '微软雅黑',sans-serif;">但仍然可以承受数分钟以内的数据丢失,</span> <span style="font-family: '微软雅黑',sans-serif;">那么你可以只使用</span> RDB <span style="font-family: '微软雅黑',sans-serif;">持久化。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">有很多用户都只使用</span> AOF <span style="font-family: '微软雅黑',sans-serif;">持久化,</span> <span style="font-family: '微软雅黑',sans-serif;">但我们并不推荐这种方式:</span> <span style="font-family: '微软雅黑',sans-serif;">因为定时生成</span> RDB <span style="font-family: '微软雅黑',sans-serif;">快照(</span>snapshot<span style="font-family: '微软雅黑',sans-serif;">)非常便于进行数据库备份,</span> <span style="font-family: '微软雅黑',sans-serif;">并且</span> RDB <span style="font-family: '微软雅黑',sans-serif;">恢复数据集的速度也要比</span> AOF <span style="font-family: '微软雅黑',sans-serif;">恢复的速度要快,</span> <span style="font-family: '微软雅黑',sans-serif;">除此之外,</span> <span style="font-family: '微软雅黑',sans-serif;">使用</span> RDB <span style="font-family: '微软雅黑',sans-serif;">还可以避免之前提到的</span> AOF <span style="font-family: '微软雅黑',sans-serif;">程序的</span> bug <span style="font-family: '微软雅黑',sans-serif;">。</span>
</p>

<div style="border: solid windowtext 2.0pt; padding: 1.0pt 4.0pt 1.0pt 4.0pt; background: #F2F2F2;">
  <p>
    Note: <span style="font-family: '微软雅黑',sans-serif;">因为以上提到的种种原因,</span> <span style="font-family: '微软雅黑',sans-serif;">未来</span>redis<span style="font-family: '微软雅黑',sans-serif;">可能会将</span> AOF <span style="font-family: '微软雅黑',sans-serif;">和</span> RDB <span style="font-family: '微软雅黑',sans-serif;">整合成单个持久化模型(这是一个长期计划)。</span>
  </p>
</div>

<h3>
  <span id="135">1.3.5 <span style="font-family: '微软雅黑',sans-serif;">快照实现持久化</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">在默认情况下,</span> Redis <span style="font-family: '微软雅黑',sans-serif;">将数据库快照保存在名字为</span> dump.rdb<span style="font-family: '微软雅黑',sans-serif;">的二进制文件中。你可以对</span> Redis <span style="font-family: '微软雅黑',sans-serif;">进行设置,</span> <span style="font-family: '微软雅黑',sans-serif;">让它在&ldquo;</span> N <span style="font-family: '微软雅黑',sans-serif;">秒内数据集至少有</span> M <span style="font-family: '微软雅黑',sans-serif;">个改动&rdquo;这一条件被满足时,</span> <span style="font-family: '微软雅黑',sans-serif;">自动保存一次数据集。</span>
</p>

<p style="margin-left: 7.1pt; text-indent: 13.9pt;">
  <span style="font-family: '微软雅黑',sans-serif;">你也可以通过调用</span> SAVE<span style="font-family: '微软雅黑',sans-serif;">或者</span> BGSAVE <span style="font-family: '微软雅黑',sans-serif;">,</span> <span style="font-family: '微软雅黑',sans-serif;">手动让</span> Redis <span style="font-family: '微软雅黑',sans-serif;">进行数据集保存操作。</span>
</p>

<p>
  &nbsp;&nbsp; <span style="font-family: '微软雅黑',sans-serif;">比如说,</span> <span style="font-family: '微软雅黑',sans-serif;">以下设置会让</span> Redis <span style="font-family: '微软雅黑',sans-serif;">在满足&ldquo;</span> 60 <span style="font-family: '微软雅黑',sans-serif;">秒内有至少有</span> 1000 <span style="font-family: '微软雅黑',sans-serif;">个键被改动&rdquo;这一条件时,</span> <span style="font-family: '微软雅黑',sans-serif;">自动保存一次数据集</span>:&nbsp;<span class="cnblogs_code">save 60 1000</span>&nbsp;
</p>

<p>
  &nbsp;&nbsp; <span style="font-family: '微软雅黑',sans-serif;">这种<span style="text-decoration: underline;"><span style="background: lime;">持久化方式被称为快照</span></span></span><span style="text-decoration: underline;"><span style="background: lime;"> snapshotting</span></span>.
</p>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">当</span> Redis <span style="font-family: '微软雅黑',sans-serif;">需要保存</span> dump.rdb <span style="font-family: '微软雅黑',sans-serif;">文件时,</span> <span style="font-family: '微软雅黑',sans-serif;">服务器执行以下操作</span>:
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> Redis <span style="font-family: '微软雅黑',sans-serif;">调用</span>forks. <span style="font-family: '微软雅黑',sans-serif;">同时拥有父进程和子进程。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">子进程将数据集写入到一个临时</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件中。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">当子进程完成对新</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件的写入时,</span>Redis <span style="font-family: '微软雅黑',sans-serif;">用新</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件替换原来的</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件,并删除旧的</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">这种工作方式使得</span> Redis <span style="font-family: '微软雅黑',sans-serif;">可以从写时复制(</span>copy-on-write<span style="font-family: '微软雅黑',sans-serif;">)机制中获益。</span>
</p>

<h3>
  <span id="136_AOF">1.3.6 AOF<span style="font-family: '微软雅黑',sans-serif;">持久化</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">只进行追加操作的文件(</span>append-only file<span style="font-family: '微软雅黑',sans-serif;">,</span>AOF<span style="font-family: '微软雅黑',sans-serif;">)</span>
</p>

<p style="text-indent: 21.0pt;">
  <em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">快照功能并不是非常耐久</span></span></em><span style="font-family: '微软雅黑',sans-serif;">:如果</span> Redis <span style="font-family: '微软雅黑',sans-serif;">因为某些原因而造成故障停机,那么服务器将丢失最近写入、且仍未保存到快照中的那些数据。尽管对于某些程序来说,数据的耐久性并不是最重要的考虑因素,但是对于那些追求完全耐久能力的程序员来说,快照功能就不太适用了。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">从</span> 1.1 <span style="font-family: '微软雅黑',sans-serif;">版本开始,</span> Redis <span style="font-family: '微软雅黑',sans-serif;">增加了一种完全耐久的持久化方式:</span> AOF <span style="font-family: '微软雅黑',sans-serif;">持久化。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">你可以通过修改配置文件来打开</span> AOF <span style="font-family: '微软雅黑',sans-serif;">功能:&nbsp;<span class="cnblogs_code">appendonly yes</span>&nbsp;</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">从现在开始,每当</span> Redis <span style="font-family: '微软雅黑',sans-serif;">执行一个改变数据集的命令式(比如</span> SET<span style="font-family: '微软雅黑',sans-serif;">),这个命令就会被追加到</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件的末尾。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">这样的话,当</span>redis<span style="font-family: '微软雅黑',sans-serif;">重新启动时,程序就可以通过重新执行</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件中的命令来达到重建数据集的目的</span>
</p>

<h3>
  <span id="137_AOF">1.3.7 AOF<span style="font-family: '微软雅黑',sans-serif;">日志重写</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">因为</span> AOF <span style="font-family: '微软雅黑',sans-serif;">的运作方式是不断地将命令追加到文件的末尾,</span> <span style="font-family: '微软雅黑',sans-serif;">所以随着写入命令的不断增加,</span> <span style="background: lime;">AOF </span><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">文件的体积也会变得越来越大</span><span style="font-family: '微软雅黑',sans-serif;">。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">举个例子,</span> <span style="font-family: '微软雅黑',sans-serif;">如果你对一个计数器调用了</span> 100 <span style="font-family: '微软雅黑',sans-serif;">次</span> INCR <span style="font-family: '微软雅黑',sans-serif;">,</span> <span style="font-family: '微软雅黑',sans-serif;">那么仅仅是为了保存这个计数器的当前值,</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件就需要使用</span> 100 <span style="font-family: '微软雅黑',sans-serif;">条记录(</span>entry<span style="font-family: '微软雅黑',sans-serif;">)。然而在实际上,</span> <span style="font-family: '微软雅黑',sans-serif;">只使用一条</span> SET <span style="font-family: '微软雅黑',sans-serif;">命令已经足以保存计数器的当前值了,</span> <span style="font-family: '微软雅黑',sans-serif;">其余</span> 99 <span style="font-family: '微软雅黑',sans-serif;">条记录实际上都是多余的。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">为了处理这种情况,</span> Redis <span style="font-family: '微软雅黑',sans-serif;">支持一种有趣的特性:</span> <span style="font-family: '微软雅黑',sans-serif;">可以在不打断服务客户端的情况下,</span> <span style="font-family: '微软雅黑',sans-serif;">对</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件进行重建(</span>rebuild<span style="font-family: '微软雅黑',sans-serif;">)。<em><span style="text-decoration: underline;">执行</span></em></span><em><span style="text-decoration: underline;"> BGREWRITEAOF </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">命令,</span> Redis </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">将生成一个新的</span> AOF </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">文件,</span> </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">这个文件包含重建当前数据集所需的最少命令</span></span></em><span style="font-family: '微软雅黑',sans-serif;">。</span>Redis 2.2 <span style="font-family: '微软雅黑',sans-serif;">需要自己手动执行</span> BGREWRITEAOF <span style="font-family: '微软雅黑',sans-serif;">命令</span>.
</p>

<h3>
  <span id="138_AOF">1.3.8 AOF<span style="font-family: '微软雅黑',sans-serif;">有多耐用</span>?</span>
</h3>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">你可以配置</span> Redis <span style="font-family: '微软雅黑',sans-serif;">多久才将数据</span> fsync <span style="font-family: '微软雅黑',sans-serif;">到磁盘一次。有三种方式:</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">每次有新命令追加到</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件时就执行一次</span> fsync <span style="font-family: '微软雅黑',sans-serif;">:非常慢,也非常安全</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">每秒</span> fsync <span style="font-family: '微软雅黑',sans-serif;">一次:足够快(和使用</span> RDB <span style="font-family: '微软雅黑',sans-serif;">持久化差不多),并且在故障时只会丢失</span> 1 <span style="font-family: '微软雅黑',sans-serif;">秒钟的数据。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">从不</span> fsync <span style="font-family: '微软雅黑',sans-serif;">:将数据交给操作系统来处理。更快,也更不安全的选择。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">推荐(并且也是默认)的措施为每秒</span> fsync <span style="font-family: '微软雅黑',sans-serif;">一次,</span> <span style="font-family: '微软雅黑',sans-serif;">这种</span> fsync <span style="font-family: '微软雅黑',sans-serif;">策略可以兼顾速度和安全性。</span>
</p>

<h3>
  <span id="139_AOF">1.3.9 <span style="font-family: '微软雅黑',sans-serif;">如果</span>AOF<span style="font-family: '微软雅黑',sans-serif;">文件损坏了怎么办?</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">服务器可能在程序正在对</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件进行写入时停机,</span> <span style="font-family: '微软雅黑',sans-serif;">如果停机造成了</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件出错(</span>corrupt<span style="font-family: '微软雅黑',sans-serif;">),</span> <span style="font-family: '微软雅黑',sans-serif;">那么</span> Redis <span style="font-family: '微软雅黑',sans-serif;">在重启时会拒绝载入这个</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件,</span> <span style="font-family: '微软雅黑',sans-serif;">从而确保数据的一致性不会被破坏。当发生这种情况时,</span> <span style="font-family: '微软雅黑',sans-serif;">可以用以下方法来修复出错的</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件:</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">为现有的</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件创建一个备份。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">使用</span> Redis <span style="font-family: '微软雅黑',sans-serif;">附带的</span> redis-check-aof <span style="font-family: '微软雅黑',sans-serif;">程序,对原来的</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件进行修复</span>:&nbsp;<span class="cnblogs_code">$ redis-check-aof &ndash;fix</span>&nbsp;
</p>

<p style="text-indent: 21.7pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">使用</span> diff -u <span style="font-family: '微软雅黑',sans-serif;">对比修复后的</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件和原始</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件的备份,查看两个文件之间的不同之处。(可选)</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">重启</span> Redis <span style="font-family: '微软雅黑',sans-serif;">服务器,等待服务器载入修复后的</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件,并进行数据恢复。</span>
</p>

<h3>
  <span id="1310_AOFRDB">1.3.10 AOF<span style="font-family: '微软雅黑',sans-serif;">和</span>RDB<span style="font-family: '微软雅黑',sans-serif;">之间的相互作用</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">在版本号大于等于</span> 2.4 <span style="font-family: '微软雅黑',sans-serif;">的</span> Redis <span style="font-family: '微软雅黑',sans-serif;">中,</span>BGSAVE <span style="font-family: '微软雅黑',sans-serif;">执行的过程中,</span> <span style="font-family: '微软雅黑',sans-serif;">不可以执行</span> BGREWRITEAOF <span style="font-family: '微软雅黑',sans-serif;">。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">反过来说,</span> <span style="font-family: '微软雅黑',sans-serif;">在</span> BGREWRITEAOF <span style="font-family: '微软雅黑',sans-serif;">执行的过程中,</span> <span style="font-family: '微软雅黑',sans-serif;">也不可以执行</span> BGSAVE<span style="font-family: '微软雅黑',sans-serif;">。这可以防止两个</span> Redis <span style="font-family: '微软雅黑',sans-serif;">后台进程同时对磁盘进行大量的</span> I/O <span style="font-family: '微软雅黑',sans-serif;">操作。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">如果</span> BGSAVE <span style="font-family: '微软雅黑',sans-serif;">正在执行,</span> <span style="font-family: '微软雅黑',sans-serif;">并且用户显示地调用</span> BGREWRITEAOF <span style="font-family: '微软雅黑',sans-serif;">命令,</span> <span style="font-family: '微软雅黑',sans-serif;">那么服务器将向用户回复一个</span> OK <span style="font-family: '微软雅黑',sans-serif;">状态,</span> <span style="font-family: '微软雅黑',sans-serif;">并告知用户,</span> BGREWRITEAOF <span style="font-family: '微软雅黑',sans-serif;">已经被预定执行:</span> <span style="font-family: '微软雅黑',sans-serif;">一旦</span> BGSAVE <span style="font-family: '微软雅黑',sans-serif;">执行完毕,</span> BGREWRITEAOF <span style="font-family: '微软雅黑',sans-serif;">就会正式开始。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">当</span> Redis <span style="font-family: '微软雅黑',sans-serif;">启动时,</span> <em><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; background: lime;">如果</span><span style="background: lime;"> RDB </span></em><em><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">持久化和</span><span style="background: lime;"> AOF </span></em><em><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; background: lime;">持久化都被打开了,</span> </em><em><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">那么程序会优先使用</span><span style="background: lime;"> AOF </span></em><em><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; background: lime;">文件来恢复数据集</span></em><span style="font-family: '微软雅黑',sans-serif;">,</span> <span style="font-family: '微软雅黑',sans-serif;">因为</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件所保存的数据通常是最完整的。</span>
</p>

<h3>
  <span id="1311_redis">1.3.11 <span style="font-family: '微软雅黑',sans-serif;">备份</span>redis<span style="font-family: '微软雅黑',sans-serif;">数据</span></span>
</h3>

<p style="margin-left: 21.0pt;">
  Redis <span style="font-family: '微软雅黑',sans-serif;">对于数据备份是非常友好的,</span> <span style="font-family: '微软雅黑',sans-serif;">因为你可以在服务器运行的时候对</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件进行复制:</span>
</p>

<p style="text-indent: 21.0pt;">
  RDB <span style="font-family: '微软雅黑',sans-serif;">文件一旦被创建,</span> <span style="font-family: '微软雅黑',sans-serif;">就不会进行任何修改。</span> <span style="font-family: '微软雅黑',sans-serif;">当服务器要创建一个新的</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件时,</span> <span style="font-family: '微软雅黑',sans-serif;">它先将文件的内容保存在一个临时文件里面,</span> <span style="font-family: '微软雅黑',sans-serif;">当临时文件写入完毕时,</span> <span style="font-family: '微软雅黑',sans-serif;">程序才使用</span> rename(2) <span style="font-family: '微软雅黑',sans-serif;">原子地用临时文件替换原来的</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件。</span>
</p>

<p>
  &nbsp;&nbsp; <span style="font-family: '微软雅黑',sans-serif;">这也就是说,</span> <span style="font-family: '微软雅黑',sans-serif;">无论何时,</span> <span style="font-family: '微软雅黑',sans-serif;">复制</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件都是绝对安全的。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">创建一个定期任务(</span>cron job<span style="font-family: '微软雅黑',sans-serif;">),</span> <span style="font-family: '微软雅黑',sans-serif;">每小时将一个</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件备份到一个文件夹,</span> <span style="font-family: '微软雅黑',sans-serif;">并且每天将一个</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件备份到另一个文件夹。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">确保快照的备份都带有相应的日期和时间信息,</span> <span style="font-family: '微软雅黑',sans-serif;">每次执行定期任务脚本时,</span> <span style="font-family: '微软雅黑',sans-serif;">使用</span> find <span style="font-family: '微软雅黑',sans-serif;">命令来删除过期的快照:</span> <span style="font-family: '微软雅黑',sans-serif;">比如说,</span> <span style="font-family: '微软雅黑',sans-serif;">你可以保留最近</span> 48 <span style="font-family: '微软雅黑',sans-serif;">小时内的每小时快照,</span> <span style="font-family: '微软雅黑',sans-serif;">还可以保留最近一两个月的每日快照。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">至少每天一次,</span> <span style="font-family: '微软雅黑',sans-serif;">将</span> RDB <span style="font-family: '微软雅黑',sans-serif;">备份到你的数据中心之外,</span> <span style="font-family: '微软雅黑',sans-serif;">或者至少是备份到你运行</span> Redis <span style="font-family: '微软雅黑',sans-serif;">服务器的物理机器之外。</span>
</p>

<h3>
  <span id="1312_RDB">1.3.12 RDB<span style="font-family: '微软雅黑',sans-serif;">持久化配置</span></span>
</h3>

<p>
  <strong><span style="background: lime;">RDB</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">持久化基本配置</span></strong>
</p>

<p style="text-indent: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif;">修改配置文件</span>
</p>

<div class="cnblogs_code">
  <pre>save 900 1<span style="color: #000000;">

save 300 10 save 60 10000

<p style="text-indent: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif;">配置分别表示:</span>
</p>

<blockquote>
  <p style="text-indent: 15.75pt;">
    <span style="color: #000000; font-family: 'Courier New'; font-size: 12px; background-color: initial;">&bull; 900秒(15分钟)内有1个更改</span>
  </p>
  
  <pre><span>&bull; 300秒(5分钟)内有10个更改

• 60秒内有10000个更改
• 当达到以上定义的配置时间时,就将内存数据持久化到磁盘。

<p>
  <strong><span style="background: lime;">RDB</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">持久化高级配置</span></strong>
</p>

<div class="cnblogs_code">
  <pre>stop-writes-on-bgsave-<span style="color: #000000;">error yes

rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./clsn/redis/data/6379

<p>
  <span style="font-family: '微软雅黑',sans-serif;">以上配置分别表示:</span>
</p>

<blockquote>
  <pre><span>&bull; 后台备份进程出错时,主进程停不停止写入? 主进程不停止容易造成数据不一致

• 导出的rdb文件是否压缩 如果rdb的大小很大的话建议这么做 • 导入rbd恢复时数据时,要不要检验rdb的完整性 验证版本是不是一致 • 导出来的rdb文件名 • rdb的放置路径

<h3>
  <span id="1313_AOF">1.3.13 AOF<span style="font-family: '微软雅黑',sans-serif;">持久化配置</span></span>
</h3>

<p>
  <strong><span style="background: lime;">AOF</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">持久化基本配置</span></strong>
</p>

<div class="cnblogs_code">
  <pre>appendonly yes/<span style="color: #000000;">no

appendfsync always appendfsync everysec appendfsync no

<p style="text-indent: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif;">配置分别表示:</span>
</p>

<blockquote>
  <p style="text-indent: 15.75pt;">
    &nbsp;
  </p>
  
  <div>
    <p class="a">
      &bull; 是否打开aof日志功能
    </p>
    
    <p class="a">
      &bull; 每1个命令,都立即同步到aof
    </p>
    
    <p class="a">
      &bull; 每秒写1次
    </p>
    
    <p class="a">
      &bull; 写入工作交给操作系统,由操作系统判断缓冲区大小,统一写入到aof.
    </p>
  </div>
  
  <p style="text-indent: 15.75pt;">
    &nbsp;
  </p>
</blockquote>

<p>
  <strong><span style="background: lime;">AOF</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">持久化高级配置</span></strong>
</p>

<div class="cnblogs_code">
  <pre>no-appendfsync-on-rewrite yes/<span style="color: #000000;">no

auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb

<p>
  <span style="font-family: '微软雅黑',sans-serif;">配置分别表示:</span>
</p>

<blockquote>
  <div>
    <p class="a">
      &bull; 正在导出rdb快照的过程中,要不要停止同步aof
    </p>
    
    <p class="a">
      &bull; aof文件大小比起上次重写时的大小,增长率100%时重写,缺点:业务开始的时候,会重复重写多次。
    </p>
    
    <p class="a">
      &bull; aof文件,至少超过64M时,重写
    </p>
  </div>
</blockquote>

<h3>
  <span id="1314_RDBAOF">1.3.14 RDB<span style="font-family: '微软雅黑',sans-serif;">到</span>AOF<span style="font-family: '微软雅黑',sans-serif;">切换</span></span>
</h3>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">在</span> Redis 2.2 <span style="font-family: '微软雅黑',sans-serif;">或以上版本,可以在不重启的情况下,从</span> RDB <span style="font-family: '微软雅黑',sans-serif;">切换到</span> AOF <span style="font-family: '微软雅黑',sans-serif;">:</span>
</p>

<p style="text-indent: 21.0pt;">
  1<span style="font-family: '微软雅黑',sans-serif;">、为最新的</span> dump.rdb <span style="font-family: '微软雅黑',sans-serif;">文件创建一个备份。</span>
</p>

<p style="text-indent: 21.0pt;">
  2<span style="font-family: '微软雅黑',sans-serif;">、将备份放到一个安全的地方。</span>
</p>

<p style="text-indent: 21.0pt;">
  3<span style="font-family: '微软雅黑',sans-serif;">、执行以下两条命令</span>:
</p>

<div class="cnblogs_code">
  <pre>redis-<span style="color: #000000;">cli config set appendonly yes

redis-cli config set save “”

<p style="text-indent: 15.75pt;">
  4<span style="font-family: '微软雅黑',sans-serif;">、确保写命令会被正确地追加到</span> AOF <span style="font-family: '微软雅黑',sans-serif;">文件的末尾。</span>
</p>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">执行说明</span>
</p>

<blockquote>
  <div>
    <p class="a">
        执行的第一条命令开启了 AOF 功能: Redis 会阻塞直到初始 AOF 文件创建完成为止, 之后 Redis 会继续处理命令请求, 并开始将写入命令追加到 AOF 文件末尾。
    </p>
    
    <p class="a">
        执行的第二条命令用于关闭 RDB 功能。 这一步是可选的, 如果你愿意的话, 也可以同时使用 RDB 和 AOF 这两种持久化功能。
    </p>
  </div>
</blockquote>

<p style="text-indent: 21.0pt;">
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: red;">注意</span><span style="color: red;">:</span></strong><span style="font-family: '微软雅黑',sans-serif;">别忘了在</span> redis.conf <span style="font-family: '微软雅黑',sans-serif;">中打开</span> AOF <span style="font-family: '微软雅黑',sans-serif;">功能!</span> <span style="font-family: '微软雅黑',sans-serif;">否则的话,</span> <span style="font-family: '微软雅黑',sans-serif;">服务器重启之后,</span> <span style="font-family: '微软雅黑',sans-serif;">之前通过</span> CONFIG SET <span style="font-family: '微软雅黑',sans-serif;">设置的配置不会生效,</span> <span style="font-family: '微软雅黑',sans-serif;">程序会按原来的配置来启动服务器。</span>
</p>

<h2>
  <span id="14_Redis">1.4 Redis<span style="font-family: '微软雅黑',sans-serif;">管理实战</span></span>
</h2>

<h3>
  <span id="141">1.4.1 <span style="font-family: '微软雅黑',sans-serif;">基本数据类型</span></span>
</h3>

<table style="width: 100%; border-collapse: collapse; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td style="width: 33.84%; border-top: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: 1pt solid #9bbb59; border-right: none; background: #9bbb59; padding: 0cm 5.4pt;" width="33%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">类型</span></strong>
      </p>
    </td>
    
    <td style="width: 66.16%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="66%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">说明</span></strong>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 33.84%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="33%">
      <p style="text-align: center;" align="center">
        <strong>String&nbsp; </strong><strong><span style="font-family: '微软雅黑',sans-serif;">字符串</span></strong>
      </p>
    </td>
    
    <td style="width: 66.16%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="66%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        Redis <span style="font-family: '微软雅黑',sans-serif;">字符串数据类型的相关命令用于管理</span> redis <span style="font-family: '微软雅黑',sans-serif;">字符串值</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 33.84%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="33%">
      <p style="text-align: center;" align="center">
        <strong>Hash&nbsp; </strong><strong><span style="font-family: '微软雅黑',sans-serif;">哈希</span></strong>
      </p>
    </td>
    
    <td style="width: 66.16%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="66%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        Redis hash <span style="font-family: '微软雅黑',sans-serif;">是一个</span>string<span style="font-family: '微软雅黑',sans-serif;">类型的</span>field<span style="font-family: '微软雅黑',sans-serif;">和</span>value<span style="font-family: '微软雅黑',sans-serif;">的映射表,</span>hash<span style="font-family: '微软雅黑',sans-serif;">特别适合用于存储对象。</span>
      </p>
      
      <p style="text-align: justify; text-justify: inter-ideograph;">
        Redis <span style="font-family: '微软雅黑',sans-serif;">中每个</span> hash <span style="font-family: '微软雅黑',sans-serif;">可以存储</span> 232 - 1 <span style="font-family: '微软雅黑',sans-serif;">键值对(</span>40<span style="font-family: '微软雅黑',sans-serif;">多亿)。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 33.84%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="33%">
      <p style="text-align: center;" align="center">
        <strong>List&nbsp; </strong><strong><span style="font-family: '微软雅黑',sans-serif;">列表</span></strong>
      </p>
    </td>
    
    <td style="width: 66.16%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="66%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        Redis<span style="font-family: '微软雅黑',sans-serif;">列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)</span>
      </p>
      
      <p style="text-align: justify; text-justify: inter-ideograph;">
        &nbsp;
      </p>
      
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">一个列表最多可以包含</span> 232 - 1 <span style="font-family: '微软雅黑',sans-serif;">个元素</span> (4294967295, <span style="font-family: '微软雅黑',sans-serif;">每个列表超过</span>40<span style="font-family: '微软雅黑',sans-serif;">亿个元素</span>)<span style="font-family: '微软雅黑',sans-serif;">。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 33.84%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="33%">
      <p style="text-align: center;" align="center">
        <strong>Set&nbsp;&nbsp; </strong><strong><span style="font-family: '微软雅黑',sans-serif;">集合</span></strong>
      </p>
    </td>
    
    <td style="width: 66.16%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="66%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        Redis <span style="font-family: '微软雅黑',sans-serif;">的</span> Set <span style="font-family: '微软雅黑',sans-serif;">是</span> String <span style="font-family: '微软雅黑',sans-serif;">类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 33.84%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="33%">
      <p style="text-align: center;" align="center">
        <strong>Sorted set&nbsp; </strong><strong><span style="font-family: '微软雅黑',sans-serif;">有序集合</span></strong>
      </p>
    </td>
    
    <td style="width: 66.16%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="66%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        Redis <span style="font-family: '微软雅黑',sans-serif;">有序集合和集合一样也是</span>string<span style="font-family: '微软雅黑',sans-serif;">类型元素的集合</span>,<span style="font-family: '微软雅黑',sans-serif;">且不允许重复的成员。</span>
      </p>
    </td>
  </tr>
</table>

<h3>
  <span id="142_Key">1.4.2 <span style="font-family: '微软雅黑',sans-serif;">全局</span>Key<span style="font-family: '微软雅黑',sans-serif;">操作</span></span>
</h3>

<table style="width: 100%; border-collapse: collapse; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td style="width: 32.48%; border-top: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: 1pt solid #9bbb59; border-right: none; background: #9bbb59; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">命令</span></strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="67%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">含义</span></strong>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>KEYS * </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">查看</span>KEY<span style="font-family: '微软雅黑',sans-serif;">支持通配符</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>DEL</strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">删除给定的一个或多个</span>key
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>EXISTS </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">检查是否存在</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>RENAME </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">变更</span>KEY<span style="font-family: '微软雅黑',sans-serif;">名</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SORT </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">键值排序,有非数字时报错</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>TYPE</strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回键所存储值的类型</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>DUMP RESTORE</strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">序例化与反序列化</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>EXPIRE\ PEXPIRE </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">以秒</span>\<span style="font-family: '微软雅黑',sans-serif;">毫秒设定生存时间</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>TTL\ PTTL </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">以秒</span>\<span style="font-family: '微软雅黑',sans-serif;">毫秒为单位返回生存时间</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>PERSIST </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">取消生存实现设置</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>RANDOMKEY </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回数据库中的任意键</span>
      </p>
    </td>
  </tr>
</table>

<h3>
  <span id="143_String">1.4.3 String<span style="font-family: '微软雅黑',sans-serif;">(字符串)</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  string<span style="font-family: '微软雅黑',sans-serif;">是</span>redis<span style="font-family: '微软雅黑',sans-serif;">最基本的类型,一个</span>key<span style="font-family: '微软雅黑',sans-serif;">对应一个</span>value<span style="font-family: '微软雅黑',sans-serif;">。一个键最大能存储</span>512MB<span style="font-family: '微软雅黑',sans-serif;">。</span>
</p>

<table style="width: 100%; border-collapse: collapse; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td style="width: 46.06%; border-top: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: 1pt solid #9bbb59; border-right: none; background: #9bbb59; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">命令</span></strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="53%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">描述</span></strong>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SET key value </strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">设置指定</span> key <span style="font-family: '微软雅黑',sans-serif;">的值</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>GET key </strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取指定</span> key <span style="font-family: '微软雅黑',sans-serif;">的值。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>GETRANGE key start end</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回</span> key <span style="font-family: '微软雅黑',sans-serif;">中字符串值的子字符</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>GETSET key value</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">将给定</span> key <span style="font-family: '微软雅黑',sans-serif;">的值设为</span> value <span style="font-family: '微软雅黑',sans-serif;">,并返回</span> key <span style="font-family: '微软雅黑',sans-serif;">的旧值</span>(old value)<span style="font-family: '微软雅黑',sans-serif;">。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>GETBIT key offset</strong><strong><span style="font-family: '微软雅黑',sans-serif;">对</span> key </strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">所储存的字符串值,获取指定偏移量上的位</span>(bit)<span style="font-family: '微软雅黑',sans-serif;">。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>MGET key1 [key2..]</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取所有</span>(<span style="font-family: '微软雅黑',sans-serif;">一个或多个</span>)<span style="font-family: '微软雅黑',sans-serif;">给定</span> key <span style="font-family: '微软雅黑',sans-serif;">的值。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SETBIT key offset value</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">对</span> key <span style="font-family: '微软雅黑',sans-serif;">所储存的字符串值,设置或清除指定偏移量上的位</span>(bit)<span style="font-family: '微软雅黑',sans-serif;">。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SETEX key seconds value</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">将值</span> value <span style="font-family: '微软雅黑',sans-serif;">关联到</span> key <span style="font-family: '微软雅黑',sans-serif;">,并将</span> key <span style="font-family: '微软雅黑',sans-serif;">的过期时间设为</span> seconds (<span style="font-family: '微软雅黑',sans-serif;">以秒为单位</span>)<span style="font-family: '微软雅黑',sans-serif;">。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SETNX key value</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">只有在</span> key <span style="font-family: '微软雅黑',sans-serif;">不存在时设置</span> key <span style="font-family: '微软雅黑',sans-serif;">的值。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SETRANGE key offset value</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">用</span> value <span style="font-family: '微软雅黑',sans-serif;">参数覆写给定</span> key <span style="font-family: '微软雅黑',sans-serif;">所储存的字符串值,从偏移量</span> offset <span style="font-family: '微软雅黑',sans-serif;">开始。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>STRLEN key</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回</span> key <span style="font-family: '微软雅黑',sans-serif;">所储存的字符串值的长度。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>MSET key value [key value ...]</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">同时设置一个或多个</span> key-value <span style="font-family: '微软雅黑',sans-serif;">对。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>MSETNX key value [key value ...]</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">同时设置一个或多个</span> key-value <span style="font-family: '微软雅黑',sans-serif;">对,当且仅当所有给定</span> key <span style="font-family: '微软雅黑',sans-serif;">都不存在。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>PSETEX key milliseconds value</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">这个命令和</span> SETEX <span style="font-family: '微软雅黑',sans-serif;">命令相似,但它以毫秒为单位设置</span> key <span style="font-family: '微软雅黑',sans-serif;">的生存时间,而不是像</span> SETEX <span style="font-family: '微软雅黑',sans-serif;">命令那样,以秒为单位。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>INCR key</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">将</span> key <span style="font-family: '微软雅黑',sans-serif;">中储存的数字值增一。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>INCRBY key increment</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">将</span> key <span style="font-family: '微软雅黑',sans-serif;">所储存的值加上给定的增量值(</span>increment<span style="font-family: '微软雅黑',sans-serif;">)</span> <span style="font-family: '微软雅黑',sans-serif;">。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>INCRBYFLOAT key increment</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">将</span> key <span style="font-family: '微软雅黑',sans-serif;">所储存的值加上给定的浮点增量值(</span>increment<span style="font-family: '微软雅黑',sans-serif;">)</span> <span style="font-family: '微软雅黑',sans-serif;">。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>DECR key</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">将</span> key <span style="font-family: '微软雅黑',sans-serif;">中储存的数字值减一。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>DECRBY key decrementkey </strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">所储存的值减去给定的减量值(</span>decrement<span style="font-family: '微软雅黑',sans-serif;">)</span> <span style="font-family: '微软雅黑',sans-serif;">。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 46.06%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="46%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>APPEND key value</strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">如果</span> key <span style="font-family: '微软雅黑',sans-serif;">已经存在并且是一个字符串,</span> APPEND <span style="font-family: '微软雅黑',sans-serif;">命令将</span> <span style="font-family: '微软雅黑',sans-serif;">指定</span>value <span style="font-family: '微软雅黑',sans-serif;">追加到改</span> key <span style="font-family: '微软雅黑',sans-serif;">原来的值(</span>value<span style="font-family: '微软雅黑',sans-serif;">)的末尾。</span>
      </p>
    </td>
  </tr>
</table>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: red;">应用场景</span></strong>
</p>

<blockquote>
  <div>
    <p class="a">
      常规计数:微博数,粉丝数等。
    </p>
  </div>
</blockquote>

<h3>
  <span id="144_Hash">1.4.4 Hash<span style="font-family: '微软雅黑',sans-serif;">(字典)</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">我们可以将</span>Redis<span style="font-family: '微软雅黑',sans-serif;">中的</span>Hashes<span style="font-family: '微软雅黑',sans-serif;">类型看成具有</span>String Key<span style="font-family: '微软雅黑',sans-serif;">和</span>String Value<span style="font-family: '微软雅黑',sans-serif;">的</span>map<span style="font-family: '微软雅黑',sans-serif;">容器。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">所以该类型非常适合于存储值对象的信息。如</span>Username<span style="font-family: '微软雅黑',sans-serif;">、</span>Password<span style="font-family: '微软雅黑',sans-serif;">和</span>Age<span style="font-family: '微软雅黑',sans-serif;">等。如果</span>Hash<span style="font-family: '微软雅黑',sans-serif;">中包含很少的字段,那么该类型的数据也将仅占用很少的磁盘空间。每一个</span>Hash<span style="font-family: '微软雅黑',sans-serif;">可以存储</span>995701749 <span style="font-family: '微软雅黑',sans-serif;">个键值对。</span>
</p>

<table style="width: 100%; border-collapse: collapse; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td style="width: 50%; border-top: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: 1pt solid #9bbb59; border-right: none; background: #9bbb59; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">命令</span></strong>
      </p>
    </td>
    
    <td style="width: 50%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">描述</span></strong>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HDEL key field1 [field2] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">删除一个或多个哈希表字段</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HEXISTS key field </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">查看哈希表</span> key <span style="font-family: '微软雅黑',sans-serif;">中,指定的字段是否存在。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HGET key field </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取存储在哈希表中指定字段的值。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HGETALL key </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取在哈希表中指定</span> key <span style="font-family: '微软雅黑',sans-serif;">的所有字段和值</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HINCRBY key field increment </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">为哈希表</span> key <span style="font-family: '微软雅黑',sans-serif;">中的指定字段的整数值加上增量</span> increment <span style="font-family: '微软雅黑',sans-serif;">。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HINCRBYFLOAT key field increment</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        &nbsp;<span style="font-family: '微软雅黑',sans-serif;">为哈希表</span> key <span style="font-family: '微软雅黑',sans-serif;">中的指定字段的浮点数值加上增量</span> increment <span style="font-family: '微软雅黑',sans-serif;">。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HKEYS key </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取所有哈希表中的字段</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HLEN key </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取哈希表中字段的数量</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HMGET key field1 [field2] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取所有给定字段的值</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HMSET key field1 value1 [field2 value2 ] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">同时将多个</span> field-value (<span style="font-family: '微软雅黑',sans-serif;">域</span>-<span style="font-family: '微软雅黑',sans-serif;">值</span>)<span style="font-family: '微软雅黑',sans-serif;">对设置到哈希表</span> key <span style="font-family: '微软雅黑',sans-serif;">中。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HSET key field value </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">将哈希表</span> key <span style="font-family: '微软雅黑',sans-serif;">中的字段</span> field <span style="font-family: '微软雅黑',sans-serif;">的值设为</span> value <span style="font-family: '微软雅黑',sans-serif;">。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HSETNX key field value </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">只有在字段</span> field <span style="font-family: '微软雅黑',sans-serif;">不存在时,设置哈希表字段的值。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HVALS key </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取哈希表中所有值</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>HSCAN key cursor [MATCH pattern] [COUNT count] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">迭代哈希表中的键值对。</span>
      </p>
    </td>
  </tr>
</table>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: red;">应用场景:</span></strong>
</p>

<blockquote>
  <div>
    <p class="a">
      存储部分变更的数据,如用户信息等。
    </p>
  </div>
</blockquote>

<h3>
  <span id="145_LIST">1.4.5 LIST<span style="font-family: '微软雅黑',sans-serif;">(列表)</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  List<span style="font-family: '微软雅黑',sans-serif;">类型是按照插入顺序排序的字符串链表。和数据结构中的普通链表一样,我们可以在其头部</span>(left)<span style="font-family: '微软雅黑',sans-serif;">和尾部</span>(right)<span style="font-family: '微软雅黑',sans-serif;">添加新的元素。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">在插入时,如果该键并不存在,</span>Redis<span style="font-family: '微软雅黑',sans-serif;">将为该键创建一个新的链表。与此相反,如果链表中所有的元素均被移除,那么该键也将会被从数据库中删除。</span>
</p>

<p style="text-indent: 21.0pt;">
  List<span style="font-family: '微软雅黑',sans-serif;">中可以包含的最大元素数量是</span>4294967295<span style="font-family: '微软雅黑',sans-serif;">。</span>
</p>

<table style="width: 100%; border-collapse: collapse; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td style="width: 50%; border-top: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: 1pt solid #9bbb59; border-right: none; background: #9bbb59; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">命令</span></strong>
      </p>
    </td>
    
    <td style="width: 50%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">描述</span></strong>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>BLPOP key1 [key2 ] timeout</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">移出并获取列表的第一个元素,</span> <span style="font-family: '微软雅黑',sans-serif;">如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>BRPOP key1 [key2 ] timeout</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">移出并获取列表的最后一个元素,</span> <span style="font-family: '微软雅黑',sans-serif;">如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>&nbsp;</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        &nbsp;
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>BRPOPLPUSH source destination timeout</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它;</span> <span style="font-family: '微软雅黑',sans-serif;">如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>LINDEX key index </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">通过索引获取列表中的元素</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>LINSERT key BEFORE|AFTER pivot value</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">在列表的元素前或者后插入元素</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>LLEN key </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取列表长度</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>LPOP key </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">移出并获取列表的第一个元素</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>LPUSH key value1 [value2]</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">将一个或多个值插入到列表头部</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>LPUSHX key value </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">将一个值插入到已存在的列表头部</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>LRANGE key start stop </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取列表指定范围内的元素</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>LREM key count value </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">移除列表元素</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>LSET key index value </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">通过索引设置列表元素的值</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>LTRIM key start stop</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">对一个列表进行修剪</span>(trim)<span style="font-family: '微软雅黑',sans-serif;">,就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>RPOP key </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">移除并获取列表最后一个元素</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>RPOPLPUSH source destination </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">移除列表的最后一个元素,并将该元素添加到另一个列表并返回</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>RPUSH key value1 [value2] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">在列表中添加一个或多个值</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>RPUSHX key value </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">为已存在的列表添加值</span>
      </p>
    </td>
  </tr>
</table>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: red;">应用场景</span></strong>
</p>

<blockquote>
  <div>
    <p class="a">
      消息队列系统,比如sina微博
    </p>
  </div>
</blockquote>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">在</span>Redis<span style="font-family: '微软雅黑',sans-serif;">中我们的最新微博</span>ID<span style="font-family: '微软雅黑',sans-serif;">使用了常驻缓存,这是一直更新的。但是做了限制不能超过</span>5000<span style="font-family: '微软雅黑',sans-serif;">个</span>ID<span style="font-family: '微软雅黑',sans-serif;">,因此获取</span>ID<span style="font-family: '微软雅黑',sans-serif;">的函数会一直询问</span>Redis<span style="font-family: '微软雅黑',sans-serif;">。只有在</span>start/count<span style="font-family: '微软雅黑',sans-serif;">参数超出了这个范围的时候,才需要去访问数据库。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">系统不会像传统方式那样&ldquo;刷新&rdquo;缓存,</span>Redis<span style="font-family: '微软雅黑',sans-serif;">实例中的信息永远是一致的。</span>SQL<span style="font-family: '微软雅黑',sans-serif;">数据库(或是硬盘上的其他类型数据库)只是在用户需要获取&ldquo;很远&rdquo;的数据时才会被触发,而主页或第一个评论页是不会麻烦到硬盘上的数据库了。</span>
</p>

<h3>
  <span id="146_SET">1.4.6 SET(<span style="font-family: '微软雅黑',sans-serif;">集合</span>)</span>
</h3>

<p style="text-indent: 21.0pt;">
  Set<span style="font-family: '微软雅黑',sans-serif;">类型看作为没有排序的字符集合。</span>Set<span style="font-family: '微软雅黑',sans-serif;">可包含的最大元素数量是</span>4294967295<span style="font-family: '微软雅黑',sans-serif;">。如果多次添加相同元素,</span>Set<span style="font-family: '微软雅黑',sans-serif;">中将仅保留该元素的一份拷贝。</span>
</p>

<table style="width: 100%; border-collapse: collapse; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td style="width: 50%; border-top: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: 1pt solid #9bbb59; border-right: none; background: #9bbb59; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">命令</span></strong>
      </p>
    </td>
    
    <td style="width: 50%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">描述</span></strong>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SADD key member1 [member2] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">向集合添加一个或多个成员</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SCARD key </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取集合的成员数</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SDIFF key1 [key2] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回给定所有集合的差集</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SDIFFSTORE destination key1 [key2] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回给定所有集合的差集并存储在</span> destination <span style="font-family: '微软雅黑',sans-serif;">中</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SINTER key1 [key2] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回给定所有集合的交集</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SINTERSTORE destination key1 [key2] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回给定所有集合的交集并存储在</span> destination <span style="font-family: '微软雅黑',sans-serif;">中</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SISMEMBER key member </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">判断</span> member <span style="font-family: '微软雅黑',sans-serif;">元素是否是集合</span> key <span style="font-family: '微软雅黑',sans-serif;">的成员</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SMEMBERS key </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回集合中的所有成员</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SMOVE source destination member </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">将</span> member <span style="font-family: '微软雅黑',sans-serif;">元素从</span> source <span style="font-family: '微软雅黑',sans-serif;">集合移动到</span> destination <span style="font-family: '微软雅黑',sans-serif;">集合</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SPOP key </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">移除并返回集合中的一个随机元素</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SRANDMEMBER key [count] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回集合中一个或多个随机数</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SREM key member1 [member2] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">移除集合中一个或多个成员</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SUNION key1 [key2] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回所有给定集合的并集</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SUNIONSTORE destination key1 [key2] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">所有给定集合的并集存储在</span> destination <span style="font-family: '微软雅黑',sans-serif;">集合中</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SSCAN key cursor [MATCH pattern] [COUNT count]</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">迭代集合中的元素</span>
      </p>
    </td>
  </tr>
</table>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: red;">应用场景:</span></strong>
</p>

<blockquote>
  <div>
    <p class="a">
        在微博应用中,可以将一个用户所有的关注人存在一个集合中,将其所有粉丝存在一个集合。
    </p>
    
    <p class="a">
        Redis还为集合提供了求交集、并集、差集等操作,可以非常方便的实现如共同关注、共同喜好、二度好友等功能,对上面的所有集合操作,你还可以使用不同的命令选择将结果返回给客户端还是存集到一个新的集合中。
    </p>
  </div>
</blockquote>

<h3>
  <span id="147_SortedSet">1.4.7 SortedSet<span style="font-family: '微软雅黑',sans-serif;">(有序集合)</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  Sorted-Sets<span style="font-family: '微软雅黑',sans-serif;">中的每一个成员都会有一个分数</span>(score)<span style="font-family: '微软雅黑',sans-serif;">与之关联,</span>Redis<span style="font-family: '微软雅黑',sans-serif;">正是通过分数来为集合中的成员进行从小到大的排序。成员是唯一的,但是分数</span>(score)<span style="font-family: '微软雅黑',sans-serif;">却是可以重复的。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是</span>O(1)<span style="font-family: '微软雅黑',sans-serif;">。</span> <span style="font-family: '微软雅黑',sans-serif;">集合中最大的成员数为</span> 232 - 1 (4294967295, <span style="font-family: '微软雅黑',sans-serif;">每个集合可存储</span>40<span style="font-family: '微软雅黑',sans-serif;">多亿个成员</span>)<span style="font-family: '微软雅黑',sans-serif;">。</span>
</p>

<table style="width: 100%; border-collapse: collapse; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td style="width: 50%; border-top: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: 1pt solid #9bbb59; border-right: none; background: #9bbb59; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">命令</span></strong>
      </p>
    </td>
    
    <td style="width: 50%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">描述</span></strong>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZADD key score1 member1 [score2 member2] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">向有序集合添加一个或多个成员,或者更新已存在成员的分数</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZCARD key </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取有序集合的成员数</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZCOUNT key min max </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">计算在有序集合中指定区间分数的成员数</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZINCRBY key increment member </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">有序集合中对指定成员的分数加上增量</span> increment
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZINTERSTORE destination numkeys key [key ...] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合</span> key <span style="font-family: '微软雅黑',sans-serif;">中</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZLEXCOUNT key min max </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">在有序集合中计算指定字典区间内成员数量</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZRANGE key start stop [WITHSCORES] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">通过索引区间返回有序集合成指定区间内的成员</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZRANGEBYLEX key min max [LIMIT offset count] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">通过字典区间返回有序集合的成员</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">通过分数返回有序集合指定区间内的成员</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZRANK key member </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回有序集合中指定成员的索引</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZREM key member [member ...] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">移除有序集合中的一个或多个成员</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZREMRANGEBYLEX key min max </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">移除有序集合中给定的字典区间的所有成员</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZREMRANGEBYRANK key start stop </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">移除有序集合中给定的排名区间的所有成员</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZREMRANGEBYSCORE key min max </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">移除有序集合中给定的分数区间的所有成员</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZREVRANGE key start stop [WITHSCORES] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回有序集中指定区间内的成员,通过索引,分数从高到底</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZREVRANGEBYSCORE key max min [WITHSCORES] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回有序集中指定分数区间内的成员,分数从高到低排序</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZREVRANK key member </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回有序集合中指定成员的排名,有序集成员按分数值递减</span>(<span style="font-family: '微软雅黑',sans-serif;">从大到小</span>)<span style="font-family: '微软雅黑',sans-serif;">排序</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZSCORE key member </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回有序集中,成员的分数值</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZUNIONSTORE destination numkeys key [key ...] </strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">计算给定的一个或多个有序集的并集,并存储在新的</span> key <span style="font-family: '微软雅黑',sans-serif;">中</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ZSCAN key cursor [MATCH pattern] [COUNT count]</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">迭代有序集合中的元素(包括元素成员和元素分值)</span>
      </p>
    </td>
  </tr>
</table>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: red;">应用场景:</span></strong>
</p>

<blockquote>
  <div>
    <p class="a">
        排行榜应用,取TOP N操作这个需求与上面需求的不同之处在于,前面操作以时间为权重,这个是以某个条件为权重,比如按顶的次数排序,这时候就需要我们的sorted set出马了,将你要排序的值设置成sorted set的score,将具体的数据设置成相应的value,每次只需要执行一条ZADD命令即可。
    </p>
  </div>
</blockquote>

<h3>
  <span id="148">1.4.8 <span style="font-family: '微软雅黑',sans-serif;">消息模式</span></span>
</h3>

<p>
  Redis<span style="font-family: '微软雅黑',sans-serif;">发布消息通常有两种模式:</span>
</p>

<blockquote>
  <div>
    <p class="a">
      &bull; 队列模式(queuing)
    </p>
    
    <p class="a">
      &bull; 发布-订阅模式(publish-subscribe)
    </p>
  </div>
</blockquote>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">任务队列:</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">顾名思义,就是&ldquo;传递消息的队列&rdquo;。与任务队列进行交互的实体有两类,一类是生产者(</span>producer<span style="font-family: '微软雅黑',sans-serif;">),另一类则是消费者(</span>consumer<span style="font-family: '微软雅黑',sans-serif;">)。生产者将需要处理的任务放入任务队列中,而消费者则不断地从任务独立中读入任务信息并执行。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">任务队列的好处:</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; background: yellow;">&bull;</span><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; background: yellow;">松耦合。</span></strong>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">生产者和消费者只需按照约定的任务描述格式,进行编写代码。</span>
</p>

<p style="text-indent: 21.0pt;">
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: yellow;">&bull;</span> </strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: yellow;">易于扩展。</span></strong>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">多消费者模式下,消费者可以分布在多个不同的服务器中,由此降低单台服务器的负载。</span>
</p>

<h3>
  <span id="149_Redis">1.4.9 Redis <span style="font-family: '微软雅黑',sans-serif;">发布订阅</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">其实从</span>Pub/Sub<span style="font-family: '微软雅黑',sans-serif;">的机制来看,它更像是一个广播系统,多个</span>Subscriber<span style="font-family: '微软雅黑',sans-serif;">可以订阅多个</span>Channel<span style="font-family: '微软雅黑',sans-serif;">,多个</span>Publisher<span style="font-family: '微软雅黑',sans-serif;">可以往多个</span>Channel<span style="font-family: '微软雅黑',sans-serif;">中发布消息。可以这么简单的理解:</span>
</p>

<blockquote>
  <p>
    &bull;&nbsp;Subscriber:收音机,可以收到多个频道,并以队列方式显示
  </p>
  
  <p>
    &bull;&nbsp;Publisher:电台,可以往不同的FM频道中发消息
  </p>
  
  <p>
    &bull;&nbsp;Channel:不同频率的FM频道
  </p>
</blockquote>

<h4>
  <span id="1491nbsp"><strong><span style="courier new"4courier new"; background: yellow;">1.4.9.1&nbsp;</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: yellow;">发布订阅模型</span></strong></span>
</h4>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif;">一个</span>Publisher</strong><strong><span style="font-family: '微软雅黑',sans-serif;">,多个</span>Subscriber</strong><strong><span style="font-family: '微软雅黑',sans-serif;">模型</span></strong>
</p>

<p>
  <strong>&nbsp;&nbsp; </strong><span style="font-family: '微软雅黑',sans-serif;">如下图所示,可以作为消息队列或者消息管道。</span>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">主要应用:<em>通知、公告。</em></span>
</p>

<p style="text-align: center;" align="center">
  <img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203150217281-1667256027.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />&nbsp;
</p>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif;">多个</span>Publisher</strong><strong><span style="font-family: '微软雅黑',sans-serif;">,一个</span>Subscriber</strong><strong><span style="font-family: '微软雅黑',sans-serif;">模型</span></strong>
</p>

<p>
  &nbsp;&nbsp; <span style="font-family: '微软雅黑',sans-serif;">可以将</span>PubSub<span style="font-family: '微软雅黑',sans-serif;">做成独立的</span>HTTP<span style="font-family: '微软雅黑',sans-serif;">接口,各应用程序作为</span>Publisher<span style="font-family: '微软雅黑',sans-serif;">向</span>Channel<span style="font-family: '微软雅黑',sans-serif;">中发送消息,</span>Subscriber<span style="font-family: '微软雅黑',sans-serif;">端收到消息后执行相应的业务逻辑,比如写数据库,显示等等。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">主要应用:<em>排行榜、投票、计数。</em></span>
</p>

<p style="text-align: center;" align="center">
  &nbsp;<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203150234531-91611195.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />
</p>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif;">多个</span>Publisher</strong><strong><span style="font-family: '微软雅黑',sans-serif;">,多个</span>Subscriber</strong><strong><span style="font-family: '微软雅黑',sans-serif;">模型</span></strong>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">故名思议,就是可以向不同的</span>Channel<span style="font-family: '微软雅黑',sans-serif;">中发送消息,由不同的</span>Subscriber<span style="font-family: '微软雅黑',sans-serif;">接收。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">主要应用:群聊、聊天。</span>
</p>

<h4>
  <span id="1492nbsp"><strong>1.4.9.2&nbsp;</strong><strong><span style="font-family: '微软雅黑',sans-serif;">实践发布订阅</span></strong></span>
</h4>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: yellow;">发布订阅实践命令</span></strong>
</p>

<table style="width: 100%; border-collapse: collapse; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td style="width: 50%; border-top: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: 1pt solid #9bbb59; border-right: none; background: #9bbb59; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">命令</span></strong>
      </p>
    </td>
    
    <td style="width: 50%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">描述</span></strong>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>PUBLISH channel msg</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">将信息</span> message <span style="font-family: '微软雅黑',sans-serif;">发送到指定的频道</span> channel
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SUBSCRIBE channel [channel ...]</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">订阅频道,可以同时订阅多个频道</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>UNSUBSCRIBE [channel ...]</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">取消订阅指定的频道</span>, <span style="font-family: '微软雅黑',sans-serif;">如果不指定频道,则会取消订阅所有频道</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>PSUBSCRIBE pattern [pattern ...]</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">订阅一个或多个符合给定模式的频道,每个模式以</span> * <span style="font-family: '微软雅黑',sans-serif;">作为匹配符,比如</span> it* <span style="font-family: '微软雅黑',sans-serif;">匹配所有以</span> it <span style="font-family: '微软雅黑',sans-serif;">开头的频道</span>( it.news <span style="font-family: '微软雅黑',sans-serif;">、</span> it.blog <span style="font-family: '微软雅黑',sans-serif;">、</span> it.tweets <span style="font-family: '微软雅黑',sans-serif;">等等</span>)<span style="font-family: '微软雅黑',sans-serif;">,</span> news.* <span style="font-family: '微软雅黑',sans-serif;">匹配所有以</span> news. <span style="font-family: '微软雅黑',sans-serif;">开头的频道</span>( news.it <span style="font-family: '微软雅黑',sans-serif;">、</span> news.global.today <span style="font-family: '微软雅黑',sans-serif;">等等</span>)<span style="font-family: '微软雅黑',sans-serif;">,诸如此类</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>PUNSUBSCRIBE [pattern [pattern ...]]</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">退订指定的规则</span>, <span style="font-family: '微软雅黑',sans-serif;">如果没有参数则会退订所有规则</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>PUBSUB subcommand [argument [argument ...]]</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">查看订阅与发布系统状态</span>
      </p>
    </td>
  </tr>
</table>

<p style="text-indent: 21.0pt;">
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: red;">注意:使用发布订阅模式实现的消息队列,当有客户端订阅</span><span style="color: red;">channel</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: red;">后只能收到后续发布到该频道的消息,之前发送的不会缓存,必须</span><span style="color: red;">Provider</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: red;">和</span><span style="color: red;">Consumer</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: red;">同时在线。</span></strong>
</p>

<h4>
  <span id="1493nbsp"><strong>1.4.9.3&nbsp;</strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; background: yellow;">消息队列系统对比</span></strong></span>
</h4>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">客户端在执行订阅命令之后进入了订阅状态,只能接收</span> SUBSCRIBE <span style="font-family: '微软雅黑',sans-serif;">、</span>PSUBSCRIBE<span style="font-family: '微软雅黑',sans-serif;">、</span> UNSUBSCRIBE <span style="font-family: '微软雅黑',sans-serif;">、</span>PUNSUBSCRIBE <span style="font-family: '微软雅黑',sans-serif;">四个命令。</span>
</p>

<p style="text-indent: 21.0pt;">
  &nbsp;<span style="font-family: '微软雅黑',sans-serif;">开启的订阅客户端,无法收到该频道之前的消息,因为</span> Redis <span style="font-family: '微软雅黑',sans-serif;">不会对发布的消息进行持久化。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">和很多专业的消息队列系统(例如</span>Kafka<span style="font-family: '微软雅黑',sans-serif;">、</span>RocketMQ<span style="font-family: '微软雅黑',sans-serif;">)相比,</span>Redis<span style="font-family: '微软雅黑',sans-serif;">的发布订阅略显粗糙,例如无法实现消息堆积和回溯。但胜在足够简单,如果当前场景可以容忍的这些缺点,也不失为一个不错的选择。</span>
</p>

<h3>
  <span id="1410_Redis">1.4.10 Redis<span style="font-family: '微软雅黑',sans-serif;">事务管理</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  redis<span style="font-family: '微软雅黑',sans-serif;">中的事务跟关系型数据库中的事务是一个相似的概念,但是有不同之处。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">关系型数据库事务执行失败后面的</span>sql<span style="font-family: '微软雅黑',sans-serif;">语句不在执行,而</span>redis<span style="font-family: '微软雅黑',sans-serif;">中的一条命令执行失败,其余的命令照常执行。</span>
</p>

<p style="text-indent: 21.0pt;">
  redis<span style="font-family: '微软雅黑',sans-serif;">中开启一个事务是使用</span>multi<span style="font-family: '微软雅黑',sans-serif;">,相当于</span>begin\start transaction<span style="font-family: '微软雅黑',sans-serif;">,</span>exec<span style="font-family: '微软雅黑',sans-serif;">提交事务,</span>discard<span style="font-family: '微软雅黑',sans-serif;">取消队列命令(非回滚操作)。</span>
</p>

<p>
  &nbsp;&nbsp; <strong><span style="background: yellow;">redis</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: yellow;">于</span><span style="background: yellow;">mysql</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; background: yellow;">对比</span></strong>
</p>

<table style="width: 100%; border-collapse: collapse; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td style="width: 12.16%; border-top: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: 1pt solid #9bbb59; border-right: none; background: #9bbb59; padding: 0cm 5.4pt;" width="12%">
      <p style="text-align: center;" align="center">
        <strong>&nbsp;</strong>
      </p>
    </td>
    
    <td style="width: 33.9%; border-top: solid #9BBB59 1.0pt; border-left: none; border-bottom: solid #9BBB59 1.0pt; border-right: none; background: #9BBB59; padding: 0cm 5.4pt 0cm 5.4pt;" width="33%">
      <p style="text-align: center;" align="center">
        <strong><span style="color: white;">MySQL</span></strong>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="53%">
      <p style="text-align: center;" align="center">
        <strong><span style="color: white;">Redis</span></strong>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 12.16%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="12%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif;">开启</span></strong>
      </p>
    </td>
    
    <td style="width: 33.9%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="33%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        start transaction/begin
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        multi
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 12.16%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="12%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif;">语句</span></strong>
      </p>
    </td>
    
    <td style="width: 33.9%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="33%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">普通</span>SQL
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">普通命令</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 12.16%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="12%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif;">失败</span></strong>
      </p>
    </td>
    
    <td style="width: 33.9%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="33%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        rollback <span style="font-family: '微软雅黑',sans-serif;">回滚</span>
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        discard <span style="font-family: '微软雅黑',sans-serif;">取消</span>(<span style="font-family: '微软雅黑',sans-serif;">不叫回滚,是队列里面的命令不执行,队列里面的任务根本就没有执行。而不是执行了也可以撤回来</span>)
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 12.16%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="12%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif;">成功</span></strong>
      </p>
    </td>
    
    <td style="width: 33.9%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="33%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        commit
      </p>
    </td>
    
    <td style="width: 53.94%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="53%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        exec
      </p>
    </td>
  </tr>
</table>

<h4>
  <span id="14101nbspnbspnbspnbsp_Redis"><strong><span style="courier new"4courier new"; background: yellow;">1.4.10.1<span style="font-variant-numeric: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="background: yellow;">Redis </span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: yellow;">事务命令</span></strong></span>
</h4>

<table style="width: 100%; border-collapse: collapse; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td style="width: 28.42%; border-top: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: 1pt solid #9bbb59; border-right: none; background: #9bbb59; padding: 0cm 5.4pt;" width="28%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">命令</span></strong>
      </p>
    </td>
    
    <td style="width: 71.58%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="71%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">描述</span></strong>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 28.42%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="28%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>DISCARD</strong>
      </p>
    </td>
    
    <td style="width: 71.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="71%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">取消事务,放弃执行事务块内的所有命令。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 28.42%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="28%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>EXEC </strong>
      </p>
    </td>
    
    <td style="width: 71.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="71%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">执行所有事务块内的命令。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 28.42%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="28%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>MULTI </strong>
      </p>
    </td>
    
    <td style="width: 71.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="71%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">标记一个事务块的开始。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 28.42%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="28%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>UNWATCH</strong>
      </p>
    </td>
    
    <td style="width: 71.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="71%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">取消</span> WATCH <span style="font-family: '微软雅黑',sans-serif;">命令对所有</span> key <span style="font-family: '微软雅黑',sans-serif;">的监视。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 28.42%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="28%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>WATCH key [key ...]</strong>
      </p>
    </td>
    
    <td style="width: 71.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="71%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">监视一个</span>(<span style="font-family: '微软雅黑',sans-serif;">或多个</span>) key <span style="font-family: '微软雅黑',sans-serif;">,如果在事务执行之前这个</span>(<span style="font-family: '微软雅黑',sans-serif;">或这些</span>) key <span style="font-family: '微软雅黑',sans-serif;">被其他命令所改动,那么事务将被打断。</span>
      </p>
    </td>
  </tr>
</table>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">事务执行举例</span>
</p>

<p style="text-indent: 21.0pt;">
  &nbsp;
</p>

<div class="cnblogs_code">
  <pre>ZADD salary 2000<span style="color: #000000;"> user1

ZADD salary 3000 user2 ZRANGE salary 0 -1 WITHSCORES MULTI ZINCRBY salary 1000 user1 ZINCRBY salary -1000 user2 EXEC

<h4>
  <span id="14102nbspnbspnbspnbsp_Redis"><strong><span style="courier new"4courier new"; background: yellow;">1.4.10.2<span style="font-variant-numeric: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="background: yellow;">Redis</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: yellow;">中事务中的锁机制</span></strong></span>
</h4>

<p style="text-indent: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif;">举例:我正在买票&nbsp;<span class="cnblogs_code">Ticket -1 , money -100</span>&nbsp;</span>
</p>

<p style="text-indent: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif;">而票只有</span>1<span style="font-family: '微软雅黑',sans-serif;">张</span>, <span style="font-family: '微软雅黑',sans-serif;">如果在我</span>multi<span style="font-family: '微软雅黑',sans-serif;">之后</span>,<span style="font-family: '微软雅黑',sans-serif;">和</span>exec<span style="font-family: '微软雅黑',sans-serif;">之前</span>, <span style="font-family: '微软雅黑',sans-serif;">票被别人买了,即</span>ticket<span style="font-family: '微软雅黑',sans-serif;">变成</span><span style="font-family: '微软雅黑',sans-serif;">了。</span>
</p>

<p style="text-indent: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif;">我该如何观察这种情景</span>,<span style="font-family: '微软雅黑',sans-serif;">并不再提交:</span>
</p>

<p style="text-indent: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif;">悲观的想法</span>:
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">世界充满危险</span>,<span style="font-family: '微软雅黑',sans-serif;">肯定有人和我抢</span>, <span style="font-family: '微软雅黑',sans-serif;">给</span> ticket<span style="font-family: '微软雅黑',sans-serif;">上锁</span>, <span style="font-family: '微软雅黑',sans-serif;">只有我能操作</span>. <strong>[</strong><strong><span style="font-family: '微软雅黑',sans-serif;">悲观锁</span>]</strong>
</p>

<p style="text-indent: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif;">乐观的想法</span>:
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">没有那么人和我抢</span>,<span style="font-family: '微软雅黑',sans-serif;">因此</span>,<span style="font-family: '微软雅黑',sans-serif;">我只需要注意</span>,<span style="font-family: '微软雅黑',sans-serif;">有没有人更改</span>ticket<span style="font-family: '微软雅黑',sans-serif;">的值就可以了</span><strong> [</strong><strong><span style="font-family: '微软雅黑',sans-serif;">乐观锁</span>]</strong>
</p>

<p style="text-indent: 21.0pt;">
  Redis<span style="font-family: '微软雅黑',sans-serif;">的事务中</span>,<strong><em><span style="font-family: '微软雅黑',sans-serif;">启用的是乐观锁</span></em></strong>,<span style="font-family: '微软雅黑',sans-serif;">只负责监测</span>key<span style="font-family: '微软雅黑',sans-serif;">没有被改动</span>.
</p>

<h4>
  <span id="14103nbspnbspnbspnbsp_Redis"><strong><span style="courier new"4courier new"; background: yellow;">1.4.10.3<span style="font-variant-numeric: normal; font-variant-east-asian: normal; font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="background: yellow;">Redis</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: yellow;">服务管理命令</span></strong></span>
</h4>

<table style="width: 100%; border-collapse: collapse; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td style="width: 32.48%; border-top: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: 1pt solid #9bbb59; border-right: none; background: #9bbb59; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">命令</span></strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="67%">
      <p style="text-align: center;" align="center">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">描述</span></strong>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>BGREWRITEAOF</strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">异步执行一个</span> AOF<span style="font-family: '微软雅黑',sans-serif;">(</span>AppendOnly File<span style="font-family: '微软雅黑',sans-serif;">)</span> <span style="font-family: '微软雅黑',sans-serif;">文件重写操作</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>BGSAVE </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">在后台异步保存当前数据库的数据到磁盘</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>CLIENT KILL [ip:port] [ID client-id]</strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">关闭客户端连接</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>CLIENT LIST</strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取连接到服务器的客户端连接列表</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>CLIENT GETNAME </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取连接的名称</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>CLIENT PAUSE timeout</strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">在指定时间内终止运行来自客户端的命令</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>CLIENT SETNAME connection-name</strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">设置当前连接的名称</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>CLUSTER SLOTS </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取集群节点的映射数组</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>COMMAND </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取</span> Redis <span style="font-family: '微软雅黑',sans-serif;">命令详情数组</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>COMMAND COUNT</strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取</span> Redis <span style="font-family: '微软雅黑',sans-serif;">命令总数</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>COMMAND GETKEYS </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取给定命令的所有键</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>TIME</strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回当前服务器时间</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>COMMAND INFO command-name [command-name ...]</strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取指定</span> Redis <span style="font-family: '微软雅黑',sans-serif;">命令描述的数组</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>CONFIG GET parameter </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取指定配置参数的值</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>CONFIG REWRITE </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">对启动</span> Redis <span style="font-family: '微软雅黑',sans-serif;">服务器时所指定的</span> redis.conf <span style="font-family: '微软雅黑',sans-serif;">配置文件进行改写</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>CONFIG SET parameter value </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">修改</span> redis <span style="font-family: '微软雅黑',sans-serif;">配置参数,无需重启</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>CONFIG RESETSTAT </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">重置</span> INFO <span style="font-family: '微软雅黑',sans-serif;">命令中的某些统计数据</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>DBSIZE </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回当前数据库的</span> key <span style="font-family: '微软雅黑',sans-serif;">的数量</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>DEBUG OBJECT key </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取</span> key <span style="font-family: '微软雅黑',sans-serif;">的调试信息</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>DEBUG SEGFAULT </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">让</span> Redis <span style="font-family: '微软雅黑',sans-serif;">服务崩溃</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>FLUSHALL </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">删除所有数据库的所有</span>key
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>FLUSHDB </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">删除当前数据库的所有</span>key
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>INFO [section] </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">获取</span> Redis <span style="font-family: '微软雅黑',sans-serif;">服务器的各种信息和统计数值</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>LASTSAVE </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回最近一次</span> Redis <span style="font-family: '微软雅黑',sans-serif;">成功将数据保存到磁盘上的时间,以</span> UNIX <span style="font-family: '微软雅黑',sans-serif;">时间戳格式表示</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>MONITOR </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">实时打印出</span> Redis <span style="font-family: '微软雅黑',sans-serif;">服务器接收到的命令,调试用</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>ROLE </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回主从实例所属的角色</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SAVE </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">异步保存数据到硬盘</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SHUTDOWN [NOSAVE] [SAVE] </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">异步保存数据到硬盘,并关闭服务器</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SLAVEOF host port </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">将当前服务器转变为指定服务器的从属服务器</span>(slave server)
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SLOWLOG subcommand [argument]</strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">管理</span> redis <span style="font-family: '微软雅黑',sans-serif;">的慢日志</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 32.48%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="32%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SYNC </strong>
      </p>
    </td>
    
    <td style="width: 67.52%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="67%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">用于复制功能</span>(replication)<span style="font-family: '微软雅黑',sans-serif;">的内部命令</span>
      </p>
    </td>
  </tr>
</table>

<h3>
  <span id="1411_redis">1.4.11 redis<span style="font-family: '微软雅黑',sans-serif;">慢日志查询</span></span>
</h3>

<p style="text-indent: 7.1pt;">
  Slow log <span style="font-family: '微软雅黑',sans-serif;">是</span> Redis <span style="font-family: '微软雅黑',sans-serif;">用来记录查询执行时间的日志系统。</span>
</p>

<p style="text-indent: 7.1pt;">
  slow log <span style="font-family: '微软雅黑',sans-serif;">保存在内存里面,读写速度非常快</span>
</p>

<p style="text-indent: 7.1pt;">
  <span style="font-family: '微软雅黑',sans-serif;">可以通过改写</span> redis.conf <span style="font-family: '微软雅黑',sans-serif;">文件或者用</span> CONFIG GET <span style="font-family: '微软雅黑',sans-serif;">和</span> CONFIG SET <span style="font-family: '微软雅黑',sans-serif;">命令对它们动态地进行修改</span>
</p>

<div class="cnblogs_code">
  <pre>slowlog-log-slower-than 10000<span style="color: #000000;"> 超过多少微秒

CONFIG SET slowlog-log-slower-than 100 CONFIG SET slowlog-max-len 1000 保存多少条慢日志 CONFIG GET slow* SLOWLOG GET SLOWLOG RESET

<h2>
  <span id="15_Redis">1.5 Redis<span style="font-family: '微软雅黑',sans-serif;">主从复制</span></span>
</h2>

<h3>
  <span id="151_redis">1.5.1 redis<span style="font-family: '微软雅黑',sans-serif;">复制特性</span></span>
</h3>

<p style="margin-left: 12.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">⚗</span> <span style="font-family: '微软雅黑',sans-serif;">使用异步复制。</span>
</p>

<p style="margin-left: 12.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">⚗</span> <span style="font-family: '微软雅黑',sans-serif;">一个主服务器可以有多个从服务器。</span>
</p>

<p style="margin-left: 12.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">⚗</span> <span style="font-family: '微软雅黑',sans-serif;">从服务器也可以有自己的从服务器。</span>
</p>

<p style="margin-left: 12.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">⚗</span> <span style="font-family: '微软雅黑',sans-serif;">复制功能不会阻塞主服务器。</span>
</p>

<p style="margin-left: 12.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">⚗</span> <span style="font-family: '微软雅黑',sans-serif;">可以通过复制功能来让主服务器免于执行持久化操作,由从服务器去执行持久化操作即可。</span>
</p>

<p style="text-align: center;" align="center">
  <img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203180911906-96199576.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />
</p>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: yellow;">关闭主服务器持久化时,复制功能的数据安全</span></strong>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">当配置</span>Redis<span style="font-family: '微软雅黑',sans-serif;">复制功能时,强烈建议打开主服务器的持久化功能。</span> <span style="font-family: '微软雅黑',sans-serif;">否则的话,由于延迟等问题,部署的服务应该要避免自动拉起。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">为了帮助理解主服务器关闭持久化时自动拉起的危险性,参考一下以下会导致主从服务器数据全部丢失的例子:</span>
</p>

<blockquote>
  <p style="text-indent: 21.0pt;">
    <span style="text-indent: 0px; background-color: initial;">1.&nbsp;假设节点A为主服务器,并且关闭了持久化。&nbsp;并且节点B和节点C从节点A复制数据</span>
  </p>
  
  <p style="text-indent: 21.0pt;">
    2.&nbsp;节点A崩溃,然后由自动拉起服务重启了节点A.&nbsp;由于节点A的持久化被关闭了,所以重启之后没有任何数据
  </p>
  
  <p style="text-indent: 21.0pt;">
    3.&nbsp;节点B和节点C将从节点A复制数据,但是A的数据是空的,&nbsp;于是就把自身保存的数据副本删除。
  </p>
</blockquote>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">在关闭主服务器上的持久化,并同时开启自动拉起进程的情况下,即便使用</span>Sentinel<span style="font-family: '微软雅黑',sans-serif;">来实现</span>Redis<span style="font-family: '微软雅黑',sans-serif;">的高可用性,也是非常危险的。</span> <span style="font-family: '微软雅黑',sans-serif;">因为主服务器可能拉起得非常快,以至于</span>Sentinel<span style="font-family: '微软雅黑',sans-serif;">在配置的心跳时间间隔内没有检测到主服务器已被重启,然后还是会执行上面的数据丢失的流程。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">无论何时,数据安全都是极其重要的,所以应该<span style="text-decoration: underline;">禁止主服务器关闭持久化的同时自动拉起</span>。</span>
</p>

<h3>
  <span id="152">1.5.2 <span style="font-family: '微软雅黑',sans-serif;">主从复制原理</span></span>
</h3>

<p>
  <strong>redis </strong><strong><span style="font-family: '微软雅黑',sans-serif;">主从同步有两种方式(或者所两个阶段):全同步和部分同步。</span></strong>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">主从刚刚连接的时候,进行全同步;全同步结束后,进行部分同步。当然,如果有需要,</span>slave <span style="font-family: '微软雅黑',sans-serif;">在任何时候都可以发起全同步。</span>
</p>

<p style="text-indent: 21.0pt;">
  redis <span style="font-family: '微软雅黑',sans-serif;">策略是,无论如何,首先会尝试进行部分同步,如不成功,要求从机进行全同步,并启动</span> BGSAVE<span style="font-family: '微软雅黑',sans-serif;">&hellip;&hellip;</span>BGSAVE <span style="font-family: '微软雅黑',sans-serif;">结束后,传输</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件;如果成功,允许从机进行部分同步,并传输积压空间中的数据。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">下面这幅图,总结了主从同步的机制:</span>
</p>

<p style="text-align: center;" align="center">
  &nbsp;<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203180923062-1638674965.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />
</p>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif;">主从复制原理:</span></strong>
</p>

<p style="text-indent: 21.0pt;">
  1. <span style="font-family: '微软雅黑',sans-serif;">从服务器向主服务器发送</span> SYNC <span style="font-family: '微软雅黑',sans-serif;">命令。</span>
</p>

<p style="text-indent: 21.0pt;">
  2. <span style="font-family: '微软雅黑',sans-serif;">接到</span> SYNC <span style="font-family: '微软雅黑',sans-serif;">命令的主服务器会调用</span>BGSAVE <span style="font-family: '微软雅黑',sans-serif;">命令,创建一个</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件,并使用缓冲区记录接下来执行的所有写命令。</span>
</p>

<p style="text-indent: 21.0pt;">
  3. <span style="font-family: '微软雅黑',sans-serif;">当主服务器执行完</span> BGSAVE <span style="font-family: '微软雅黑',sans-serif;">命令时,它会向从服务器发送</span> RDB <span style="font-family: '微软雅黑',sans-serif;">文件,而从服务器则会接收并载入这个文件。</span>
</p>

<p style="text-indent: 21.0pt;">
  4. <span style="font-family: '微软雅黑',sans-serif;">主服务器将缓冲区储存的所有写命令发送给从服务器执行。</span>
</p>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif;">命令的传播</span></strong>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">在主从服务器完成同步之后,主服务器每执行一个写命令,它都会将被执行的写命令发送给从服务器执行,这个操作被称为&ldquo;命令传播&rdquo;(</span>command propagate<span style="font-family: '微软雅黑',sans-serif;">)。</span>
</p>

<p style="margin: 0cm; margin-bottom: .0001pt; text-align: center; layout-grid-mode: both;" align="center">
  <img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203180935640-1744764809.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />&nbsp;
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">命令传播是一个持续的过程:只要复制仍在继续,命令传播就会一直进行,使得主从服务器的状态可以一直保持一致。</span>
</p>

<h3>
  <span id="153_SYNCPSYNC">1.5.3 <span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: yellow;">复制中的</span><span style="background: yellow;">SYNC</span><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; background: yellow;">与</span><span style="background: yellow;">PSYNC</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">在</span> Redis 2.8 <span style="font-family: '微软雅黑',sans-serif;">版本之前,</span> <span style="font-family: '微软雅黑',sans-serif;">断线之后重连的从服务器总要执行一次完整重同步(</span>full resynchronization<span style="font-family: '微软雅黑',sans-serif;">)操作。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">从</span> Redis 2.8 <span style="font-family: '微软雅黑',sans-serif;">开始,</span>Redis <span style="font-family: '微软雅黑',sans-serif;">使用</span> PSYNC<span style="font-family: '微软雅黑',sans-serif;">命令代替</span> SYNC <span style="font-family: '微软雅黑',sans-serif;">命令。</span>PSYNC <span style="font-family: '微软雅黑',sans-serif;">比起</span> SYNC <span style="font-family: '微软雅黑',sans-serif;">的最大改进在于</span> PSYNC <span style="font-family: '微软雅黑',sans-serif;">实现了部分重同步(</span>partial resync<span style="font-family: '微软雅黑',sans-serif;">)特性:在主从服务器断线并且重新连接的时候,只要条件允许,</span>PSYNC <span style="font-family: '微软雅黑',sans-serif;">可以让主服务器只向从服务器同步断线期间缺失的数据,而不用重新向从服务器同步整个数据库。</span>
</p>

<h3>
  <span id="154">1.5.4 <span style="font-family: '微软雅黑',sans-serif;">复制的一致性问题</span></span>
</h3>

<p style="margin: 0cm; margin-bottom: .0001pt; text-align: center; layout-grid-mode: both;" align="center">
  <img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203180944625-1490879935.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />&nbsp;
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">在读写分离环境下,客户端向主服务器发送写命令</span> SET n 10086<span style="font-family: '微软雅黑',sans-serif;">,主服务器在执行这个写命令之后,向客户端返回回复,并将这个写命令传播给从服务器。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">接到回复的客户端继续向从服务器发送读命令</span> GET n <span style="font-family: '微软雅黑',sans-serif;">,并且因为网络状态的原因,客户端的</span> GET<span style="font-family: '微软雅黑',sans-serif;">命令比主服务器传播的</span> SET <span style="font-family: '微软雅黑',sans-serif;">命令更快到达了从服务器。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">因为从服务器键</span> n <span style="font-family: '微软雅黑',sans-serif;">的值还未被更新,所以客户端在从服务器读取到的将是一个错误(过期)的</span> n<span style="font-family: '微软雅黑',sans-serif;">值。</span>
</p>

<h3>
  <span id="155">1.5.5 <span style="font-family: '微软雅黑',sans-serif;">复制安全性提升</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">主服务器只在有至少</span> N <span style="font-family: '微软雅黑',sans-serif;">个从服务器的情况下,才执行写操作从</span> Redis 2.8 <span style="font-family: '微软雅黑',sans-serif;">开始,</span> <span style="font-family: '微软雅黑',sans-serif;">为了保证数据的安全性,</span> <span style="font-family: '微软雅黑',sans-serif;">可以通过配置,</span> <span style="font-family: '微软雅黑',sans-serif;">让主服务器只在有至少</span> N <span style="font-family: '微软雅黑',sans-serif;">个当前已连接从服务器的情况下,</span> <span style="font-family: '微软雅黑',sans-serif;">才执行写命令。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">不过,</span> <span style="font-family: '微软雅黑',sans-serif;">因为</span> Redis <span style="font-family: '微软雅黑',sans-serif;">使用异步复制,</span> <span style="font-family: '微软雅黑',sans-serif;">所以主服务器发送的写数据并不一定会被从服务器接收到,</span> <span style="font-family: '微软雅黑',sans-serif;">因此,</span> <span style="font-family: '微软雅黑',sans-serif;">数据丢失的可能性仍然是存在的。</span>
</p>

<p style="text-indent: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif;">通过以下两个参数保证数据的安全:</span>
</p>

<div class="cnblogs_code">
  <pre>min-slaves-to-write &lt;number of slaves><span style="color: #000000;">

min-slaves-max-lag <number of seconds>

<h3>
  <span id="156_Redis">1.5.6 Redis<span style="font-family: '微软雅黑',sans-serif;">主从复制实践</span></span>
</h3>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">在安装</span>redis<span style="font-family: '微软雅黑',sans-serif;">时就进行了多实例的配置</span>
</p>

<div class="cnblogs_code">
  <pre><span style="color: #000000;">准备两个或两个以上redis实例

6380/redis-server 6380/redis.conf 6381/redis-server 6381/redis.conf 6382/redis-server 6382/redis.conf

<p>
  <span style="font-family: '微软雅黑',sans-serif;">配置文件示例:</span>
</p>

<div class="cnblogs_code">
  <pre>bind 127.0.0.1 10.0.0.186<span style="color: #000000;">

port 6380 daemonize yes pidfile /var/run/redis_6380.pid loglevel notice logfile "/var/log/redis_6380.log" dbfilename dump.rdb dir /application/redis/6380/ appendonly no appendfilename "appendonly.aof" appendfsync everysec slowlog-log-slower-than 10000 slowlog-max-len 128 protected-mode no

<p>
  <span style="font-family: '微软雅黑',sans-serif;">启动:</span>
</p>

<div class="cnblogs_code">
  <pre>./6380/redis-server ./6380/<span style="color: #000000;">redis.conf

./6381/redis-server ./6381/redis.conf ./6382/redis-server ./6382/redis.conf

<p>
  <span style="font-family: '微软雅黑',sans-serif;">复制环境说明</span>
</p>

<div class="cnblogs_code">
  <pre>主节点:6380<span style="color: #000000;">

从节点:6381、6382

<p>
  <span style="font-family: '微软雅黑',sans-serif;">开启主从(在</span>6381 6382<span style="font-family: '微软雅黑',sans-serif;">实例中执行)</span>
</p>

<div class="cnblogs_code">
  <pre>redis-cli -p 6381/6382<span style="color: #000000;">

SLAVEOF 127.0.0.1 6380

<p>
  &nbsp;&nbsp; <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; color: red; background: yellow;">至此</span><span style="color: red; background: yellow;">redis</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; color: red; background: yellow;">主从复制完成</span></strong>
</p>

<h3>
  <span id="157_Redis">1.5.7 Redis<span style="font-family: '微软雅黑',sans-serif;">主从复制管理</span></span>
</h3>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">主从复制状态监控:</span>info replication
</p>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">主从切换:</span> slaveof no one
</p>

<h2>
  <span id="16_Redis_HA_Redis_Sentinel"><span style="courier new"4courier new";font-weight: normal;">1.6 </span>Redis HA <span style="font-family: '微软雅黑',sans-serif;">实践</span><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; font-weight: normal;">(</span><span style="font-weight: normal;">Redis Sentinel</span><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";font-weight: normal;">)</span></span>
</h2>

<p style="margin-left: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">官方文档:</span><a href="https://redis.io/topics/sentinel">https://redis.io/topics/sentinel</a>
</p>

<p style="text-indent: 21.0pt;">
  Redis-Sentinel<span style="font-family: '微软雅黑',sans-serif;">是</span>Redis<span style="font-family: '微软雅黑',sans-serif;">官方推荐的高可用性</span>(HA)<span style="font-family: '微软雅黑',sans-serif;">解决方案,当用</span>Redis<span style="font-family: '微软雅黑',sans-serif;">做</span>Master-slave<span style="font-family: '微软雅黑',sans-serif;">的高可用方案时,假如</span>master<span style="font-family: '微软雅黑',sans-serif;">宕机了,</span>Redis<span style="font-family: '微软雅黑',sans-serif;">本身</span>(<span style="font-family: '微软雅黑',sans-serif;">包括它的很多客户端</span>)<span style="font-family: '微软雅黑',sans-serif;">都没有实现自动进行主备切换,而</span>Redis-sentinel<span style="font-family: '微软雅黑',sans-serif;">本身也是一个独立运行的进程,它能监控多个</span>master-slave<span style="font-family: '微软雅黑',sans-serif;">集群,发现</span>master<span style="font-family: '微软雅黑',sans-serif;">宕机后能进行自动切换。</span>
</p>

<p style="text-align: center;" align="center">
  &nbsp;<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203180958125-1195300728.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />
</p>

<p style="text-indent: 21.0pt;">
  Sentinel <span style="font-family: '微软雅黑',sans-serif;">是一个监视器,它可以根据被监视实例的身份和状态来判断应该执行何种动作。</span>
</p>

<h3>
  <span id="161_Redis_Sentinel">1.6.1 Redis Sentinel <span style="font-family: '微软雅黑',sans-serif;">功能</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">监控</span></strong><strong><span style="font-family: '微软雅黑',sans-serif;">(</span>Monitoring</strong><strong><span style="font-family: '微软雅黑',sans-serif;">):</span></strong>
</p>

<p>
  Sentinel <span style="font-family: '微软雅黑',sans-serif;">会不断地检查你的主服务器和从服务器是否运作正常。</span>
</p>

<p style="margin-left: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">提醒</span></strong><strong><span style="font-family: '微软雅黑',sans-serif;">(</span>Notification</strong><strong><span style="font-family: '微软雅黑',sans-serif;">):</span></strong>
</p>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">当被监控的某个</span> Redis <span style="font-family: '微软雅黑',sans-serif;">服务器出现问题时,</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">可以通过</span> API <span style="font-family: '微软雅黑',sans-serif;">向管理员或者其他应用程序发送通知。</span>
</p>

<p style="margin-left: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">自动故障迁移</span></strong><strong><span style="font-family: '微软雅黑',sans-serif;">(</span>Automatic failover</strong><strong><span style="font-family: '微软雅黑',sans-serif;">):</span></strong>
</p>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">当一个主服务器不能正常工作时,</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">会开始一次自动故障迁移操作,</span> <span style="font-family: '微软雅黑',sans-serif;">它会将失效主服务器的其中一个从服务器升级为新的主服务器,</span> <span style="font-family: '微软雅黑',sans-serif;">并让失效主服务器的其他从服务器改为复制新的主服务器;</span> <span style="font-family: '微软雅黑',sans-serif;">当客户端试图连接失效的主服务器时,</span> <span style="font-family: '微软雅黑',sans-serif;">集群也会向客户端返回新主服务器的地址,</span> <span style="font-family: '微软雅黑',sans-serif;">使得集群可以使用新主服务器代替失效服务器。</span>
</p>

<h3>
  <span id="162">1.6.2 <span style="font-family: '微软雅黑',sans-serif;">服务器连接</span></span>
</h3>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">发现并连接主服务器</span></strong>
</p>

<p>
  Sentinel <span style="font-family: '微软雅黑',sans-serif;">通过用户给定的配置文件来发现主服务器。</span>
</p>

<p style="text-align: center;" align="center">
  <img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203181008109-1757083206.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />
</p>

<p>
  Sentinel <span style="font-family: '微软雅黑',sans-serif;">会与被监视的主服务器创建</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">两个网络连接:</span>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span><span style="font-family: 'MS Gothic';">&zwj;</span><span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">命令连接用于向主服务器发送命令。</span>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span><span style="font-family: 'MS Gothic';">&zwj;</span><span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <span style="font-family: '微软雅黑',sans-serif;">订阅连接用于订阅指定的频道,从而发现</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">监视同一主服务器的其他</span> Sentinel<span style="font-family: '微软雅黑',sans-serif;">。</span>
</p>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">发现并连接从服务器</span></strong>
</p>

<p>
  &nbsp;&nbsp; Sentinel <span style="font-family: '微软雅黑',sans-serif;">通过向主服务器发送</span> INFO <span style="font-family: '微软雅黑',sans-serif;">命令来自动获得所有从服务器的地址。</span>
</p>

<p>
  &nbsp;&nbsp; <span style="font-family: '微软雅黑',sans-serif;">跟主服务器一样,</span>Sentinel <span style="font-family: '微软雅黑',sans-serif;">会与每个被发现的从服务器创建命令连接和订阅连接。</span>
</p>

<p style="text-align: center;" align="center">
  &nbsp;<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203181018843-233905319.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />
</p>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">发现其他</span><span style="background: lime;"> Sentinel</span></strong>
</p>

<p>
  &nbsp;&nbsp; Sentinel <span style="font-family: '微软雅黑',sans-serif;">会通过命令连接向被监视的主从服务器发送</span> <span style="font-family: '微软雅黑',sans-serif;">&ldquo;</span>HELLO<span style="font-family: '微软雅黑',sans-serif;">&rdquo;</span> <span style="font-family: '微软雅黑',sans-serif;">信息,该消息包含</span>Sentinel <span style="font-family: '微软雅黑',sans-serif;">的</span> IP<span style="font-family: '微软雅黑',sans-serif;">、端口号、</span>ID <span style="font-family: '微软雅黑',sans-serif;">等内容,以此来向其他</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">宣告自己的存在。与此同时</span>Sentinel <span style="font-family: '微软雅黑',sans-serif;">会通过订阅连接接收其他</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">的&ldquo;</span>HELLO<span style="font-family: '微软雅黑',sans-serif;">&rdquo;</span> <span style="font-family: '微软雅黑',sans-serif;">信息,以此来发现监视同一个主服务器的其他</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">。</span>
</p>

<p style="text-align: center;" align="center">
  &nbsp;<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203181027140-762797243.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />
</p>

<p style="text-indent: 21.0pt;">
  sentinel1 <span style="font-family: '微软雅黑',sans-serif;">通过发送</span>HELLO <span style="font-family: '微软雅黑',sans-serif;">信息来让</span>sentinel2 <span style="font-family: '微软雅黑',sans-serif;">和</span> sentinel3<span style="font-family: '微软雅黑',sans-serif;">发现自己,其他两个</span>sentinel <span style="font-family: '微软雅黑',sans-serif;">也会进行类似的操作。</span>
</p>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">多个</span><span style="background: lime;">Sentienl</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; background: lime;">之间的链接</span></strong>
</p>

<p>
  &nbsp;&nbsp; Sentinel <span style="font-family: '微软雅黑',sans-serif;">之间只会互相创建命令连接,用于进行通信。因为已经有主从服务器作为发送和接收</span> HELLO <span style="font-family: '微软雅黑',sans-serif;">信息的中介,所以</span> Sentinel<span style="font-family: '微软雅黑',sans-serif;">之间不会创建订阅连接。</span>
</p>

<p style="text-align: center;" align="center">
  <img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203181035312-1532457764.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />&nbsp;
</p>

<h3>
  <span id="163">1.6.3 <span style="font-family: '微软雅黑',sans-serif;">检测实例的状态</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  Sentinel <span style="font-family: '微软雅黑',sans-serif;">使用</span> PING <span style="font-family: '微软雅黑',sans-serif;">命令来检测实例的状态:如果实例在指定的时间内没有返回回复,或者返回错误的回复,那么该实例会被</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">判断为下线。</span>
</p>

<p style="text-align: center;" align="center">
  &nbsp;<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203181045578-395395120.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />
</p>

<p>
  Redis <span style="font-family: '微软雅黑',sans-serif;">的</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">中关于下线(</span>down<span style="font-family: '微软雅黑',sans-serif;">)有两个不同的概念:</span>
</p>

<p style="margin-left: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span> <em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">主观下线(</span>Subjectively Down</span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">,</span> </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">简称</span> SDOWN</span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">)</span></span></em><span style="font-family: '微软雅黑',sans-serif;">指的是单个</span>Sentinel <span style="font-family: '微软雅黑',sans-serif;">实例对服务器做出的下线判断。</span>
</p>

<p style="margin-left: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">?</span><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">客观下线(</span>Objectively Down</span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">,</span> </span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">简称</span> ODOWN</span></em><em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">)</span></span></em><span style="font-family: '微软雅黑',sans-serif;">指的是多个</span>Sentinel <span style="font-family: '微软雅黑',sans-serif;">实例在对同一个服务器做出</span> SDOWN <span style="font-family: '微软雅黑',sans-serif;">判断,</span> <span style="font-family: '微软雅黑',sans-serif;">并且通过</span> SENTINEL is-master-down-by-addr <span style="font-family: '微软雅黑',sans-serif;">命令互相交流之后,</span> <span style="font-family: '微软雅黑',sans-serif;">得出的服务器下线判断。</span> <span style="font-family: '微软雅黑',sans-serif;">(一个</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">可以通过向另一个</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">发送</span> SENTINEL is-master-down-by-addr <span style="font-family: '微软雅黑',sans-serif;">命令来询问对方是否认为给定的服务器已下线。)</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">如果一个服务器没有在</span> master-down-after-milliseconds <span style="font-family: '微软雅黑',sans-serif;">选项所指定的时间内,</span> <span style="font-family: '微软雅黑',sans-serif;">对向它发送</span> PING <span style="font-family: '微软雅黑',sans-serif;">命令的</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">返回一个有效回复(</span>valid reply<span style="font-family: '微软雅黑',sans-serif;">),</span> <span style="font-family: '微软雅黑',sans-serif;">那么</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">就会将这个服务器标记为主观下线。</span>
</p>

<h3>
  <span id="164_FAILOVER">1.6.4 <span style="font-family: '微软雅黑',sans-serif;">故障转移</span>FAILOVER</span>
</h3>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">一次故障转移操作由以下步骤组成:</span>
</p>

<blockquote>
  <p>
    1.&nbsp;&nbsp;&nbsp;发现主服务器已经进入客观下线状态。
  </p>
  
  <p>
    2.&nbsp;&nbsp;&nbsp;基于Raft leader election&nbsp;协议&nbsp;,&nbsp;进行投票选举
  </p>
  
  <p>
    3.&nbsp;&nbsp;&nbsp;如果当选失败,那么在设定的故障迁移超时时间的两倍之后,重新尝试当选。&nbsp;如果当选成功,&nbsp;那么执行以下步骤。
  </p>
  
  <p>
    4.&nbsp;&nbsp;&nbsp;选出一个从服务器,并将它升级为主服务器。
  </p>
  
  <p>
    5.&nbsp;&nbsp;&nbsp;向被选中的从服务器发送&nbsp;SLAVEOF NO ONE&nbsp;命令,让它转变为主服务器。
  </p>
  
  <p>
    6.&nbsp;&nbsp;&nbsp;通过发布与订阅功能,&nbsp;将更新后的配置传播给所有其他&nbsp;Sentinel&nbsp;,其他&nbsp;Sentinel&nbsp;对它们自己的配置进行更新。
  </p>
  
  <p>
    7.&nbsp;&nbsp;&nbsp;向已下线主服务器的从服务器发送&nbsp;SLAVEOF&nbsp;命令,让它们去复制新的主服务器。
  </p>
  
  <p>
    8.&nbsp;&nbsp;&nbsp;当所有从服务器都已经开始复制新的主服务器时,&nbsp;leader Sentinel&nbsp;终止这次故障迁移操作。
  </p>
</blockquote>

<h3>
  <span id="165_sentinel">1.6.5 <span style="font-family: '微软雅黑',sans-serif;">配置</span>sentinel</span>
</h3>

<p style="margin-left: 7.1pt;">
  <span style="font-family: '微软雅黑',sans-serif;">创建程序目录</span>
</p>

<div class="cnblogs_code">
  <pre>cd /<span style="color: #000000;">application

mkdir 26380 cp /usr/local/redis/src/redis-sentinel ./26380/ cd 26380

<p>
  &nbsp;&nbsp; <span style="font-family: '微软雅黑',sans-serif;">编辑配置文件</span>
</p>

<div class="cnblogs_code">
  <pre><span style="color: #000000;">vim sentinel.conf

port 26380 dir "/tmp" sentinel monitor mymaster 127.0.0.1 6380 2 sentinel down-after-milliseconds mymaster 60000 sentinel config-epoch mymaster 0

<p>
  <span style="font-family: '微软雅黑',sans-serif;">启动</span>sentinel
</p>

<div class="cnblogs_code">
  <pre>./redis-sentinel ./sentinel.conf</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">配置文件说明</span>
</p>

<div class="cnblogs_code">
  <pre><span style="color: #008000;">#</span><span style="color: #008000;"> 指定监控master</span>

sentinel monitor mymaster 127.0.0.1 6370 2 # {2表示多少个sentinel同意} # 安全信息 sentinel auth-pass mymaster root # 超过15000毫秒后认为主机宕机 sentinel down-after-milliseconds mymaster 15000 # 当主从切换多久后认为主从切换失败 sentinel failover-timeout mymaster 900000 # 这两个配置后面的数量主从机需要一样,epoch为master的版本 sentinel leader-epoch mymaster 1 sentinel config-epoch mymaster 1

<h3>
  <span id="166_Sentinel">1.6.6 Sentinel<span style="font-family: '微软雅黑',sans-serif;">命令操作</span></span>
</h3>

<table style="width: 100%; border-collapse: collapse; border-width: initial; border-style: none; border-color: initial;" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td style="width: 50%; border-top: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: 1pt solid #9bbb59; border-right: none; background: #9bbb59; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">命令</span></strong>
      </p>
    </td>
    
    <td style="width: 50%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">描述</span></strong>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>PING</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回</span> PONG
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SENTINEL masters</strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">列出所有被监视的主服务器</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SENTINEL slaves <master name></strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        &nbsp;
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SENTINEL get-master-addr-by-name <master name></strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">返回给定名字的主服务器的</span> IP <span style="font-family: '微软雅黑',sans-serif;">地址和端口号。</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; background: #eaf1dd; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SENTINEL reset <pattern></strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">重置所有名字和给定模式</span> pattern <span style="font-family: '微软雅黑',sans-serif;">相匹配的主服务器</span>
      </p>
    </td>
  </tr>
  
  <tr>
    <td style="width: 50%; border-right: 1pt solid #c2d69b; border-bottom: 1pt solid #c2d69b; border-left: 1pt solid #c2d69b; border-top: none; padding: 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <strong>SENTINEL failover <master name></strong>
      </p>
    </td>
    
    <td style="width: 50.0%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="50%">
      <p style="text-align: justify; text-justify: inter-ideograph;">
        <span style="font-family: '微软雅黑',sans-serif;">当主服务器失效时,</span> <span style="font-family: '微软雅黑',sans-serif;">在不询问其他</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">意见的情况下,</span> <span style="font-family: '微软雅黑',sans-serif;">强制开始一次自动故障迁移。</span>
      </p>
    </td>
  </tr>
</table>

<h3>
  <span id="167_Sentinel">1.6.7 Sentinel<span style="font-family: '微软雅黑',sans-serif;">发布与订阅信息</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">客户端可以将</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">看作是一个只提供了订阅功能的</span> Redis <span style="font-family: '微软雅黑',sans-serif;">服务器:</span> <span style="font-family: '微软雅黑',sans-serif;">你不可以使用</span> <em>PUBLISH </em><span style="font-family: '微软雅黑',sans-serif;">命令向这个服务器发送信息,</span> <span style="font-family: '微软雅黑',sans-serif;">但你可以用</span> <em>SUBSCRIBE</em> <span style="font-family: '微软雅黑',sans-serif;">命令或者</span> <em>PSUBSCRIBE </em><span style="font-family: '微软雅黑',sans-serif;">命令,</span> <span style="font-family: '微软雅黑',sans-serif;">通过订阅给定的频道来获取相应的事件提醒。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">一个频道能够接收和这个频道的名字相同的事件。</span> <span style="font-family: '微软雅黑',sans-serif;">比如说,</span> <span style="font-family: '微软雅黑',sans-serif;">名为</span><em> +sdown</em> <span style="font-family: '微软雅黑',sans-serif;">的频道就可以接收所有实例进入主观下线(</span><em>SDOWN</em><span style="font-family: '微软雅黑',sans-serif;">)状态的事件。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">通过执行</span> PSUBSCRIBE * <span style="font-family: '微软雅黑',sans-serif;">命令可以接收所有事件信息。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">以下列出的是客户端可以通过订阅来获得的频道和信息的格式:</span>
</p>

<blockquote>
  <p style="text-indent: 21.0pt;">
    <span style="font-family: '微软雅黑',sans-serif;"><span style="font-size: 12.0pt; mso-bidi-font-size: 11.0pt; font-family: 'Courier New'; mso-fareast-font-family: 微软雅黑; mso-bidi-font-family: 'Times New Roman'; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">&nbsp;</span><span style="font-size: 12pt;">第一个英文单词是频道</span><span style="font-size: 12.0pt; mso-bidi-font-size: 11.0pt; font-family: 'Courier New'; mso-fareast-font-family: 微软雅黑; mso-bidi-font-family: 'Times New Roman'; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;" lang="EN-US">/</span><span style="font-size: 12pt;">事件的名字,</span><span style="font-size: 12pt;">其余的是数据的格式。</span></span>
  </p>
</blockquote>

<p style="text-indent: 21.0pt;">
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: red;">注意,</span></strong> <span style="font-family: '微软雅黑',sans-serif;">当格式中包含</span> instance details <span style="font-family: '微软雅黑',sans-serif;">字样时,</span> <span style="font-family: '微软雅黑',sans-serif;">表示频道所返回的信息中包</span>
</p>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">含了以下用于识别目标实例的内容:</span>
</p>

<div class="cnblogs_code">
  <pre>&lt;instance-type> &lt;name> &lt;ip> &lt;port> @ &lt;master-name> &lt;master-ip> &lt;master-port><span style="color: #000000;">

@ 字符之后的内容用于指定主服务器, 这些内容是可选的, 它们仅在 @ 字符之前的内容指定的实例不是主服务器时使用。

<h2>
  <span id="17_Redis_cluster">1.7 Redis cluster</span>
</h2>

<h3>
  <span id="171_Redis">1.7.1 Redis<span style="font-family: '微软雅黑',sans-serif;">集群</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  Redis <span style="font-family: '微软雅黑',sans-serif;">集群是一个可以在多个</span> Redis <span style="font-family: '微软雅黑',sans-serif;">节点之间进行<em><span style="text-decoration: underline;">数据共享</span></em>的设施(</span>installation<span style="font-family: '微软雅黑',sans-serif;">)。</span>
</p>

<p style="text-indent: 21.0pt;">
  Redis <span style="font-family: '微软雅黑',sans-serif;">集群不支持那些需要同时处理多个键的</span> Redis <span style="font-family: '微软雅黑',sans-serif;">命令,</span> <span style="font-family: '微软雅黑',sans-serif;">因为执行这些命令需要在多个</span> Redis <span style="font-family: '微软雅黑',sans-serif;">节点之间移动数据,</span> <span style="font-family: '微软雅黑',sans-serif;">并且在高负载的情况下,这些命令将降低</span> Redis <span style="font-family: '微软雅黑',sans-serif;">集群的性能,</span> <span style="font-family: '微软雅黑',sans-serif;">并导致不可预测的行为。</span>
</p>

<p style="text-indent: 21.0pt;">
  Redis <span style="font-family: '微软雅黑',sans-serif;">集群通过分区(</span>partition<span style="font-family: '微软雅黑',sans-serif;">)来提供一定程度的可用性(</span>availability<span style="font-family: '微软雅黑',sans-serif;">):</span> <span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">即使集群中有一部分节点失效或者无法进行通讯,集群也可以继续处理命令请求。</span></span><span style="font-family: '微软雅黑',sans-serif;">将数据自动切分(</span>split<span style="font-family: '微软雅黑',sans-serif;">)到多个节点的能力。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">当集群中的一部分节点失效或者无法进行通讯时,</span> <span style="font-family: '微软雅黑',sans-serif;">仍然可以继续处理命令请求的能力。</span>
</p>

<h3>
  <span id="172_Redis">1.7.2 Redis <span style="font-family: '微软雅黑',sans-serif;">集群数据共享</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  Redis <span style="font-family: '微软雅黑',sans-serif;">集群使用数据分片(</span>sharding<span style="font-family: '微软雅黑',sans-serif;">)而非一致性哈希(</span>consistency hashing<span style="font-family: '微软雅黑',sans-serif;">)来实现:</span> <span style="font-family: '微软雅黑',sans-serif;">一个</span> Redis <span style="font-family: '微软雅黑',sans-serif;">集群包含</span> 16384 <span style="font-family: '微软雅黑',sans-serif;">个哈希槽(</span>hash slot<span style="font-family: '微软雅黑',sans-serif;">),数据库中的每个键都属于这</span> 16384 <span style="font-family: '微软雅黑',sans-serif;">个哈希槽的其中一个,</span> <span style="font-family: '微软雅黑',sans-serif;">集群使用公式</span>CRC16(key) % 16384 <span style="font-family: '微软雅黑',sans-serif;">来计算键</span> key <span style="font-family: '微软雅黑',sans-serif;">属于哪个槽,</span> <span style="font-family: '微软雅黑',sans-serif;">其中</span> CRC16(key) <span style="font-family: '微软雅黑',sans-serif;">语句用于计算键</span> key <span style="font-family: '微软雅黑',sans-serif;">的</span> CRC16 <span style="font-family: '微软雅黑',sans-serif;">校验和</span> <span style="font-family: '微软雅黑',sans-serif;">。</span>
</p>

<blockquote>
  <p style="text-indent: 21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">
    <span style="font-family: '微软雅黑',sans-serif;">节点</span>&nbsp;A&nbsp;<span style="font-family: '微软雅黑',sans-serif;">负责处理</span>&nbsp;0&nbsp;<span style="font-family: '微软雅黑',sans-serif;">号至</span>&nbsp;5500&nbsp;<span style="font-family: '微软雅黑',sans-serif;">号哈希槽。</span>
  </p>
  
  <p style="text-indent: 21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">
    <span style="font-family: '微软雅黑',sans-serif;">节点</span>&nbsp;B&nbsp;<span style="font-family: '微软雅黑',sans-serif;">负责处理</span>&nbsp;5501&nbsp;<span style="font-family: '微软雅黑',sans-serif;">号至</span>&nbsp;11000&nbsp;<span style="font-family: '微软雅黑',sans-serif;">号哈希槽。</span>
  </p>
  
  <p style="text-indent: 21.0pt;">
    &nbsp;
  </p>
  
  <p style="text-indent: 21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">
    <span style="font-family: '微软雅黑',sans-serif;">节点</span>&nbsp;C&nbsp;<span style="font-family: '微软雅黑',sans-serif;">负责处理</span>&nbsp;11001&nbsp;<span style="font-family: '微软雅黑',sans-serif;">号至</span>&nbsp;16384&nbsp;<span style="font-family: '微软雅黑',sans-serif;">号哈希槽。</span>
  </p>
</blockquote>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">槽的计算公式</span></strong>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">集群使用公式</span> CRC16(key) & 16383 <span style="font-family: '微软雅黑',sans-serif;">计算键</span> key<span style="font-family: '微软雅黑',sans-serif;">属于哪个槽。</span>
</p>

<p style="text-align: center;" align="center">
  <img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203181101453-1152417651.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />&nbsp;
</p>

<h3>
  <span id="173">1.7.3 <span style="font-family: '微软雅黑',sans-serif;">集群运行机制</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">所有的</span>redis<span style="font-family: '微软雅黑',sans-serif;">节点彼此互联</span>(PING-PONG<span style="font-family: '微软雅黑',sans-serif;">机制</span>),<span style="font-family: '微软雅黑',sans-serif;">内部使用二进制协议优化传输速度和带宽</span>.
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">节点的</span>fail<span style="font-family: '微软雅黑',sans-serif;">是通过集群中超过半数的</span>master<span style="font-family: '微软雅黑',sans-serif;">节点检测失效时才失效。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">客户端与</span>redis<span style="font-family: '微软雅黑',sans-serif;">节点直连</span>,<span style="font-family: '微软雅黑',sans-serif;">不需要中间</span>proxy<span style="font-family: '微软雅黑',sans-serif;">层</span>.<span style="font-family: '微软雅黑',sans-serif;">客户端不需要连接集群所有节点</span>,<span style="font-family: '微软雅黑',sans-serif;">连接集群中任何一个可用节点即可</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">把所有的物理节点映射到</span>[0-16383]slot<span style="font-family: '微软雅黑',sans-serif;">上</span>,cluster <span style="font-family: '微软雅黑',sans-serif;">负责维护</span>node<->slot<->key
</p>

<p style="text-align: center;" align="center">
  &nbsp;<img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203181112250-212579303.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">为了使得集群在一部分节点下线或者无法与集群的大多数(</span>majority<span style="font-family: '微软雅黑',sans-serif;">)节点进行通讯的情况下,</span> <span style="font-family: '微软雅黑',sans-serif;">仍然可以正常运作,</span> Redis <span style="font-family: '微软雅黑',sans-serif;">集群对节点使用了主从复制功能:</span> <span style="font-family: '微软雅黑',sans-serif;">集群中的每个节点都有</span> 1 <span style="font-family: '微软雅黑',sans-serif;">个至</span> N <span style="font-family: '微软雅黑',sans-serif;">个复制品(</span>replica<span style="font-family: '微软雅黑',sans-serif;">),</span> <span style="font-family: '微软雅黑',sans-serif;">其中一个复制品为主节点(</span>master<span style="font-family: '微软雅黑',sans-serif;">),</span> <span style="font-family: '微软雅黑',sans-serif;">而其余的</span> N-1 <span style="font-family: '微软雅黑',sans-serif;">个复制品为从节点(</span>slave<span style="font-family: '微软雅黑',sans-serif;">)。</span>
</p>

<blockquote>
  <p style="text-indent: 21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">
    <span style="font-family: '微软雅黑',sans-serif;">在之前列举的节点</span>&nbsp;A&nbsp;<span style="font-family: '微软雅黑',sans-serif;">、</span>B&nbsp;<span style="font-family: '微软雅黑',sans-serif;">、</span>C&nbsp;<span style="font-family: '微软雅黑',sans-serif;">的例子中,</span>&nbsp;<span style="font-family: '微软雅黑',sans-serif;">如果节点</span>&nbsp;B&nbsp;<span style="font-family: '微软雅黑',sans-serif;">下线了,</span>&nbsp;<span style="font-family: '微软雅黑',sans-serif;">那么集群将无法正常运行,</span>&nbsp;<span style="font-family: '微软雅黑',sans-serif;">因为集群找不到节点来处理</span>&nbsp;5501&nbsp;<span style="font-family: '微软雅黑',sans-serif;">号至</span>&nbsp;11000&nbsp;&nbsp;<span style="font-family: '微软雅黑',sans-serif;">号的哈希</span>
  </p>
  
  <p style="color: #000000;">
    <span style="font-family: '微软雅黑',sans-serif;">槽。</span>
  </p>
  
  <p style="text-indent: 21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">
    <span style="font-family: '微软雅黑',sans-serif;">假如在创建集群的时候(或者至少在节点</span>&nbsp;B&nbsp;<span style="font-family: '微软雅黑',sans-serif;">下线之前),</span>&nbsp;<span style="font-family: '微软雅黑',sans-serif;">我们为主节点</span>&nbsp; B<span style="font-family: '微软雅黑',sans-serif;">添加了从节点</span>&nbsp;B1&nbsp;<span style="font-family: '微软雅黑',sans-serif;">,</span>&nbsp;<span style="font-family: '微软雅黑',sans-serif;">那么当主节点</span>&nbsp;B&nbsp;<span style="font-family: '微软雅黑',sans-serif;">下线的时候,</span>&nbsp;<span style="font-family: '微软雅黑',sans-serif;">集群就会将</span>&nbsp;B1&nbsp;&nbsp;<span style="font-family: '微软雅黑',sans-serif;">设置为新的主节点,</span>&nbsp;<span style="font-family: '微软雅黑',sans-serif;">并让它代替下线的主节点</span>&nbsp;B&nbsp;<span style="font-family: '微软雅黑',sans-serif;">,</span>&nbsp;<span style="font-family: '微软雅黑',sans-serif;">继续处理</span>&nbsp;5501&nbsp;<span style="font-family: '微软雅黑',sans-serif;">号至</span>&nbsp; 11000&nbsp;<span style="font-family: '微软雅黑',sans-serif;">号的哈希槽,</span>&nbsp;<span style="font-family: '微软雅黑',sans-serif;">这样集群就不会因为主节点</span>&nbsp;B&nbsp;&nbsp;<span style="font-family: '微软雅黑',sans-serif;">的下线而无法正常运作了。</span>
  </p>
  
  <p style="text-indent: 21.0pt;">
    &nbsp;
  </p>
  
  <p style="text-indent: 21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">
    <span style="font-family: '微软雅黑',sans-serif;">不过如果节点</span>&nbsp;B&nbsp;<span style="font-family: '微软雅黑',sans-serif;">和</span>&nbsp;B1&nbsp;<span style="font-family: '微软雅黑',sans-serif;">都下线的话,</span>&nbsp;Redis&nbsp;<span style="font-family: '微软雅黑',sans-serif;">集群还是会停止运作。</span>
  </p>
</blockquote>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">集群的复制特性重用了</span> SLAVEOF <span style="font-family: '微软雅黑',sans-serif;">命令的代码,所以集群节点的复制行为和</span>SLAVEOF <span style="font-family: '微软雅黑',sans-serif;">命令的复制行为完全相同。</span>
</p>

<h3>
  <span id="174">1.7.4 <span style="font-family: '微软雅黑',sans-serif;">集群的故障转移</span></span>
</h3>

<p style="margin-left: 42.0pt; text-indent: -21.0pt;">
  1.&nbsp;<span style="font-family: '微软雅黑',sans-serif;">在集群里面,节点会对其他节点进行下线检测。</span>
</p>

<p style="margin-left: 42.0pt; text-indent: -21.0pt;">
  2.&nbsp;<span style="font-family: '微软雅黑',sans-serif;">当一个主节点下线时,集群里面的其他主节点负责对下线主节点进行故障移。</span>
</p>

<p style="margin-left: 42.0pt; text-indent: -21.0pt;">
  3.&nbsp;<span style="font-family: '微软雅黑',sans-serif;">换句话说,集群的节点集成了下线检测和故障转移等类似</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">的功能。</span>
</p>

<p style="margin-left: 42.0pt; text-indent: -21.0pt;">
  4.&nbsp;<span style="font-family: '微软雅黑',sans-serif;">因为</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">是一个独立运行的监控程序,而集群的下线检测和故障转移等功能是集成在节点里面的,它们的运行模式非常地不同,所以尽管这两者的功能很相似,但集群的实现没有重用</span> Sentinel <span style="font-family: '微软雅黑',sans-serif;">的代码。</span>
</p>

<p>
  <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";background: lime;">在集群里面执行命令的两种情况</span></strong>
</p>

<p style="text-indent: 21.0pt;">
  <em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">命令发送到了正确的节点:</span></span></em>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">命令要处理的键所在的槽正好是由接收命令的节点负责,那么该节点执行命令,就像单机</span> Redis <span style="font-family: '微软雅黑',sans-serif;">服务器一样。</span>
</p>

<p style="text-align: center;" align="center">
  <img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203181122062-428980842.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />&nbsp;
</p>

<blockquote>
  <p style="color: #000000;">
    <span style="font-family: '微软雅黑',sans-serif;">槽位说明:</span>
  </p>
  
  <p style="color: #000000;">
    7000:&nbsp;<span style="font-family: '微软雅黑',sans-serif;">槽</span>&nbsp;0~5000
  </p>
  
  <p style="color: #000000;">
    7001<span style="font-family: '微软雅黑',sans-serif;">:槽</span>&nbsp;5001~10000
  </p>
  
  <p style="text-align: center;" align="center">
    &nbsp;
  </p>
  
  <p style="color: #000000;">
    7002<span style="font-family: '微软雅黑',sans-serif;">:槽</span>&nbsp;10001~16383
  </p>
</blockquote>

<div class="cnblogs_code">
  <pre>键 date 位于 2022 槽,该槽由节点 7000 负责,命令会直接执行。</pre>
</div>

<p style="text-indent: 21.0pt;">
  <em><span style="text-decoration: underline;"><span style="font-family: '微软雅黑',sans-serif;">命令发送到了错误的节点</span></span></em><span style="font-family: '微软雅黑',sans-serif;">:</span>
</p>

<p style="margin-left: 21.0pt; text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">接收到命令的节点并非处理键所在槽的节点,那么节点将向客户端返回一个转向(</span>redirection<span style="font-family: '微软雅黑',sans-serif;">)错误,告知客户端应该到哪个节点去执行这个命令,客户端会根据错误提示的信息,重新向正确的节点发送命令。</span>
</p>

<p style="text-align: center;" align="center">
  <img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203181132062-1713171897.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />
</p>

<div class="cnblogs_code">
  <pre>键 date 位于 2022 槽,该槽由节点 7000 负责,但错误发送到了7001节点,7001向客户返回转向错误。</pre>
</div>

<p style="text-align: center;" align="center">
  <img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203181138968-1262286598.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />
</p>

<div class="cnblogs_code">
  <pre>客户端根据转向错误的指引,转向到节点7000,并重新发送命令</pre>
</div>

<h3>
  <span id="175">1.7.5 <span style="font-family: '微软雅黑',sans-serif;">关于转向错误</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">在集群中的节点会互相告知对方,自己负责处理哪些槽。</span>
</p>

<p style="text-align: center;" align="center">
  <img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203181147953-1285314260.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />
</p>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">集群中的每个节点都会记录</span> 16384 <span style="font-family: '微软雅黑',sans-serif;">个槽分别由哪个节点负责,从而形成一个&ldquo;槽表&rdquo;(</span>slot table<span style="font-family: '微软雅黑',sans-serif;">)。</span>
</p>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">节点在接收到命令请求时,会通过槽表检查键所在的槽是否由本节点处理:</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">✍</span> <span style="font-family: '微软雅黑',sans-serif;">如果是的话,那么节点直接执行命令;</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: 'Segoe UI Emoji',sans-serif;">✍</span> <span style="font-family: '微软雅黑',sans-serif;">如果不是的话,那么节点就从槽表里面提取出正确节点的地址信息,然后返回转向错误。</span>
</p>

<p style="text-align: center;" align="center">
  <img data-original="https://clsn.io/wp-content/uploads/2018/03/1190037-20180203181156921-293783345.png" src="/wp-content/themes/clsn-003/img/blank.gif" alt="Redis 数据库" alt="" />&nbsp;
</p>

<h3>
  <span id="176">1.7.6 <span style="font-family: '微软雅黑',sans-serif;">配置集群</span></span>
</h3>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">前期准备</span>
</p>

<p>
  # EPEL<span style="font-family: '微软雅黑',sans-serif;">源安装</span>ruby<span style="font-family: '微软雅黑',sans-serif;">支持</span>
</p>

<div class="cnblogs_code">
  <pre>yum install ruby rubygems -y</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">使用国内源</span>
</p>

<div class="cnblogs_code">
  <pre>gem source -a http://mirrors.aliyun.com/rubygems/  -remove https://rubygems.org/

# gem sources –add https://gems.ruby-china.org/ –remove https://rubygems.org/ # 安装redis支持 gem install redis -v 3.3.3 gem sources -l

<p>
  <span style="font-family: '微软雅黑',sans-serif;">配置文件</span>
</p>

<p style="text-indent: 21.0pt;">
  Redis <span style="font-family: '微软雅黑',sans-serif;">集群由多个运行在集群模式(</span>cluster mode<span style="font-family: '微软雅黑',sans-serif;">)下的</span> Redis <span style="font-family: '微软雅黑',sans-serif;">实例组成,</span> <span style="font-family: '微软雅黑',sans-serif;">实例的集群模式需要通过配置来开启,</span> <span style="font-family: '微软雅黑',sans-serif;">开启集群模式的实例将可以使用集群特有的功能和命令。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">以下是一个包含了最少选项的集群配置文件示例:</span>
</p>

<p style="text-indent: 21.0pt;">
  &nbsp;
</p>

<div class="cnblogs_code">
  <pre>port 7000<span style="color: #000000;">

cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes

<p>
  <span style="font-family: '微软雅黑',sans-serif;">创建程序目录</span>
</p>

<div class="cnblogs_code">
  <pre>cd /application/<span style="color: #000000;">redis

mkdir 7000 7001 7002 7003 7004 7005

<p>
  <span style="font-family: '微软雅黑',sans-serif;">拷贝应用</span>
</p>

<div class="cnblogs_code">
  <pre><span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> 0 1 2 3 4 5<span style="color: #000000;"> 
do
cp </span>/usr/local/redis/src/redis-server  ./700<span style="color: #000000;">$i

done

<p>
  <span style="font-family: '微软雅黑',sans-serif;">创建配置文件</span>
</p>

<div class="cnblogs_code">
  <pre><span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> 7000 7001 7002 7003 7004 7005<span style="color: #000000;">

do echo "port $i cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes" > $i/redis.conf done

<p>
  <span style="font-family: '微软雅黑',sans-serif;">启动</span>redis<span style="font-family: '微软雅黑',sans-serif;">集群</span>
</p>

<div class="cnblogs_code">
  <pre><span style="color: #0000ff;">for</span> i <span style="color: #0000ff;">in</span> 7000 7001 7002 7003 7004 7005<span style="color: #000000;">

do cd $i ./redis-server ./redis.conf & cd ../ done

<p>
  <span style="font-family: '微软雅黑',sans-serif;">创建集群</span>
</p>

<div class="cnblogs_code">
  <pre>cd /usr/local/redis/src/<span style="color: #000000;">

./redis-trib.rb –replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

<p style="text-indent: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif;">给定</span> redis-trib.rb <span style="font-family: '微软雅黑',sans-serif;">程序的命令是</span> create <span style="font-family: '微软雅黑',sans-serif;">,</span> <span style="font-family: '微软雅黑',sans-serif;">这表示我们希望创建一个新的集群。</span>
</p>

<p style="text-indent: 15.75pt;">
  <span style="font-family: '微软雅黑',sans-serif;">选项</span> --replicas 1 <span style="font-family: '微软雅黑',sans-serif;">表示我们希望为集群中的每个主节点创建一个从节点。</span>
</p>

<h3>
  <span id="177">1.7.7 <span style="font-family: '微软雅黑',sans-serif;">集群管理</span></span>
</h3>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">写数据,查看集群状态</span>
</p>

<div class="cnblogs_code">
  <pre>redis-cli -c -p 7000<span style="color: #000000;">

set foo bar get foo

<p>
  <span style="font-family: '微软雅黑',sans-serif;">重新分片实践</span>
</p>

<div class="cnblogs_code">
  <pre>cd /usr/local/redis/src/<span style="color: #000000;">

./redis-trib.rb reshard 127.0.0.1:7000

<p>
  <span style="font-family: '微软雅黑',sans-serif;">集群状态</span>
</p>

<div class="cnblogs_code">
  <pre>redis-cli -p 7000 cluster nodes | grep master</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">故障转移</span>
</p>

<div class="cnblogs_code">
  <pre>redis-cli -p 7002 debug segfault</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">查看状态</span>
</p>

<div class="cnblogs_code">
  <pre>redis-cli -p 7000 cluster nodes | grep master</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">增加新的节点</span>
</p>

<div class="cnblogs_code">
  <pre>./redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000</pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">删除一个节点</span>
</p>

<div class="cnblogs_code">
  <pre>redis-trib <span style="color: #0000ff;">del</span>-node ip:port <span style="color: #800000;">'</span><span style="color: #800000;">&lt;node-id></span><span style="color: #800000;">'</span></pre>
</div>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">删除</span>master<span style="font-family: '微软雅黑',sans-serif;">节点之前首先要使用</span>reshard<span style="font-family: '微软雅黑',sans-serif;">移除</span>master<span style="font-family: '微软雅黑',sans-serif;">的全部</span>slot,<span style="font-family: '微软雅黑',sans-serif;">然后再删除当前节点</span>
</p>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">添加一个从节点</span>
</p>

<div class="cnblogs_code">
  <pre>./redis-trib.rb add-node --slave --master-id $[nodeid] 127.0.0.1:7008 127.0.0.1:7000</pre>
</div>

<h3>
  <span id="178">1.7.8 <span style="font-family: '微软雅黑',sans-serif;">状态说明</span></span>
</h3>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">集群最近一次向节点发送</span> PING <span style="font-family: '微软雅黑',sans-serif;">命令之后,</span> <span style="font-family: '微软雅黑',sans-serif;">过去了多长时间还没接到回复。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">节点最近一次返回</span> PONG <span style="font-family: '微软雅黑',sans-serif;">回复的时间。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">节点的配置节点(</span>configuration epoch<span style="font-family: '微软雅黑',sans-serif;">):详细信息请参考</span>Redis <span style="font-family: '微软雅黑',sans-serif;">集群规范</span> <span style="font-family: '微软雅黑',sans-serif;">。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">本节点的网络连接情况:例如</span> connected <span style="font-family: '微软雅黑',sans-serif;">。</span>
</p>

<p style="text-indent: 21.0pt;">
  <span style="font-family: '微软雅黑',sans-serif;">节点目前包含的槽:例如</span> 127.0.0.1:7001 <span style="font-family: '微软雅黑',sans-serif;">目前包含号码为</span> 5960 <span style="font-family: '微软雅黑',sans-serif;">至</span> 10921 <span style="font-family: '微软雅黑',sans-serif;">的哈希槽。</span>
</p>

<h2>
  <span id="18_Redis_API">1.8 Redis API</span>
</h2>

<h3>
  <span id="181_PHPredis">1.8.1 PHP<span style="font-family: '微软雅黑',sans-serif;">使用</span>redis</span>
</h3>

<div class="cnblogs_code">
  <pre>tar zxvf 2.2.7<span style="color: #000000;">.tar.gz

cd phpredis-2.2.7 /application/php/bin/phpize ./configure –with-php-config=/application/php/bin/php-config make && make install echo extension=“redis.so” » /application/php/lib/php.ini service php-fpm restart service nginx restart

<p>
  <span style="font-family: '微软雅黑',sans-serif;">连接测试代码</span>
</p>

<div class="cnblogs_code">
  <pre>[root@clsn ~]<span style="color: #008000;">#</span><span style="color: #008000;"> cat /application/nginx/html/check.php</span>

<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect(127.0.0.1, 6379); echo "Connection to server sucessfully"; //查看服务是否运行 echo "Server is running: " . $redis->ping(); ?>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">字符串操作</span>
</p>

<div class="cnblogs_code">
  <pre>&lt;<span style="color: #000000;">?php

//连接本地的 Redis 服务 $redis = new Redis(); $redis->connect(127.0.0.1, 6379); echo "Connection to server sucessfully"; //设置 redis 字符串数据 $redis->set("tutorial-name", "Redis tutorial"); // 获取存储的数据并输出 echo "Stored string in redis:: " . $redis-

get("tutorial-name"); ?>

<h3>
  <span id="182_Pythonredis">1.8.2 Python<span style="font-family: '微软雅黑',sans-serif;">连接</span>redis</span>
</h3>

<p>
  <span style="font-family: '微软雅黑',sans-serif;">安装软件包</span>
</p>

<div class="cnblogs_code">
  <pre>[root@Redis ~]<span style="color: #008000;">#</span><span style="color: #008000;"> yum install python-pip ipython -y  </span>

[root@Redis ~]# pip install redis

<p>
  <span style="font-family: '微软雅黑',sans-serif;">测试</span>
</p>

<div class="cnblogs_code">
  <pre>[root@Redis ~]<span style="color: #008000;">#</span><span style="color: #008000;"> ipython</span>

In 1: import redis

In [2]: clsn = redis.StrictRedis(host=localhost, port=6379, db=0)

In [3]: clsn.set(blog,blog.nmtui.com) Out[3]: True

In [4]: clsn.get(blog) Out[4]: blog.nmtui.com

<h2>
  <span id="19">1.9 <span style="font-family: '微软雅黑',sans-serif;">参考文献</span></span>
</h2>

<blockquote>
  <p style="margin-left: 21pt; text-indent: -21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">
    [1]&nbsp;<a href="/wp-content/themes/clsn-003/inc/go.php?url=http://blog.csdn.net/oanqoanq/article/details/50992535" >http://blog.csdn.net/oanqoanq/article/details/50992535</a>
  </p>
  
  <p style="margin-left: 21pt; text-indent: -21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">
    [2]&nbsp;<a href="/wp-content/themes/clsn-003/inc/go.php?url=http://www.redis.cn/topics/persistence.html" >http://www.redis.cn/topics/persistence.html</a>
  </p>
  
  <p style="margin-left: 21pt; text-indent: -21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">
    [3]&nbsp;<a href="/wp-content/themes/clsn-003/inc/go.php?url=http://www.runoob.com/redis/redis-tutorial.html" >http://www.runoob.com/redis/redis-tutorial.html</a>
  </p>
  
  <p style="margin-left: 21pt; text-indent: -21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">
    [4]&nbsp;<a href="/wp-content/themes/clsn-003/inc/go.php?url=http://blog.csdn.net/hechurui/article/details/49508813" >http://blog.csdn.net/hechurui/article/details/49508813</a>
  </p>
  
  <p style="margin-left: 21pt; text-indent: -21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">
    [5]&nbsp;<a href="https://www.cnblogs.com/lukexwang/p/4711977.html">https://www.cnblogs.com/lukexwang/p/4711977.html</a>
  </p>
  
  <p style="margin-left: 21pt; text-indent: -21pt; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">
    [6]&nbsp;<a href="https://www.cnblogs.com/daoluanxiaozi/p/3724299.html">https://www.cnblogs.com/daoluanxiaozi/p/3724299.html</a>
  </p>
</blockquote>

<div id="toc_container" class="toc_white have_bullets">
  <ul class="toc_list">
    <li>
      <a href="#11_Redis">1.1 Redis简介</a><ul>
        <li>
          <a href="#111">1.1.1 介绍</a>
        </li>
        <li>
          <a href="#112">1.1.2 软件获取和帮助</a>
        </li>
        <li>
          <a href="#113_Redis">1.1.3 Redis特性</a>
        </li>
        <li>
          <a href="#114">1.1.4 企业缓存数据库解决方案对比</a>
        </li>
        <li>
          <a href="#115_Redis">1.1.5 Redis应用场景</a>
        </li>
      </ul>
    </li>
    
    <li>
      <a href="#12_Redis">1.2 Redis简单部署</a><ul>
        <li>
          <a href="#121">1.2.1 典型安装-单实例</a>
        </li>
        <li>
          <a href="#122_redis">1.2.2 启动第一个redis实例</a>
        </li>
        <li>
          <a href="#123_Redis">1.2.3 Redis多实例配置</a>
        </li>
        <li>
          <a href="#124_redisconf">1.2.4 redis.conf配置说明</a>
        </li>
        <li>
          <a href="#125">1.2.5 在线变更配置</a>
        </li>
      </ul>
    </li>
    
    <li>
      <a href="#13_Redis">1.3 Redis数据持久化</a><ul>
        <li>
          <a href="#131">1.3.1 持久化策略</a>
        </li>
        <li>
          <a href="#132_RDB">1.3.2 RDB 持久化</a>
        </li>
        <li>
          <a href="#133_AOF">1.3.3 AOF 持久化</a>
        </li>
        <li>
          <a href="#134">1.3.4 如何选择使用哪种持久化方式</a>
        </li>
        <li>
          <a href="#135">1.3.5 快照实现持久化</a>
        </li>
        <li>
          <a href="#136_AOF">1.3.6 AOF持久化</a>
        </li>
        <li>
          <a href="#137_AOF">1.3.7 AOF日志重写</a>
        </li>
        <li>
          <a href="#138_AOF">1.3.8 AOF有多耐用?</a>
        </li>
        <li>
          <a href="#139_AOF">1.3.9 如果AOF文件损坏了怎么办?</a>
        </li>
        <li>
          <a href="#1310_AOFRDB">1.3.10 AOF和RDB之间的相互作用</a>
        </li>
        <li>
          <a href="#1311_redis">1.3.11 备份redis数据</a>
        </li>
        <li>
          <a href="#1312_RDB">1.3.12 RDB持久化配置</a>
        </li>
        <li>
          <a href="#1313_AOF">1.3.13 AOF持久化配置</a>
        </li>
        <li>
          <a href="#1314_RDBAOF">1.3.14 RDB到AOF切换</a>
        </li>
      </ul>
    </li>
    
    <li>
      <a href="#14_Redis">1.4 Redis管理实战</a><ul>
        <li>
          <a href="#141">1.4.1 基本数据类型</a>
        </li>
        <li>
          <a href="#142_Key">1.4.2 全局Key操作</a>
        </li>
        <li>
          <a href="#143_String">1.4.3 String(字符串)</a>
        </li>
        <li>
          <a href="#144_Hash">1.4.4 Hash(字典)</a>
        </li>
        <li>
          <a href="#145_LIST">1.4.5 LIST(列表)</a>
        </li>
        <li>
          <a href="#146_SET">1.4.6 SET(集合)</a>
        </li>
        <li>
          <a href="#147_SortedSet">1.4.7 SortedSet(有序集合)</a>
        </li>
        <li>
          <a href="#148">1.4.8 消息模式</a>
        </li>
        <li>
          <a href="#149_Redis">1.4.9 Redis 发布订阅</a><ul>
            <li>
              <a href="#1491nbsp">1.4.9.1&nbsp;发布订阅模型</a>
            </li>
            <li>
              <a href="#1492nbsp">1.4.9.2&nbsp;实践发布订阅</a>
            </li>
            <li>
              <a href="#1493nbsp">1.4.9.3&nbsp;消息队列系统对比</a>
            </li>
          </ul>
        </li>
        
        <li>
          <a href="#1410_Redis">1.4.10 Redis事务管理</a><ul>
            <li>
              <a href="#14101nbspnbspnbspnbsp_Redis">1.4.10.1&nbsp;&nbsp;&nbsp;&nbsp; Redis 事务命令</a>
            </li>
            <li>
              <a href="#14102nbspnbspnbspnbsp_Redis">1.4.10.2&nbsp;&nbsp;&nbsp;&nbsp; Redis中事务中的锁机制</a>
            </li>
            <li>
              <a href="#14103nbspnbspnbspnbsp_Redis">1.4.10.3&nbsp;&nbsp;&nbsp;&nbsp; Redis服务管理命令</a>
            </li>
          </ul>
        </li>
        
        <li>
          <a href="#1411_redis">1.4.11 redis慢日志查询</a>
        </li>
      </ul>
    </li>
    
    <li>
      <a href="#15_Redis">1.5 Redis主从复制</a><ul>
        <li>
          <a href="#151_redis">1.5.1 redis复制特性</a>
        </li>
        <li>
          <a href="#152">1.5.2 主从复制原理</a>
        </li>
        <li>
          <a href="#153_SYNCPSYNC">1.5.3 复制中的SYNC与PSYNC</a>
        </li>
        <li>
          <a href="#154">1.5.4 复制的一致性问题</a>
        </li>
        <li>
          <a href="#155">1.5.5 复制安全性提升</a>
        </li>
        <li>
          <a href="#156_Redis">1.5.6 Redis主从复制实践</a>
        </li>
        <li>
          <a href="#157_Redis">1.5.7 Redis主从复制管理</a>
        </li>
      </ul>
    </li>
    
    <li>
      <a href="#16_Redis_HA_Redis_Sentinel">1.6 Redis HA 实践(Redis Sentinel)</a><ul>
        <li>
          <a href="#161_Redis_Sentinel">1.6.1 Redis Sentinel 功能</a>
        </li>
        <li>
          <a href="#162">1.6.2 服务器连接</a>
        </li>
        <li>
          <a href="#163">1.6.3 检测实例的状态</a>
        </li>
        <li>
          <a href="#164_FAILOVER">1.6.4 故障转移FAILOVER</a>
        </li>
        <li>
          <a href="#165_sentinel">1.6.5 配置sentinel</a>
        </li>
        <li>
          <a href="#166_Sentinel">1.6.6 Sentinel命令操作</a>
        </li>
        <li>
          <a href="#167_Sentinel">1.6.7 Sentinel发布与订阅信息</a>
        </li>
      </ul>
    </li>
    
    <li>
      <a href="#17_Redis_cluster">1.7 Redis cluster</a><ul>
        <li>
          <a href="#171_Redis">1.7.1 Redis集群</a>
        </li>
        <li>
          <a href="#172_Redis">1.7.2 Redis 集群数据共享</a>
        </li>
        <li>
          <a href="#173">1.7.3 集群运行机制</a>
        </li>
        <li>
          <a href="#174">1.7.4 集群的故障转移</a>
        </li>
        <li>
          <a href="#175">1.7.5 关于转向错误</a>
        </li>
        <li>
          <a href="#176">1.7.6 配置集群</a>
        </li>
        <li>
          <a href="#177">1.7.7 集群管理</a>
        </li>
        <li>
          <a href="#178">1.7.8 状态说明</a>
        </li>
      </ul>
    </li>
    
    <li>
      <a href="#18_Redis_API">1.8 Redis API</a><ul>
        <li>
          <a href="#181_PHPredis">1.8.1 PHP使用redis</a>
        </li>
        <li>
          <a href="#182_Pythonredis">1.8.2 Python连接redis</a>
        </li>
      </ul>
    </li>
    
    <li>
      <a href="#19">1.9 参考文献</a>
    </li>
  </ul>
</div>