mirror of https://github.com/dunwu/db-tutorial.git
146 lines
79 KiB
HTML
146 lines
79 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en-US">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||
<title>Redis 应用指南 | DB-TUTORIAL</title>
|
||
<meta name="generator" content="VuePress 1.9.9">
|
||
<link rel="icon" href="/db-tutorial/img/favicon.ico">
|
||
<script src="https://cdn.wwads.cn/js/makemoney.js" type="text/javascript"></script>
|
||
<meta name="description" content="☕ db-tutorial 是一个数据库教程。">
|
||
<meta name="keywords" content="vuepress,theme,blog,vdoing">
|
||
<meta name="theme-color" content="#11a8cd">
|
||
<meta name="wwads-cn-verify" content="mxqWx62nfQQ9ocT4e5DzISHzOWyF4s">
|
||
|
||
<link rel="preload" href="/db-tutorial/assets/css/0.styles.51390d19.css" as="style"><link rel="preload" href="/db-tutorial/assets/js/app.be3f2e92.js" as="script"><link rel="preload" href="/db-tutorial/assets/js/2.aded268b.js" as="script"><link rel="preload" href="/db-tutorial/assets/js/45.68f2ab27.js" as="script"><link rel="prefetch" href="/db-tutorial/assets/js/10.551ab278.js"><link rel="prefetch" href="/db-tutorial/assets/js/11.c049b6a2.js"><link rel="prefetch" href="/db-tutorial/assets/js/12.92d587d6.js"><link rel="prefetch" href="/db-tutorial/assets/js/13.a7b35fa5.js"><link rel="prefetch" href="/db-tutorial/assets/js/14.9ec959b5.js"><link rel="prefetch" href="/db-tutorial/assets/js/15.e7943372.js"><link rel="prefetch" href="/db-tutorial/assets/js/16.c6ad7b26.js"><link rel="prefetch" href="/db-tutorial/assets/js/17.39500ebd.js"><link rel="prefetch" href="/db-tutorial/assets/js/18.29949979.js"><link rel="prefetch" href="/db-tutorial/assets/js/19.78f879b8.js"><link rel="prefetch" href="/db-tutorial/assets/js/20.3ce14df7.js"><link rel="prefetch" href="/db-tutorial/assets/js/21.9d311c7c.js"><link rel="prefetch" href="/db-tutorial/assets/js/22.90234550.js"><link rel="prefetch" href="/db-tutorial/assets/js/23.12cf9e2d.js"><link rel="prefetch" href="/db-tutorial/assets/js/24.5b436e46.js"><link rel="prefetch" href="/db-tutorial/assets/js/25.8f2dc7d9.js"><link rel="prefetch" href="/db-tutorial/assets/js/26.61665ff4.js"><link rel="prefetch" href="/db-tutorial/assets/js/27.dfc7cc88.js"><link rel="prefetch" href="/db-tutorial/assets/js/28.a6286a66.js"><link rel="prefetch" href="/db-tutorial/assets/js/29.bfa8c106.js"><link rel="prefetch" href="/db-tutorial/assets/js/3.82108019.js"><link rel="prefetch" href="/db-tutorial/assets/js/30.c93b7a2e.js"><link rel="prefetch" href="/db-tutorial/assets/js/31.d48aab81.js"><link rel="prefetch" href="/db-tutorial/assets/js/32.33f8bb7c.js"><link rel="prefetch" href="/db-tutorial/assets/js/33.eb3b622d.js"><link rel="prefetch" href="/db-tutorial/assets/js/34.1ba1a06c.js"><link rel="prefetch" href="/db-tutorial/assets/js/35.24e4fee8.js"><link rel="prefetch" href="/db-tutorial/assets/js/36.dc61504e.js"><link rel="prefetch" href="/db-tutorial/assets/js/37.f1c9c729.js"><link rel="prefetch" href="/db-tutorial/assets/js/38.5024023f.js"><link rel="prefetch" href="/db-tutorial/assets/js/39.30ed016e.js"><link rel="prefetch" href="/db-tutorial/assets/js/4.884deeca.js"><link rel="prefetch" href="/db-tutorial/assets/js/40.6a6327f0.js"><link rel="prefetch" href="/db-tutorial/assets/js/41.3767f4d8.js"><link rel="prefetch" href="/db-tutorial/assets/js/42.14a20b0f.js"><link rel="prefetch" href="/db-tutorial/assets/js/43.b059081c.js"><link rel="prefetch" href="/db-tutorial/assets/js/44.73f724d9.js"><link rel="prefetch" href="/db-tutorial/assets/js/46.aed8a61f.js"><link rel="prefetch" href="/db-tutorial/assets/js/47.3bbf2366.js"><link rel="prefetch" href="/db-tutorial/assets/js/48.81688356.js"><link rel="prefetch" href="/db-tutorial/assets/js/49.1d366c93.js"><link rel="prefetch" href="/db-tutorial/assets/js/5.42f2cdb7.js"><link rel="prefetch" href="/db-tutorial/assets/js/50.ee6f08b7.js"><link rel="prefetch" href="/db-tutorial/assets/js/51.47835d9d.js"><link rel="prefetch" href="/db-tutorial/assets/js/52.b0cab79c.js"><link rel="prefetch" href="/db-tutorial/assets/js/53.106440a2.js"><link rel="prefetch" href="/db-tutorial/assets/js/54.340ab690.js"><link rel="prefetch" href="/db-tutorial/assets/js/55.00623de9.js"><link rel="prefetch" href="/db-tutorial/assets/js/56.9f086601.js"><link rel="prefetch" href="/db-tutorial/assets/js/57.f5ad5f3f.js"><link rel="prefetch" href="/db-tutorial/assets/js/58.14827368.js"><link rel="prefetch" href="/db-tutorial/assets/js/59.c41f57d6.js"><link rel="prefetch" href="/db-tutorial/assets/js/6.5a5b4e54.js"><link rel="prefetch" href="/db-tutorial/assets/js/60.314534c0.js"><link rel="prefetch" href="/db-tutorial/assets/js/61.1ea5e4b7.js"><link rel="prefetch" href="/db-tutorial/assets/js/62.8b52e8fc.js"><link rel="prefetch" href="/db-tutorial/assets/js/63.d2fa8325.js"><link rel="prefetch" href="/db-tutorial/assets/js/64.ea2577e7.js"><link rel="prefetch" href="/db-tutorial/assets/js/65.563da2bb.js"><link rel="prefetch" href="/db-tutorial/assets/js/66.34eb51bd.js"><link rel="prefetch" href="/db-tutorial/assets/js/67.ab57f04f.js"><link rel="prefetch" href="/db-tutorial/assets/js/68.15b6f540.js"><link rel="prefetch" href="/db-tutorial/assets/js/69.54590de4.js"><link rel="prefetch" href="/db-tutorial/assets/js/7.913bec54.js"><link rel="prefetch" href="/db-tutorial/assets/js/70.40a2cea2.js"><link rel="prefetch" href="/db-tutorial/assets/js/71.3ce50922.js"><link rel="prefetch" href="/db-tutorial/assets/js/72.b9c022e9.js"><link rel="prefetch" href="/db-tutorial/assets/js/73.fba94661.js"><link rel="prefetch" href="/db-tutorial/assets/js/74.998d6c2f.js"><link rel="prefetch" href="/db-tutorial/assets/js/75.6efb68b0.js"><link rel="prefetch" href="/db-tutorial/assets/js/76.57273256.js"><link rel="prefetch" href="/db-tutorial/assets/js/77.3ddffb5b.js"><link rel="prefetch" href="/db-tutorial/assets/js/78.8fde3d74.js"><link rel="prefetch" href="/db-tutorial/assets/js/79.7a472c31.js"><link rel="prefetch" href="/db-tutorial/assets/js/8.103b4774.js"><link rel="prefetch" href="/db-tutorial/assets/js/80.4c55c65f.js"><link rel="prefetch" href="/db-tutorial/assets/js/81.cbdb67b8.js"><link rel="prefetch" href="/db-tutorial/assets/js/82.9c14d852.js"><link rel="prefetch" href="/db-tutorial/assets/js/83.a3d7d272.js"><link rel="prefetch" href="/db-tutorial/assets/js/84.6994dacc.js"><link rel="prefetch" href="/db-tutorial/assets/js/85.aca8d788.js"><link rel="prefetch" href="/db-tutorial/assets/js/86.00671865.js"><link rel="prefetch" href="/db-tutorial/assets/js/87.9076c4e8.js"><link rel="prefetch" href="/db-tutorial/assets/js/88.bbe68dab.js"><link rel="prefetch" href="/db-tutorial/assets/js/89.e2173071.js"><link rel="prefetch" href="/db-tutorial/assets/js/9.386bfe3a.js"><link rel="prefetch" href="/db-tutorial/assets/js/90.429162f2.js"><link rel="prefetch" href="/db-tutorial/assets/js/91.6ecc7c85.js"><link rel="prefetch" href="/db-tutorial/assets/js/92.efe42934.js"><link rel="prefetch" href="/db-tutorial/assets/js/93.69562766.js"><link rel="prefetch" href="/db-tutorial/assets/js/94.04aebfbc.js"><link rel="prefetch" href="/db-tutorial/assets/js/95.fe7cfce4.js"><link rel="prefetch" href="/db-tutorial/assets/js/96.674475d6.js"><link rel="prefetch" href="/db-tutorial/assets/js/97.8a7b73f2.js"><link rel="prefetch" href="/db-tutorial/assets/js/98.bf85add1.js">
|
||
<link rel="stylesheet" href="/db-tutorial/assets/css/0.styles.51390d19.css">
|
||
</head>
|
||
<body class="theme-mode-light">
|
||
<div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/db-tutorial/" class="home-link router-link-active"><img src="https://raw.githubusercontent.com/dunwu/images/master/common/dunwu-logo.png" alt="DB-TUTORIAL" class="logo"> <span class="site-name can-hide">DB-TUTORIAL</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/db-tutorial/12.数据库/01.数据库综合/" class="nav-link">数据库综合</a></div><div class="nav-item"><a href="/db-tutorial/12.数据库/02.数据库中间件/" class="nav-link">数据库中间件</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关系型数据库" class="dropdown-title"><a href="/db-tutorial/12.数据库/03.关系型数据库/" class="link-title">关系型数据库</a> <span class="title" style="display:none;">关系型数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/01.综合/" class="nav-link">综合</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/02.Mysql/" class="nav-link">Mysql</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/99.其他/" class="nav-link">其他</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档数据库" class="dropdown-title"><!----> <span class="title" style="display:;">文档数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/04.文档数据库/01.MongoDB/" class="nav-link">MongoDB</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="KV数据库" class="dropdown-title"><!----> <span class="title" style="display:;">KV数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/05.KV数据库/01.Redis/" class="nav-link">Redis</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="搜索引擎数据库" class="dropdown-title"><!----> <span class="title" style="display:;">搜索引擎数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/07.搜索引擎数据库/01.Elasticsearch/" class="nav-link">Elasticsearch</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/07.搜索引擎数据库/02.Elastic/" class="nav-link">Elastic技术栈</a></li></ul></div></div> <a href="https://github.com/dunwu/db-tutorial" target="_blank" rel="noopener noreferrer" class="repo-link">
|
||
GitHub
|
||
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><!----> <nav class="nav-links"><div class="nav-item"><a href="/db-tutorial/12.数据库/01.数据库综合/" class="nav-link">数据库综合</a></div><div class="nav-item"><a href="/db-tutorial/12.数据库/02.数据库中间件/" class="nav-link">数据库中间件</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关系型数据库" class="dropdown-title"><a href="/db-tutorial/12.数据库/03.关系型数据库/" class="link-title">关系型数据库</a> <span class="title" style="display:none;">关系型数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/01.综合/" class="nav-link">综合</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/02.Mysql/" class="nav-link">Mysql</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/99.其他/" class="nav-link">其他</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档数据库" class="dropdown-title"><!----> <span class="title" style="display:;">文档数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/04.文档数据库/01.MongoDB/" class="nav-link">MongoDB</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="KV数据库" class="dropdown-title"><!----> <span class="title" style="display:;">KV数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/05.KV数据库/01.Redis/" class="nav-link">Redis</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="搜索引擎数据库" class="dropdown-title"><!----> <span class="title" style="display:;">搜索引擎数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/07.搜索引擎数据库/01.Elasticsearch/" class="nav-link">Elasticsearch</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/07.搜索引擎数据库/02.Elastic/" class="nav-link">Elastic技术栈</a></li></ul></div></div> <a href="https://github.com/dunwu/db-tutorial" target="_blank" rel="noopener noreferrer" class="repo-link">
|
||
GitHub
|
||
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库综合</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>关系型数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>文档数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>KV数据库</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading open"><span>Redis</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/db-tutorial/pages/451b73/" class="sidebar-link">Redis 面试总结</a></li><li><a href="/db-tutorial/pages/94e9d6/" aria-current="page" class="active sidebar-link">Redis 应用指南</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#一、redis-简介" class="sidebar-link">一、Redis 简介</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#redis-使用场景" class="sidebar-link">Redis 使用场景</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#redis-的优势" class="sidebar-link">Redis 的优势</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#redis-与-memcached" class="sidebar-link">Redis 与 Memcached</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#redis-为什么快" class="sidebar-link">Redis 为什么快</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#二、redis-数据类型" class="sidebar-link">二、Redis 数据类型</a></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#三、redis-内存淘汰" class="sidebar-link">三、Redis 内存淘汰</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#内存淘汰要点" class="sidebar-link">内存淘汰要点</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#主键过期时间" class="sidebar-link">主键过期时间</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#淘汰策略" class="sidebar-link">淘汰策略</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#如何选择淘汰策略" class="sidebar-link">如何选择淘汰策略</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#内部实现" class="sidebar-link">内部实现</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#四、redis-持久化" class="sidebar-link">四、Redis 持久化</a></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#五、redis-事件" class="sidebar-link">五、Redis 事件</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#文件事件" class="sidebar-link">文件事件</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#时间事件" class="sidebar-link">时间事件</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#事件的调度与执行" class="sidebar-link">事件的调度与执行</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#六、redis-事务" class="sidebar-link">六、Redis 事务</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#multi" class="sidebar-link">MULTI</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#exec" class="sidebar-link">EXEC</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#discard" class="sidebar-link">DISCARD</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#watch" class="sidebar-link">WATCH</a></li><li class="sidebar-sub-header level4"><a href="/db-tutorial/pages/94e9d6/#取消-watch-的场景" class="sidebar-link">取消 WATCH 的场景</a></li><li class="sidebar-sub-header level4"><a href="/db-tutorial/pages/94e9d6/#使用-watch-创建原子操作" class="sidebar-link">使用 WATCH 创建原子操作</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#rollback" class="sidebar-link">Rollback</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#七、redis-管道" class="sidebar-link">七、Redis 管道</a></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#八、redis-发布与订阅" class="sidebar-link">八、Redis 发布与订阅</a></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#九、redis-复制" class="sidebar-link">九、Redis 复制</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#旧版复制" class="sidebar-link">旧版复制</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#新版复制" class="sidebar-link">新版复制</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#部分重同步" class="sidebar-link">部分重同步</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#psync-命令" class="sidebar-link">PSYNC 命令</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/94e9d6/#心跳检测" class="sidebar-link">心跳检测</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#十、redis-哨兵" class="sidebar-link">十、Redis 哨兵</a></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#十一、redis-集群" class="sidebar-link">十一、Redis 集群</a></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#十二、redis-client" class="sidebar-link">十二、Redis Client</a></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#扩展阅读" class="sidebar-link">扩展阅读</a></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/94e9d6/#参考资料" class="sidebar-link">参考资料</a></li></ul></li><li><a href="/db-tutorial/pages/ed757c/" class="sidebar-link">Redis 数据类型和应用</a></li><li><a href="/db-tutorial/pages/4de901/" class="sidebar-link">Redis 持久化</a></li><li><a href="/db-tutorial/pages/379cd8/" class="sidebar-link">Redis 复制</a></li><li><a href="/db-tutorial/pages/615afe/" class="sidebar-link">Redis 哨兵</a></li><li><a href="/db-tutorial/pages/77dfbe/" class="sidebar-link">Redis 集群</a></li><li><a href="/db-tutorial/pages/1fc9c4/" class="sidebar-link">Redis 实战</a></li><li><a href="/db-tutorial/pages/537098/" class="sidebar-link">Redis 运维</a></li></ul></section></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>列式数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>搜索引擎数据库</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/db-tutorial/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/db-tutorial/categories/?category=%E6%95%B0%E6%8D%AE%E5%BA%93" title="分类" data-v-06225672>数据库</a></li><li data-v-06225672><a href="/db-tutorial/categories/?category=KV%E6%95%B0%E6%8D%AE%E5%BA%93" title="分类" data-v-06225672>KV数据库</a></li><li data-v-06225672><a href="/db-tutorial/categories/?category=Redis" title="分类" data-v-06225672>Redis</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/dunwu" target="_blank" title="作者" class="beLink" data-v-06225672>dunwu</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2020-01-30</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAAAXNSR0IArs4c6QAABH1JREFUSA3tVl1oHFUUPmdmd2ltklqbpJDiNnXFmgbFktho7YMPNiJSSZM0+CAYSkUELVhM6YuwIPpgoOKDqOBDC0XE2CQoNtQXBUFTTcCi+Wlh1V2TQExsUzcltd3M9Tt3ZjZzZ2fT+OJTL8yeM+eee757fmeJbq//KQL8X3DUSFOcfr7cRsRtxNQMWueeVzOkaITIGqQHNg5y8+jNW9ldM7A6nTpAjuolUikAwq7CE3WcM2RRDz+XGVgN3FptU/aUSlvq9Pa3iZ1+sgAqJyyAFqkipd9dqiwHF3P65YycLWc/6sqGrvoEoIp6DOFaX5h6+dnfjkWprwqsPk0dUGq5vySwDImC10KxFHgGL1SWoc92O3eVht09qdXNH11I2SsTsJYqMWzihqGMi+A+Garf3BAuuLI5oGlULyNfyB/HYNujwktOfRrMr5t77NmevqaUopx0grnKAyvVpmwUDB4x6FPXuGvYLTDwWsejwgtgkYKPqRJg8SV6xaiZ3ZTppGneS4yfH5/66fZSDHv+QZci/+h5c5UHtpy67JUqGppM0sh0Nc1dW6/N1W5Yoqat8/TU/VnadmdeW2PLLSyh0cvxBs3KbqTmwYPpxN4do/mzE8nEpvX/UMu2Wbp74zUAK5q6WkHns7V0eWkdPbPzd3rxkTGybadYySumVzhcaJFbs5UrEkQ/+CK8gF5dnh/6ciIZ73gwQ927L1IitoxKLXYP3SjYdOrHHfTZhRRlFyrorafPk20B3HPD1y2G3qKZME5Jcf3t/HUC13/8tSd++vqFveMUTwAUxSUFI1QekR1+bIze3D9MF2aq6cPvG72CgnldWCFqyRw3lwH8ZMerjTD9ElRO7Gv44wNpC90aASqGfVlz/Rx17srQ57/UU26hkhQqUB7dBR71WmzQhHUnblGmVOEw0jhbV1n9OlXUDCIRGaNV5Jp43N516fN7JmnTHdfp7Hgy0luO4aMhtkLL8Bi3bUWYvzh5Mn1dTxrL6QmGuRhGL/TiTTxRoEdTszSaq9GR0NGA3KdkOz3hqSV3MIDhQ5IVX/Ivx3umBti2es2h4eZby7x8br1rkf7Mo90AqC8aQ3sJeNzqFRu+vSANAQe3PL7l0HGOAdwDCeZYvNKeoZp1Qfs6Aipndh86HmFRi0LAnEO47wsqM6cdfjh3jBPUzhZy7nvlUfFsamED1VQt6aISHVymXZ/B2aCtIG8AI8xfobj2d3en1wWVhOeHELKmLQ1s211s88comkv4UCwWyF787mJdYXtNfhKAXVqnKTq8QZvGAGGOfaTo5pGZ/PwbUCr5+DPr/1J92JNHr9aOl/F3iI5+O1nfybsGxoimvZ3ViWSluDITw3P37mypheDIPY0tw7+O/5ApbkYw+zpfaUVu32Pi98+defdUhEpZkRFq0aqyNh9FuL9hpYbEm6iwi0z2REd09ZmyENEbuhjDWzKvZXTqKYaBIr3tt5kuPtQBZFvEUwHt60vfCNu41XsksH9Ij1BMMz1Y0OOunHNShFIP5868g5zeXmuLwL9T4b6Q2+KejgAAAABJRU5ErkJggg==">Redis 应用指南<!----></h1> <div class="theme-vdoing-content content__default"><h1 id="redis-应用指南"><a href="#redis-应用指南" class="header-anchor">#</a> Redis 应用指南</h1> <h2 id="一、redis-简介"><a href="#一、redis-简介" class="header-anchor">#</a> 一、Redis 简介</h2> <blockquote><p>Redis 是速度非常快的非关系型(NoSQL)内存键值数据库,可以存储键和五种不同类型的值之间的映射。</p> <p>键的类型只能为字符串,值支持的五种类型数据类型为:字符串、列表、集合、有序集合、散列表。</p></blockquote> <h3 id="redis-使用场景"><a href="#redis-使用场景" class="header-anchor">#</a> Redis 使用场景</h3> <ul><li><strong>缓存</strong> - 将热点数据放到内存中,设置内存的最大使用量以及过期淘汰策略来保证缓存的命中率。</li> <li><strong>计数器</strong> - Redis 这种内存数据库能支持计数器频繁的读写操作。</li> <li><strong>应用限流</strong> - 限制一个网站访问流量。</li> <li><strong>消息队列</strong> - 使用 List 数据类型,它是双向链表。</li> <li><strong>查找表</strong> - 使用 HASH 数据类型。</li> <li><strong>交集运算</strong> - 使用 SET 类型,例如求两个用户的共同好友。</li> <li><strong>排行榜</strong> - 使用 ZSET 数据类型。</li> <li><strong>分布式 Session</strong> - 多个应用服务器的 Session 都存储到 Redis 中来保证 Session 的一致性。</li> <li><strong>分布式锁</strong> - 除了可以使用 SETNX 实现分布式锁之外,还可以使用官方提供的 RedLock 分布式锁实现。</li></ul> <h3 id="redis-的优势"><a href="#redis-的优势" class="header-anchor">#</a> Redis 的优势</h3> <ul><li>性能极高 – Redis 能读的速度是 110000 次/s,写的速度是 81000 次/s。</li> <li>丰富的数据类型 - 支持字符串、列表、集合、有序集合、散列表。</li> <li>原子 - Redis 的所有操作都是原子性的。单个操作是原子性的。多个操作也支持事务,即原子性,通过 MULTI 和 EXEC 指令包起来。</li> <li>持久化 - Redis 支持数据的持久化。可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。</li> <li>备份 - Redis 支持数据的备份,即 master-slave 模式的数据备份。</li> <li>丰富的特性 - Redis 还支持发布订阅, 通知, key 过期等等特性。</li></ul> <h3 id="redis-与-memcached"><a href="#redis-与-memcached" class="header-anchor">#</a> Redis 与 Memcached</h3> <p>Redis 与 Memcached 因为都可以用于缓存,所以常常被拿来做比较,二者主要有以下区别:</p> <p><strong>数据类型</strong></p> <ul><li>Memcached 仅支持字符串类型;</li> <li>而 Redis 支持五种不同种类的数据类型,使得它可以更灵活地解决问题。</li></ul> <p><strong>数据持久化</strong></p> <ul><li>Memcached 不支持持久化;</li> <li>Redis 支持两种持久化策略:RDB 快照和 AOF 日志。</li></ul> <p><strong>分布式</strong></p> <ul><li>Memcached 不支持分布式,只能通过在客户端使用像一致性哈希这样的分布式算法来实现分布式存储,这种方式在存储和查询时都需要先在客户端计算一次数据所在的节点。</li> <li>Redis Cluster 实现了分布式的支持。</li></ul> <p><strong>内存管理机制</strong></p> <ul><li>Memcached 将内存分割成特定长度的块来存储数据,以完全解决内存碎片的问题,但是这种方式会使得内存的利用率不高,例如块的大小为 128 bytes,只存储 100 bytes 的数据,那么剩下的 28 bytes 就浪费掉了。</li> <li>在 Redis 中,并不是所有数据都一直存储在内存中,可以将一些很久没用的 value 交换到磁盘。而 Memcached 的数据则会一直在内存中。</li></ul> <h3 id="redis-为什么快"><a href="#redis-为什么快" class="header-anchor">#</a> Redis 为什么快</h3> <p>Redis 单机 QPS 能达到 100000。</p> <p>Redis 是单线程模型(Redis 6.0 已经支持多线程模型),为什么还能有这么高的并发?</p> <ul><li>Redis 完全基于内存操作。</li> <li>Redis 数据结构简单。</li> <li>采用单线程,避免线程上下文切换和竞争。</li> <li>使用 I/O 多路复用模型(非阻塞 I/O)。</li></ul> <blockquote><p>I/O 多路复用</p> <p>I/O 多路复用模型是利用 select、poll、epoll 可以同时监察多个流的 I/O 事件的能力,在空闲的时候,会把当前线程阻塞掉,当有一个或多个流有 I/O 事件时,就从阻塞态中唤醒,于是程序就会轮询一遍所有的流(epoll 是只轮询那些真正发出了事件的流),并且只依次顺序的处理就绪的流,这种做法就避免了大量的无用操作。</p></blockquote> <h2 id="二、redis-数据类型"><a href="#二、redis-数据类型" class="header-anchor">#</a> 二、Redis 数据类型</h2> <p>Redis 基本数据类型:STRING、HASH、LIST、SET、ZSET</p> <p>Redis 高级数据类型:BitMap、HyperLogLog、GEO</p> <blockquote><p>💡 更详细的特性及原理说明请参考:<a href="/db-tutorial/12.数据库/05.KV数据库/01.Redis/03.Redis数据类型和应用.html">Redis 数据类型和应用</a></p></blockquote> <h2 id="三、redis-内存淘汰"><a href="#三、redis-内存淘汰" class="header-anchor">#</a> 三、Redis 内存淘汰</h2> <h3 id="内存淘汰要点"><a href="#内存淘汰要点" class="header-anchor">#</a> 内存淘汰要点</h3> <ul><li><p><strong>最大缓存</strong> - Redis 允许通过 <code>maxmemory</code> 参数来设置内存最大值。</p></li> <li><p><strong>失效时间</strong> - 作为一种定期清理无效数据的重要机制,在 Redis 提供的诸多命令中,<code>EXPIRE</code>、<code>EXPIREAT</code>、<code>PEXPIRE</code>、<code>PEXPIREAT</code> 以及 <code>SETEX</code> 和 <code>PSETEX</code> 均可以用来设置一条键值对的失效时间。而一条键值对一旦被关联了失效时间就会在到期后自动删除(或者说变得无法访问更为准确)。</p></li> <li><p><strong>淘汰策略</strong> - 随着不断的向 Redis 中保存数据,当内存剩余空间无法满足添加的数据时,Redis 内就会施行数据淘汰策略,清除一部分内容然后保证新的数据可以保存到内存中。内存淘汰机制是为了更好的使用内存,用一定得 miss 来换取内存的利用率,保证 Redis 缓存中保存的都是热点数据。</p></li> <li><p><strong>非精准的 LRU</strong> - 实际上 Redis 实现的 LRU 并不是可靠的 LRU,也就是名义上我们使用 LRU 算法淘汰键,但是实际上被淘汰的键并不一定是真正的最久没用的。</p></li></ul> <h3 id="主键过期时间"><a href="#主键过期时间" class="header-anchor">#</a> 主键过期时间</h3> <p>Redis 可以为每个键设置过期时间,当键过期时,会自动删除该键。</p> <p>对于散列表这种容器,只能为整个键设置过期时间(整个散列表),而不能为键里面的单个元素设置过期时间。</p> <p>可以使用 <code>EXPIRE</code> 或 <code>EXPIREAT</code> 来为 key 设置过期时间。</p> <blockquote><p>🔔 注意:当 <code>EXPIRE</code> 的时间如果设置的是负数,<code>EXPIREAT</code> 设置的时间戳是过期时间,将直接删除 key。</p></blockquote> <p>示例:</p> <div class="language-shell extra-class"><pre class="language-shell"><code>redis<span class="token operator">></span> SET mykey <span class="token string">"Hello"</span>
|
||
<span class="token string">"OK"</span>
|
||
redis<span class="token operator">></span> EXPIRE mykey <span class="token number">10</span>
|
||
<span class="token punctuation">(</span>integer<span class="token punctuation">)</span> <span class="token number">1</span>
|
||
redis<span class="token operator">></span> TTL mykey
|
||
<span class="token punctuation">(</span>integer<span class="token punctuation">)</span> <span class="token number">10</span>
|
||
redis<span class="token operator">></span> SET mykey <span class="token string">"Hello World"</span>
|
||
<span class="token string">"OK"</span>
|
||
redis<span class="token operator">></span> TTL mykey
|
||
<span class="token punctuation">(</span>integer<span class="token punctuation">)</span> <span class="token parameter variable">-1</span>
|
||
redis<span class="token operator">></span>
|
||
</code></pre></div><h3 id="淘汰策略"><a href="#淘汰策略" class="header-anchor">#</a> 淘汰策略</h3> <p>内存淘汰只是 Redis 提供的一个功能,为了更好地实现这个功能,必须为不同的应用场景提供不同的策略,内存淘汰策略讲的是为实现内存淘汰我们具体怎么做,要解决的问题包括淘汰键空间如何选择?在键空间中淘汰键如何选择?</p> <p>Redis 提供了下面几种内存淘汰策略供用户选:</p> <ul><li><strong><code>noeviction</code></strong> - 当内存使用达到阈值的时候,所有引起申请内存的命令会报错。这是 Redis 默认的策略。</li> <li><strong><code>allkeys-lru</code></strong> - 在主键空间中,优先移除最近未使用的 key。</li> <li><strong><code>allkeys-random</code></strong> - 在主键空间中,随机移除某个 key。</li> <li><strong><code>volatile-lru</code></strong> - 在设置了过期时间的键空间中,优先移除最近未使用的 key。</li> <li><strong><code>volatile-random</code></strong> - 在设置了过期时间的键空间中,随机移除某个 key。</li> <li><strong><code>volatile-ttl</code></strong> - 在设置了过期时间的键空间中,具有更早过期时间的 key 优先移除。</li></ul> <h3 id="如何选择淘汰策略"><a href="#如何选择淘汰策略" class="header-anchor">#</a> 如何选择淘汰策略</h3> <ul><li>如果<strong>数据呈现幂等分布(存在热点数据,部分数据访问频率高,部分数据访问频率低),则使用 <code>allkeys-lru</code></strong>。</li> <li>如果<strong>数据呈现平等分布(数据访问频率大致相同),则使用 <code>allkeys-random</code></strong>。</li> <li>如果希望<strong>使用不同的 TTL 值向 Redis 提示哪些 key 更适合被淘汰,请使用 <code>volatile-ttl</code></strong>。</li> <li><strong><code>volatile-lru</code> 和 <code>volatile-random</code> 适合既应用于缓存和又应用于持久化存储的场景</strong>,然而我们也可以通过使用两个 Redis 实例来达到相同的效果。</li> <li><strong>将 key 设置过期时间实际上会消耗更多的内存,因此建议使用 <code>allkeys-lru</code> 策略从而更有效率的使用内存</strong>。</li></ul> <h3 id="内部实现"><a href="#内部实现" class="header-anchor">#</a> 内部实现</h3> <p>Redis 删除失效主键的方法主要有两种:</p> <ul><li>消极方法(passive way),在主键被访问时如果发现它已经失效,那么就删除它。</li> <li>主动方法(active way),周期性地从设置了失效时间的主键中选择一部分失效的主键删除。</li> <li>主动删除:当前已用内存超过 <code>maxmemory</code> 限定时,触发主动清理策略,该策略由启动参数的配置决定主键具体的失效时间全部都维护在 <code>expires</code> 这个字典表中。</li></ul> <h2 id="四、redis-持久化"><a href="#四、redis-持久化" class="header-anchor">#</a> 四、Redis 持久化</h2> <p>Redis 是内存型数据库,为了保证数据在宕机后不会丢失,需要将内存中的数据持久化到硬盘上。</p> <p>Redis 支持两种持久化方式:RDB 和 AOF。</p> <ul><li>RDB - <strong>RDB 即快照方式,它将某个时间点的所有 Redis 数据保存到一个经过压缩的二进制文件(RDB 文件)中</strong>。</li> <li>AOF - <code>AOF(Append Only File)</code> 是以文本日志形式将所有写命令追加到 AOF 文件的末尾,以此来记录数据的变化。当服务器重启的时候会重新载入和执行这些命令来恢复原始的数据。AOF 适合作为 <strong>热备</strong>。</li></ul> <blockquote><p>💡 更详细的特性及原理说明请参考:<a href="/db-tutorial/12.数据库/05.KV数据库/01.Redis/04.Redis持久化.html">Redis 持久化</a></p></blockquote> <h2 id="五、redis-事件"><a href="#五、redis-事件" class="header-anchor">#</a> 五、Redis 事件</h2> <p>Redis 服务器是一个事件驱动程序,服务器需要处理两类事件:</p> <ul><li><strong><code>文件事件(file event)</code></strong> - Redis 服务器通过套接字(Socket)与客户端或者其它服务器进行通信,文件事件就是对套接字操作的抽象。服务器与客户端(或其他的服务器)的通信会产生文件事件,而服务器通过监听并处理这些事件来完成一系列网络通信操作。</li> <li><strong><code>时间事件(time event)</code></strong> - Redis 服务器有一些操作需要在给定的时间点执行,时间事件是对这类定时操作的抽象。</li></ul> <h3 id="文件事件"><a href="#文件事件" class="header-anchor">#</a> 文件事件</h3> <p>Redis 基于 Reactor 模式开发了自己的网络时间处理器。</p> <ul><li>Redis 文件事件处理器使用 I/O 多路复用程序来同时监听多个套接字,并根据套接字目前执行的任务来为套接字关联不同的事件处理器。</li> <li>当被监听的套接字准备好执行连接应答、读取、写入、关闭操作时,与操作相对应的文件事件就会产生,这时文件事件处理器就会调用套接字之前关联好的事件处理器来处理这些事件。</li></ul> <p>虽然文件事件处理器以单线程方式运行,但通过使用 I/O 多路复用程序来监听多个套接字,文件事件处理器实现了高性能的网络通信模型。</p> <p>文件事件处理器有四个组成部分:套接字、I/O 多路复用程序、文件事件分派器、事件处理器。</p> <p><img src="https://raw.githubusercontent.com/dunwu/images/master/snap/20200130172525.png" alt="img"></p> <h3 id="时间事件"><a href="#时间事件" class="header-anchor">#</a> 时间事件</h3> <p>时间事件又分为:</p> <ul><li><strong>定时事件</strong>:是让一段程序在指定的时间之内执行一次;</li> <li><strong>周期性事件</strong>:是让一段程序每隔指定时间就执行一次。</li></ul> <p>Redis 将所有时间事件都放在一个无序链表中,每当时间事件执行器运行时,通过遍历整个链表查找出已到达的时间事件,并调用响应的事件处理器。</p> <h3 id="事件的调度与执行"><a href="#事件的调度与执行" class="header-anchor">#</a> 事件的调度与执行</h3> <p>服务器需要不断监听文件事件的套接字才能得到待处理的文件事件,但是不能一直监听,否则时间事件无法在规定的时间内执行,因此监听时间应该根据距离现在最近的时间事件来决定。</p> <p>事件调度与执行由 aeProcessEvents 函数负责,伪代码如下:</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">aeProcessEvents</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
|
||
|
||
<span class="token comment">## 获取到达时间离当前时间最接近的时间事件</span>
|
||
time_event <span class="token operator">=</span> aeSearchNearestTimer<span class="token punctuation">(</span><span class="token punctuation">)</span>
|
||
|
||
<span class="token comment">## 计算最接近的时间事件距离到达还有多少毫秒</span>
|
||
remaind_ms <span class="token operator">=</span> time_event<span class="token punctuation">.</span>when <span class="token operator">-</span> unix_ts_now<span class="token punctuation">(</span><span class="token punctuation">)</span>
|
||
|
||
<span class="token comment">## 如果事件已到达,那么 remaind_ms 的值可能为负数,将它设为 0</span>
|
||
<span class="token keyword">if</span> remaind_ms <span class="token operator"><</span> <span class="token number">0</span><span class="token punctuation">:</span>
|
||
remaind_ms <span class="token operator">=</span> <span class="token number">0</span>
|
||
|
||
<span class="token comment">## 根据 remaind_ms 的值,创建 timeval</span>
|
||
timeval <span class="token operator">=</span> create_timeval_with_ms<span class="token punctuation">(</span>remaind_ms<span class="token punctuation">)</span>
|
||
|
||
<span class="token comment">## 阻塞并等待文件事件产生,最大阻塞时间由传入的 timeval 决定</span>
|
||
aeApiPoll<span class="token punctuation">(</span>timeval<span class="token punctuation">)</span>
|
||
|
||
<span class="token comment">## 处理所有已产生的文件事件</span>
|
||
procesFileEvents<span class="token punctuation">(</span><span class="token punctuation">)</span>
|
||
|
||
<span class="token comment">## 处理所有已到达的时间事件</span>
|
||
processTimeEvents<span class="token punctuation">(</span><span class="token punctuation">)</span>
|
||
</code></pre></div><p>将 aeProcessEvents 函数置于一个循环里面,加上初始化和清理函数,就构成了 Redis 服务器的主函数,伪代码如下:</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
|
||
|
||
<span class="token comment">## 初始化服务器</span>
|
||
init_server<span class="token punctuation">(</span><span class="token punctuation">)</span>
|
||
|
||
<span class="token comment">## 一直处理事件,直到服务器关闭为止</span>
|
||
<span class="token keyword">while</span> server_is_not_shutdown<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
|
||
aeProcessEvents<span class="token punctuation">(</span><span class="token punctuation">)</span>
|
||
|
||
<span class="token comment">## 服务器关闭,执行清理操作</span>
|
||
clean_server<span class="token punctuation">(</span><span class="token punctuation">)</span>
|
||
</code></pre></div><p>从事件处理的角度来看,服务器运行流程如下:</p> <div align="center"><img src="https://raw.githubusercontent.com/dunwu/images/master/cs/database/redis/redis-event.png"></div> <h2 id="六、redis-事务"><a href="#六、redis-事务" class="header-anchor">#</a> 六、Redis 事务</h2> <blockquote><p><strong>Redis 提供的不是严格的事务,Redis 只保证串行执行命令,并且能保证全部执行,但是执行命令失败时并不会回滚,而是会继续执行下去</strong>。</p></blockquote> <p><code>MULTI</code> 、 <code>EXEC</code> 、 <code>DISCARD</code> 和 <code>WATCH</code> 是 Redis 事务相关的命令。</p> <p>事务可以一次执行多个命令, 并且有以下两个重要的保证:</p> <ul><li>事务是一个单独的隔离操作:事务中的所有命令都会序列化、按顺序地执行。事务在执行的过程中,不会被其他客户端发送来的命令请求所打断。</li> <li>事务是一个原子操作:事务中的命令要么全部被执行,要么全部都不执行。</li></ul> <h3 id="multi"><a href="#multi" class="header-anchor">#</a> MULTI</h3> <p><strong><a href="https://redis.io/commands/multi" target="_blank" rel="noopener noreferrer"><code>MULTI</code><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 命令用于开启一个事务,它总是返回 OK 。</strong></p> <p><code>MULTI</code> 执行之后, 客户端可以继续向服务器发送任意多条命令, 这些命令不会立即被执行, 而是被放到一个队列中, 当 EXEC 命令被调用时, 所有队列中的命令才会被执行。</p> <p>以下是一个事务例子, 它原子地增加了 foo 和 bar 两个键的值:</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token operator">></span> MULTI
|
||
OK
|
||
<span class="token operator">></span> INCR foo
|
||
QUEUED
|
||
<span class="token operator">></span> INCR bar
|
||
QUEUED
|
||
<span class="token operator">></span> EXEC
|
||
<span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>integer<span class="token punctuation">)</span> <span class="token number">1</span>
|
||
<span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>integer<span class="token punctuation">)</span> <span class="token number">1</span>
|
||
</code></pre></div><h3 id="exec"><a href="#exec" class="header-anchor">#</a> EXEC</h3> <p><strong><a href="https://redis.io/commands/exec" target="_blank" rel="noopener noreferrer"><code>EXEC</code><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 命令负责触发并执行事务中的所有命令。</strong></p> <ul><li>如果客户端在使用 <code>MULTI</code> 开启了一个事务之后,却因为断线而没有成功执行 <code>EXEC</code> ,那么事务中的所有命令都不会被执行。</li> <li>另一方面,如果客户端成功在开启事务之后执行 <code>EXEC</code> ,那么事务中的所有命令都会被执行。</li></ul> <h3 id="discard"><a href="#discard" class="header-anchor">#</a> DISCARD</h3> <p><strong>当执行 <a href="https://redis.io/commands/discard" target="_blank" rel="noopener noreferrer"><code>DISCARD</code><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 命令时, 事务会被放弃, 事务队列会被清空, 并且客户端会从事务状态中退出。</strong></p> <p>示例:</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token operator">></span> SET foo <span class="token number">1</span>
|
||
OK
|
||
<span class="token operator">></span> MULTI
|
||
OK
|
||
<span class="token operator">></span> INCR foo
|
||
QUEUED
|
||
<span class="token operator">></span> DISCARD
|
||
OK
|
||
<span class="token operator">></span> GET foo
|
||
<span class="token string">"1"</span>
|
||
</code></pre></div><h3 id="watch"><a href="#watch" class="header-anchor">#</a> WATCH</h3> <p><strong><a href="https://redis.io/commands/watch" target="_blank" rel="noopener noreferrer"><code>WATCH</code><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 命令可以为 Redis 事务提供 check-and-set (CAS)行为。</strong></p> <p>被 WATCH 的键会被监视,并会发觉这些键是否被改动过了。 如果有至少一个被监视的键在 EXEC 执行之前被修改了, 那么整个事务都会被取消, EXEC 返回 nil-reply 来表示事务已经失败。</p> <div class="language-python extra-class"><pre class="language-python"><code>WATCH mykey
|
||
val <span class="token operator">=</span> GET mykey
|
||
val <span class="token operator">=</span> val <span class="token operator">+</span> <span class="token number">1</span>
|
||
MULTI
|
||
SET mykey $val
|
||
EXEC
|
||
</code></pre></div><p>使用上面的代码, 如果在 WATCH 执行之后, EXEC 执行之前, 有其他客户端修改了 mykey 的值, 那么当前客户端的事务就会失败。 程序需要做的, 就是不断重试这个操作, 直到没有发生碰撞为止。</p> <p>这种形式的锁被称作乐观锁, 它是一种非常强大的锁机制。 并且因为大多数情况下, 不同的客户端会访问不同的键, 碰撞的情况一般都很少, 所以通常并不需要进行重试。</p> <p>WATCH 使得 EXEC 命令需要有条件地执行:事务只能在所有被监视键都没有被修改的前提下执行,如果这个前提不能满足的话,事务就不会被执行。</p> <p>WATCH 命令可以被调用多次。对键的监视从 WATCH 执行之后开始生效,直到调用 EXEC 为止。</p> <p>用户还可以在单个 WATCH 命令中监视任意多个键,例如:</p> <div class="language-python extra-class"><pre class="language-python"><code>redis<span class="token operator">></span> WATCH key1 key2 key3
|
||
OK
|
||
</code></pre></div><h4 id="取消-watch-的场景"><a href="#取消-watch-的场景" class="header-anchor">#</a> 取消 WATCH 的场景</h4> <p>当 EXEC 被调用时, 不管事务是否成功执行, 对所有键的监视都会被取消。</p> <p>另外, 当客户端断开连接时, 该客户端对键的监视也会被取消。</p> <p>使用无参数的 UNWATCH 命令可以手动取消对所有键的监视。 对于一些需要改动多个键的事务, 有时候程序需要同时对多个键进行加锁, 然后检查这些键的当前值是否符合程序的要求。 当值达不到要求时, 就可以使用 UNWATCH 命令来取消目前对键的监视, 中途放弃这个事务, 并等待事务的下次尝试。</p> <h4 id="使用-watch-创建原子操作"><a href="#使用-watch-创建原子操作" class="header-anchor">#</a> 使用 WATCH 创建原子操作</h4> <p>WATCH 可以用于创建 Redis 没有内置的原子操作。</p> <p>举个例子,以下代码实现了原创的 ZPOP 命令,它可以原子地弹出有序集合中分值(score)最小的元素:</p> <div class="language- extra-class"><pre class="language-text"><code>WATCH zset
|
||
element = ZRANGE zset 0 0
|
||
MULTI
|
||
ZREM zset element
|
||
EXEC
|
||
</code></pre></div><h3 id="rollback"><a href="#rollback" class="header-anchor">#</a> Rollback</h3> <p><strong>Redis 不支持回滚</strong>。Redis 不支持回滚的理由:</p> <ul><li>Redis 命令只会因为错误的语法而失败,或是命令用在了错误类型的键上面。</li> <li>因为不需要对回滚进行支持,所以 Redis 的内部可以保持简单且快速。</li></ul> <h2 id="七、redis-管道"><a href="#七、redis-管道" class="header-anchor">#</a> 七、Redis 管道</h2> <p>Redis 是一种基于 C/S 模型以及请求/响应协议的 TCP 服务。Redis 支持管道技术。管道技术允许请求以异步方式发送,即旧请求的应答还未返回的情况下,允许发送新请求。这种方式可以大大提高传输效率。</p> <p>在需要批量执行 Redis 命令时,如果一条一条执行,显然很低效。为了减少通信次数并降低延迟,可以使用 Redis 管道功能。Redis 的管道(pipeline)功能没有提供命令行支持,但是在各种语言版本的客户端中都有相应的实现。</p> <p>以 Jedis 为例:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token class-name">Pipeline</span> pipe <span class="token operator">=</span> conn<span class="token punctuation">.</span><span class="token function">pipelined</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
pipe<span class="token punctuation">.</span><span class="token function">multi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
pipe<span class="token punctuation">.</span><span class="token function">hset</span><span class="token punctuation">(</span><span class="token string">"login:"</span><span class="token punctuation">,</span> token<span class="token punctuation">,</span> user<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
pipe<span class="token punctuation">.</span><span class="token function">zadd</span><span class="token punctuation">(</span><span class="token string">"recent:"</span><span class="token punctuation">,</span> timestamp<span class="token punctuation">,</span> token<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
<span class="token keyword">if</span> <span class="token punctuation">(</span>item <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
||
pipe<span class="token punctuation">.</span><span class="token function">zadd</span><span class="token punctuation">(</span><span class="token string">"viewed:"</span> <span class="token operator">+</span> token<span class="token punctuation">,</span> timestamp<span class="token punctuation">,</span> item<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
pipe<span class="token punctuation">.</span><span class="token function">zremrangeByRank</span><span class="token punctuation">(</span><span class="token string">"viewed:"</span> <span class="token operator">+</span> token<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">26</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
pipe<span class="token punctuation">.</span><span class="token function">zincrby</span><span class="token punctuation">(</span><span class="token string">"viewed:"</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> item<span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
<span class="token punctuation">}</span>
|
||
pipe<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
|
||
</code></pre></div><blockquote><p>🔔 注意:使用管道发送命令时,Redis Server 会将部分请求放到缓存队列中(占用内存),执行完毕后一次性发送结果。如果需要发送大量的命令,会占用大量的内存,因此应该按照合理数量分批次的处理。</p></blockquote> <h2 id="八、redis-发布与订阅"><a href="#八、redis-发布与订阅" class="header-anchor">#</a> 八、Redis 发布与订阅</h2> <p>Redis 提供了 5 个发布与订阅命令:</p> <table><thead><tr><th>命令</th> <th>描述</th></tr></thead> <tbody><tr><td><code>SUBSCRIBE</code></td> <td><code>SUBSCRIBE channel [channel ...]</code>—订阅指定频道。</td></tr> <tr><td><code>UNSUBSCRIBE</code></td> <td><code>UNSUBSCRIBE [channel [channel ...]]</code>—取消订阅指定频道。</td></tr> <tr><td><code>PUBLISH</code></td> <td><code>PUBLISH channel message</code>—发送信息到指定的频道。</td></tr> <tr><td><code>PSUBSCRIBE</code></td> <td><code>PSUBSCRIBE pattern [pattern ...]</code>—订阅符合指定模式的频道。</td></tr> <tr><td><code>PUNSUBSCRIBE</code></td> <td><code>PUNSUBSCRIBE [pattern [pattern ...]]</code>—取消订阅符合指定模式的频道。</td></tr></tbody></table> <p>订阅者订阅了频道之后,发布者向频道发送字符串消息会被所有订阅者接收到。</p> <p>某个客户端使用 SUBSCRIBE 订阅一个频道,其它客户端可以使用 PUBLISH 向这个频道发送消息。</p> <p>发布与订阅模式和观察者模式有以下不同:</p> <ul><li>观察者模式中,观察者和主题都知道对方的存在;而在发布与订阅模式中,发布者与订阅者不知道对方的存在,它们之间通过频道进行通信。</li> <li>观察者模式是同步的,当事件触发时,主题会去调用观察者的方法;而发布与订阅模式是异步的;</li></ul> <hr> <p><strong><em>分割线以下为 Redis 集群功能特性</em></strong></p> <h2 id="九、redis-复制"><a href="#九、redis-复制" class="header-anchor">#</a> 九、Redis 复制</h2> <blockquote><p>关系型数据库通常会使用一个主服务器向多个从服务器发送更新,并使用从服务器来处理所有读请求,Redis 也采用了同样的方式来实现复制特性。</p></blockquote> <h3 id="旧版复制"><a href="#旧版复制" class="header-anchor">#</a> 旧版复制</h3> <p>Redis 2.8 版本以前的复制功能基于 <code>SYNC</code> 命令实现。</p> <p>Redis 的复制功能分为同步(sync)和命令传播(command propagate)两个操作:</p> <ul><li><strong><code>同步(sync)</code></strong> - 用于将从服务器的数据库状态更新至主服务器当前的数据库状态。</li> <li><strong><code>命令传播(command propagate)</code></strong> - 当主服务器的数据库状态被修改,导致主从数据库状态不一致时,让主从服务器的数据库重新回到一致状态。</li></ul> <p>这种方式存在缺陷:不能高效处理断线重连后的复制情况。</p> <h3 id="新版复制"><a href="#新版复制" class="header-anchor">#</a> 新版复制</h3> <p>Redis 2.8 版本以后的复制功能基于 <code>PSYNC</code> 命令实现。<code>PSYNC</code> 命令具有完整重同步和部分重同步两种模式。</p> <ul><li><strong><code>完整重同步(full resychronization)</code></strong> - 用于初次复制。执行步骤与 <code>SYNC</code> 命令基本一致。</li> <li><strong><code>部分重同步(partial resychronization)</code></strong> - 用于断线后重复制。<strong>如果条件允许,主服务器可以将主从服务器连接断开期间执行的写命令发送给从服务器</strong>,从服务器只需接收并执行这些写命令,即可将主从服务器的数据库状态保持一致。</li></ul> <h3 id="部分重同步"><a href="#部分重同步" class="header-anchor">#</a> 部分重同步</h3> <p>部分重同步有三个组成部分:</p> <ul><li>主从服务器的<strong>复制偏移量(replication offset)</strong></li> <li>主服务器的<strong>复制积压缓冲区(replication backlog)</strong></li> <li><strong>服务器的运行 ID</strong></li></ul> <h3 id="psync-命令"><a href="#psync-命令" class="header-anchor">#</a> PSYNC 命令</h3> <p>从服务器向要复制的主服务器发送 <code>PSYNC <runid> <offset></code> 命令</p> <ul><li>假如主从服务器的 <strong>master run id 相同</strong>,并且<strong>指定的偏移量(offset)在内存缓冲区中还有效</strong>,复制就会从上次中断的点开始继续。</li> <li>如果其中一个条件不满足,就会进行完全重新同步。</li></ul> <h3 id="心跳检测"><a href="#心跳检测" class="header-anchor">#</a> 心跳检测</h3> <p>主服务器通过向从服务传播命令来更新从服务器状态,保持主从数据一致。</p> <p>从服务器通过向主服务器发送命令 <code>REPLCONF ACK <replication_offset></code> 来进行心跳检测,以及命令丢失检测。</p> <blockquote><p>💡 更详细的特性及原理说明请参考:<a href="/db-tutorial/12.数据库/05.KV数据库/01.Redis/05.Redis复制.html">Redis 复制</a></p></blockquote> <h2 id="十、redis-哨兵"><a href="#十、redis-哨兵" class="header-anchor">#</a> 十、Redis 哨兵</h2> <p>Sentinel(哨兵)可以监听主服务器,并在主服务器进入下线状态时,自动从从服务器中选举出新的主服务器。</p> <blockquote><p>💡 更详细的特性及原理说明请参考:<a href="/db-tutorial/12.数据库/05.KV数据库/01.Redis/06.Redis哨兵.html">Redis 哨兵</a></p></blockquote> <h2 id="十一、redis-集群"><a href="#十一、redis-集群" class="header-anchor">#</a> 十一、Redis 集群</h2> <p>分片是将数据划分为多个部分的方法,可以将数据存储到多台机器里面,也可以从多台机器里面获取数据,这种方法在解决某些问题时可以获得线性级别的性能提升。</p> <p>假设有 4 个 Reids 实例 R0,R1,R2,R3,还有很多表示用户的键 user:1,user:2,... 等等,有不同的方式来选择一个指定的键存储在哪个实例中。最简单的方式是范围分片,例如用户 id 从 0~1000 的存储到实例 R0 中,用户 id 从 1001~2000 的存储到实例 R1 中,等等。但是这样需要维护一张映射范围表,维护操作代价很高。还有一种方式是哈希分片,使用 CRC32 哈希函数将键转换为一个数字,再对实例数量求模就能知道应该存储的实例。</p> <p>主要有三种分片方式:</p> <ul><li>客户端分片:客户端使用一致性哈希等算法决定键应当分布到哪个节点。</li> <li>代理分片:将客户端请求发送到代理上,由代理转发请求到正确的节点上。</li> <li>服务器分片:Redis Cluster(官方的 Redis 集群解决方案)。</li></ul> <h2 id="十二、redis-client"><a href="#十二、redis-client" class="header-anchor">#</a> 十二、Redis Client</h2> <p>Redis 社区中有多种编程语言的客户端,可以在这里查找合适的客户端:<a href="https://redis.io/clients" target="_blank" rel="noopener noreferrer">Redis 官方罗列的客户端清单<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>redis 官方推荐的 Java Redis Client:</p> <ul><li><a href="https://github.com/xetorthio/jedis" target="_blank" rel="noopener noreferrer">jedis<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> - 最流行的 Redis Java 客户端</li> <li><a href="https://github.com/redisson/redisson" target="_blank" rel="noopener noreferrer">redisson<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> - 额外提供了很多的分布式服务特性,如:分布式锁、分布式 Java 常用对象(BitSet、BlockingQueue、CountDownLatch 等)</li> <li><a href="https://github.com/lettuce-io/lettuce-core" target="_blank" rel="noopener noreferrer">lettuce<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> - Spring Boot 2.0 默认 Redis 客户端</li></ul> <h2 id="扩展阅读"><a href="#扩展阅读" class="header-anchor">#</a> 扩展阅读</h2> <blockquote><p>💡 Redis 常用于分布式缓存,有关缓存的特性和原理请参考:<a href="https://dunwu.github.io/design/distributed/%E5%88%86%E5%B8%83%E5%BC%8F%E7%BC%93%E5%AD%98.html" target="_blank" rel="noopener noreferrer">缓存基本原理<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote> <h2 id="参考资料"><a href="#参考资料" class="header-anchor">#</a> 参考资料</h2> <ul><li><strong>官网</strong> <ul><li><a href="https://redis.io/" target="_blank" rel="noopener noreferrer">Redis 官网<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/antirez/redis" target="_blank" rel="noopener noreferrer">Redis github<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="http://redis.cn/" target="_blank" rel="noopener noreferrer">Redis 官方文档中文版<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li> <li><strong>书籍</strong> <ul><li><a href="https://item.jd.com/11791607.html" target="_blank" rel="noopener noreferrer">《Redis 实战》<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://item.jd.com/11486101.html" target="_blank" rel="noopener noreferrer">《Redis 设计与实现》<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li> <li><strong>教程</strong> <ul><li><a href="http://redisdoc.com/" target="_blank" rel="noopener noreferrer">Redis 命令参考<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li> <li><strong>资源汇总</strong> <ul><li><a href="https://github.com/JamzyWang/awesome-redis" target="_blank" rel="noopener noreferrer">awesome-redis<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li> <li><strong>Redis Client</strong> <ul><li><a href="https://docs.spring.io/spring-data/redis/docs/1.8.13.RELEASE/reference/html/" target="_blank" rel="noopener noreferrer">spring-data-redis 官方文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95" target="_blank" rel="noopener noreferrer">redisson 官方文档(中文,略有滞后)<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/redisson/redisson/wiki/Table-of-Content" target="_blank" rel="noopener noreferrer">redisson 官方文档(英文)<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.jianshu.com/p/82f0d5abb002" target="_blank" rel="noopener noreferrer">CRUG | Redisson PRO vs. Jedis: Which Is Faster? 翻译<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://blog.csdn.net/everlasting_188/article/details/51073505" target="_blank" rel="noopener noreferrer">redis 分布锁 Redisson 性能测试<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li></ul></div></div> <div class="page-edit"><div class="edit-link"><a href="https://github.com/dunwu/db-tutorial/edit/master/docs/12.数据库/05.KV数据库/01.Redis/02.Redis应用指南.md" target="_blank" rel="noopener noreferrer">📝 帮助改善此页面!</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/db-tutorial/tags/?tag=%E6%95%B0%E6%8D%AE%E5%BA%93" title="标签">#数据库</a><a href="/db-tutorial/tags/?tag=KV%E6%95%B0%E6%8D%AE%E5%BA%93" title="标签">#KV数据库</a><a href="/db-tutorial/tags/?tag=Redis" title="标签">#Redis</a></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2024/10/09, 07:16:02</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/db-tutorial/pages/451b73/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">Redis 面试总结</div></a> <a href="/db-tutorial/pages/ed757c/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">Redis 数据类型和应用</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
|
||
←
|
||
<a href="/db-tutorial/pages/451b73/" class="prev">Redis 面试总结</a></span> <span class="next"><a href="/db-tutorial/pages/ed757c/">Redis 数据类型和应用</a>→
|
||
</span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/db-tutorial/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/db-tutorial/pages/b59ba2/"><div>
|
||
HBase Java API 管理功能
|
||
<!----></div></a> <span class="date">04-13</span></dt></dl><dl><dd>02</dd> <dt><a href="/db-tutorial/pages/ce5ca0/"><div>
|
||
HBase Java API 其他高级特性
|
||
<!----></div></a> <span class="date">03-31</span></dt></dl><dl><dd>03</dd> <dt><a href="/db-tutorial/pages/c8cfeb/"><div>
|
||
HBase 数据模型
|
||
<!----></div></a> <span class="date">03-16</span></dt></dl> <dl><dd></dd> <dt><a href="/db-tutorial/archives/" class="more">更多文章></a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:forbreak@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/dunwu" title="GitHub" target="_blank" class="iconfont icon-github"></a></div>
|
||
Theme by
|
||
<a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a>
|
||
| Copyright © 2019-2024
|
||
<span>钝悟(dunwu) | CC-BY-SA-4.0</span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
|
||
跟随系统
|
||
</li><li class="iconfont icon-rijianmoshi">
|
||
浅色模式
|
||
</li><li class="iconfont icon-yejianmoshi">
|
||
深色模式
|
||
</li><li class="iconfont icon-yuedu">
|
||
阅读模式
|
||
</li></ul></div></div> <!----> <!----> <div class="custom-html-window custom-html-window-rb" style="display:;"><div class="custom-wrapper"><span class="close-but">×</span> <div>
|
||
<div class="wwads-cn wwads-vertical windowRB" data-id="261" style="max-width:160px;
|
||
min-width: auto;min-height:auto;"></div>
|
||
<style>
|
||
.windowRB{ padding: 0;}
|
||
.windowRB .wwads-img{margin-top: 10px;}
|
||
.windowRB .wwads-content{margin: 0 10px 40px 10px;}
|
||
.custom-html-window-rb .close-but{
|
||
display: none;
|
||
}
|
||
</style>
|
||
</div></div></div></div><div class="global-ui"><div></div></div></div>
|
||
<script src="/db-tutorial/assets/js/app.be3f2e92.js" defer></script><script src="/db-tutorial/assets/js/2.aded268b.js" defer></script><script src="/db-tutorial/assets/js/45.68f2ab27.js" defer></script>
|
||
</body>
|
||
</html>
|