mirror of
https://github.com/zhwei820/learn.lianglianglee.com.git
synced 2025-09-23 19:56:41 +08:00
378 lines
18 KiB
HTML
378 lines
18 KiB
HTML
<!DOCTYPE html>
|
||
<!-- saved from url=(0046)https://kaiiiz.github.io/hexo-theme-book-demo/ -->
|
||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||
<head>
|
||
<head>
|
||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
|
||
<link rel="icon" href="/static/favicon.png">
|
||
<title>08 WebFlux 中 Redis 实现缓存.md.html</title>
|
||
<!-- Spectre.css framework -->
|
||
<link rel="stylesheet" href="/static/index.css">
|
||
<!-- theme css & js -->
|
||
<meta name="generator" content="Hexo 4.2.0">
|
||
</head>
|
||
<body>
|
||
<div class="book-container">
|
||
<div class="book-sidebar">
|
||
<div class="book-brand">
|
||
<a href="/">
|
||
<img src="/static/favicon.png">
|
||
<span>技术文章摘抄</span>
|
||
</a>
|
||
</div>
|
||
<div class="book-menu uncollapsible">
|
||
<ul class="uncollapsible">
|
||
<li><a href="/" class="current-tab">首页</a></li>
|
||
</ul>
|
||
<ul class="uncollapsible">
|
||
<li><a href="../">上一级</a></li>
|
||
</ul>
|
||
<ul class="uncollapsible">
|
||
<li>
|
||
<a href="/专栏/案例上手 Spring Boot WebFlux(完)/01 导读:课程概要.md.html">01 导读:课程概要</a>
|
||
</li>
|
||
<li>
|
||
<a href="/专栏/案例上手 Spring Boot WebFlux(完)/02 WebFlux 快速入门实践.md.html">02 WebFlux 快速入门实践</a>
|
||
</li>
|
||
<li>
|
||
<a href="/专栏/案例上手 Spring Boot WebFlux(完)/03 WebFlux Web CRUD 实践.md.html">03 WebFlux Web CRUD 实践</a>
|
||
</li>
|
||
<li>
|
||
<a href="/专栏/案例上手 Spring Boot WebFlux(完)/04 WebFlux 整合 MongoDB.md.html">04 WebFlux 整合 MongoDB</a>
|
||
</li>
|
||
<li>
|
||
<a href="/专栏/案例上手 Spring Boot WebFlux(完)/05 WebFlux 整合 Thymeleaf.md.html">05 WebFlux 整合 Thymeleaf</a>
|
||
</li>
|
||
<li>
|
||
<a href="/专栏/案例上手 Spring Boot WebFlux(完)/06 WebFlux 中 Thymeleaf 和 MongoDB 实践.md.html">06 WebFlux 中 Thymeleaf 和 MongoDB 实践</a>
|
||
</li>
|
||
<li>
|
||
<a href="/专栏/案例上手 Spring Boot WebFlux(完)/07 WebFlux 整合 Redis.md.html">07 WebFlux 整合 Redis</a>
|
||
</li>
|
||
<li>
|
||
<a class="current-tab" href="/专栏/案例上手 Spring Boot WebFlux(完)/08 WebFlux 中 Redis 实现缓存.md.html">08 WebFlux 中 Redis 实现缓存</a>
|
||
|
||
</li>
|
||
<li>
|
||
<a href="/专栏/案例上手 Spring Boot WebFlux(完)/09 WebFlux 中 WebSocket 实现通信.md.html">09 WebFlux 中 WebSocket 实现通信</a>
|
||
</li>
|
||
<li>
|
||
<a href="/专栏/案例上手 Spring Boot WebFlux(完)/10 WebFlux 集成测试及部署.md.html">10 WebFlux 集成测试及部署</a>
|
||
</li>
|
||
<li>
|
||
<a href="/专栏/案例上手 Spring Boot WebFlux(完)/11 WebFlux 实战图书管理系统.md.html">11 WebFlux 实战图书管理系统</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
<div class="sidebar-toggle" onclick="sidebar_toggle()" onmouseover="add_inner()" onmouseleave="remove_inner()">
|
||
<div class="sidebar-toggle-inner"></div>
|
||
</div>
|
||
<script>
|
||
function add_inner() {
|
||
let inner = document.querySelector('.sidebar-toggle-inner')
|
||
inner.classList.add('show')
|
||
}
|
||
function remove_inner() {
|
||
let inner = document.querySelector('.sidebar-toggle-inner')
|
||
inner.classList.remove('show')
|
||
}
|
||
function sidebar_toggle() {
|
||
let sidebar_toggle = document.querySelector('.sidebar-toggle')
|
||
let sidebar = document.querySelector('.book-sidebar')
|
||
let content = document.querySelector('.off-canvas-content')
|
||
if (sidebar_toggle.classList.contains('extend')) { // show
|
||
sidebar_toggle.classList.remove('extend')
|
||
sidebar.classList.remove('hide')
|
||
content.classList.remove('extend')
|
||
} else { // hide
|
||
sidebar_toggle.classList.add('extend')
|
||
sidebar.classList.add('hide')
|
||
content.classList.add('extend')
|
||
}
|
||
}
|
||
function open_sidebar() {
|
||
let sidebar = document.querySelector('.book-sidebar')
|
||
let overlay = document.querySelector('.off-canvas-overlay')
|
||
sidebar.classList.add('show')
|
||
overlay.classList.add('show')
|
||
}
|
||
function hide_canvas() {
|
||
let sidebar = document.querySelector('.book-sidebar')
|
||
let overlay = document.querySelector('.off-canvas-overlay')
|
||
sidebar.classList.remove('show')
|
||
overlay.classList.remove('show')
|
||
}
|
||
</script>
|
||
<div class="off-canvas-content">
|
||
<div class="columns">
|
||
<div class="column col-12 col-lg-12">
|
||
<div class="book-navbar">
|
||
<!-- For Responsive Layout -->
|
||
<header class="navbar">
|
||
<section class="navbar-section">
|
||
<a onclick="open_sidebar()">
|
||
<i class="icon icon-menu"></i>
|
||
</a>
|
||
</section>
|
||
</header>
|
||
</div>
|
||
<div class="book-content" style="max-width: 960px; margin: 0 auto;
|
||
overflow-x: auto;
|
||
overflow-y: hidden;">
|
||
<div class="book-post">
|
||
<p id="tip" align="center"></p>
|
||
<div><h1>08 WebFlux 中 Redis 实现缓存</h1>
|
||
<h3>前言</h3>
|
||
<p>首先,补充下上一篇的内容,RedisTemplate 实现操作 Redis,但操作是同步的,不是 Reactive 的。自然,支持 Reactive 的操作类为 ReactiveRedisTemplate,下面我们写个小案例。</p>
|
||
<h3>ReactiveRedisTemplate</h3>
|
||
<p>在上一篇工程中,新建 CityWebFluxReactiveController 类,路由为 /city2 开头。</p>
|
||
<pre><code>@RestController
|
||
@RequestMapping(value = "/city2")
|
||
public class CityWebFluxReactiveController {
|
||
@Autowired
|
||
private ReactiveRedisTemplate reactiveRedisTemplate;
|
||
@GetMapping(value = "/{id}")
|
||
public Mono<City> findCityById(@PathVariable("id") Long id) {
|
||
String key = "city_" + id;
|
||
ReactiveValueOperations<String, City> operations = reactiveRedisTemplate.opsForValue();
|
||
Mono<City> city = operations.get(key);
|
||
return city;
|
||
}
|
||
@PostMapping
|
||
public Mono<City> saveCity(@RequestBody City city) {
|
||
String key = "city_" + city.getId();
|
||
ReactiveValueOperations<String, City> operations = reactiveRedisTemplate.opsForValue();
|
||
return operations.getAndSet(key, city);
|
||
}
|
||
@DeleteMapping(value = "/{id}")
|
||
public Mono<Long> deleteCity(@PathVariable("id") Long id) {
|
||
String key = "city_" + id;
|
||
return reactiveRedisTemplate.delete(key);
|
||
}
|
||
}
|
||
</code></pre>
|
||
<ul>
|
||
<li>写法和以前保持一致,@Autowired 注入 ReactiveRedisTemplate 对象。</li>
|
||
<li>ReactiveValueOperations 是 String(或 value)的操作视图,操作视图还有 ReactiveHashOperations、ReactiveListOperations、ReactiveSetOperations 和 ReactiveZSetOperations 等。</li>
|
||
<li>不一样的是,操作视图 set 方法是操作 City 对象,但可以 get 回 Mono 或者 Flux 对象。</li>
|
||
</ul>
|
||
<h3>结构</h3>
|
||
<p>回到这个工程,新建一个工程编写整合 Redis 实现缓存案例,工程如图:</p>
|
||
<p><img src="assets/3b4f95fda4771ca70c5bbc644f82a0c01525318.png" alt="png" /></p>
|
||
<p>目录核心如下:</p>
|
||
<ul>
|
||
<li>pom.xml maven 配置</li>
|
||
<li>application.properties 配置文件</li>
|
||
<li>domain 实体类</li>
|
||
<li>dao mongodb数据操作层</li>
|
||
<li>handler 业务层,本文要点</li>
|
||
<li>controller 控制层</li>
|
||
</ul>
|
||
<p><a href="https://github.com/JeffLi1993/springboot-learning-example">单击这里查看源代码</a>。</p>
|
||
<h3>控制层 CityWebFluxController</h3>
|
||
<p>代码如下:</p>
|
||
<pre><code>@RestController
|
||
@RequestMapping(value = "/city")
|
||
public class CityWebFluxController {
|
||
@Autowired
|
||
private CityHandler cityHandler;
|
||
@GetMapping(value = "/{id}")
|
||
public Mono<City> findCityById(@PathVariable("id") Long id) {
|
||
return cityHandler.findCityById(id);
|
||
}
|
||
@GetMapping()
|
||
public Flux<City> findAllCity() {
|
||
return cityHandler.findAllCity();
|
||
}
|
||
@PostMapping()
|
||
public Mono<City> saveCity(@RequestBody City city) {
|
||
return cityHandler.save(city);
|
||
}
|
||
@PutMapping()
|
||
public Mono<City> modifyCity(@RequestBody City city) {
|
||
return cityHandler.modifyCity(city);
|
||
}
|
||
@DeleteMapping(value = "/{id}")
|
||
public Mono<Long> deleteCity(@PathVariable("id") Long id) {
|
||
return cityHandler.deleteCity(id);
|
||
}
|
||
}
|
||
</code></pre>
|
||
<h3>CityHandler 业务层</h3>
|
||
<p>目前,@Cacheable 等注解形式实现缓存没有很好的集成,二者 Mono / Flux 对象没有实现 Serializable,无法通过默认序列化器,解决方式是需要自定义序列化,这里通过手动方式与 Redis 手动集成,并实现缓存策略。</p>
|
||
<p><a href="http://coolshell.cn/articles/17416.html">参考《缓存更新的套路》</a>,缓存更新的模式有四种:Cache aside、Read through、Write through、Write behind caching。</p>
|
||
<p>这里使用的是 Cache Aside 策略,从三个维度(摘自耗子叔叔博客):</p>
|
||
<ul>
|
||
<li>失效:应用程序先从 Cache 取数据,没有得到,则从数据库中取数据,成功后,放到缓存中。</li>
|
||
<li>命中:应用程序从 Cache 中取数据,取到后返回。</li>
|
||
<li>更新:先把数据存到数据库中,成功后,再让缓存失效。</li>
|
||
</ul>
|
||
<p>代码如下:</p>
|
||
<pre><code>@Component
|
||
public class CityHandler {
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CityHandler.class);
|
||
@Autowired
|
||
private RedisTemplate redisTemplate;
|
||
private final CityRepository cityRepository;
|
||
@Autowired
|
||
public CityHandler(CityRepository cityRepository) {
|
||
this.cityRepository = cityRepository;
|
||
}
|
||
public Mono<City> save(City city) {
|
||
return cityRepository.save(city);
|
||
}
|
||
public Mono<City> findCityById(Long id) {
|
||
// 从缓存中获取城市信息
|
||
String key = "city_" + id;
|
||
ValueOperations<String, City> operations = redisTemplate.opsForValue();
|
||
// 缓存存在
|
||
boolean hasKey = redisTemplate.hasKey(key);
|
||
if (hasKey) {
|
||
City city = operations.get(key);
|
||
LOGGER.info("CityHandler.findCityById() : 从缓存中获取了城市 >> " + city.toString());
|
||
return Mono.create(cityMonoSink -> cityMonoSink.success(city));
|
||
}
|
||
// 从 MongoDB 中获取城市信息
|
||
Mono<City> cityMono = cityRepository.findById(id);
|
||
if (cityMono == null)
|
||
return cityMono;
|
||
// 插入缓存
|
||
cityMono.subscribe(cityObj -> {
|
||
operations.set(key, cityObj);
|
||
LOGGER.info("CityHandler.findCityById() : 城市插入缓存 >> " + cityObj.toString());
|
||
});
|
||
return cityMono;
|
||
}
|
||
public Flux<City> findAllCity() {
|
||
return cityRepository.findAll().cache();
|
||
}
|
||
public Mono<City> modifyCity(City city) {
|
||
Mono<City> cityMono = cityRepository.save(city);
|
||
// 缓存存在,删除缓存
|
||
String key = "city_" + city.getId();
|
||
boolean hasKey = redisTemplate.hasKey(key);
|
||
if (hasKey) {
|
||
redisTemplate.delete(key);
|
||
LOGGER.info("CityHandler.modifyCity() : 从缓存中删除城市 ID >> " + city.getId());
|
||
}
|
||
return cityMono;
|
||
}
|
||
public Mono<Long> deleteCity(Long id) {
|
||
cityRepository.deleteById(id);
|
||
// 缓存存在,删除缓存
|
||
String key = "city_" + id;
|
||
boolean hasKey = redisTemplate.hasKey(key);
|
||
if (hasKey) {
|
||
redisTemplate.delete(key);
|
||
LOGGER.info("CityHandler.deleteCity() : 从缓存中删除城市 ID >> " + id);
|
||
}
|
||
return Mono.create(cityMonoSink -> cityMonoSink.success(id));
|
||
}
|
||
}
|
||
</code></pre>
|
||
<p>首先这里注入了 RedisTemplate 对象,联想到 Spring 的 JdbcTemplate ,RedisTemplate 封装了 RedisConnection,具有连接管理,序列化和 Redis 操作等功能,还有针对 String 的支持对象 StringRedisTemplate。</p>
|
||
<p>回到更新缓存的逻辑。</p>
|
||
<p>a. findCityById 获取城市逻辑:</p>
|
||
<ul>
|
||
<li>如果缓存存在,从缓存中获取城市信息;</li>
|
||
<li>如果缓存不存在,从 DB 中获取城市信息,然后插入缓存。</li>
|
||
</ul>
|
||
<p>b. deleteCity 删除 / modifyCity 更新城市逻辑:</p>
|
||
<ul>
|
||
<li>如果缓存存在,删除;</li>
|
||
<li>如果缓存不存在,不操作。</li>
|
||
</ul>
|
||
<h3>运行工程</h3>
|
||
<p>一个操作 Redis 工程就开发完毕了,下面运行工程验证下。使用 IDEA 右侧工具栏,点击 Maven Project Tab,点击使用下 Maven 插件的 install 命令;或者使用命令行的形式,在工程根目录下,执行 Maven 清理和安装工程的指令:</p>
|
||
<pre><code>cd springboot-webflux-7-redis-cache
|
||
mvn clean install
|
||
</code></pre>
|
||
<p>在控制台中看到成功的输出:</p>
|
||
<pre><code>... 省略
|
||
[INFO] ------------------------------------------------------------------------
|
||
[INFO] BUILD SUCCESS
|
||
[INFO] ------------------------------------------------------------------------
|
||
[INFO] Total time: 01:30 min
|
||
[INFO] Finished at: 2018-10-15T10:00:54+08:00
|
||
[INFO] Final Memory: 31M/174M
|
||
[INFO] ------------------------------------------------------------------------
|
||
</code></pre>
|
||
<p>在 IDEA 中执行 Application 类启动,任意正常模式或者 Debug 模式,可以在控制台看到成功运行的输出:</p>
|
||
<pre><code>... 省略
|
||
2018-04-10 08:43:39.932 INFO 2052 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on /0:0:0:0:0:0:0:0:8080
|
||
2018-04-10 08:43:39.935 INFO 2052 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
|
||
2018-04-10 08:43:39.960 INFO 2052 --- [ main] org.spring.springboot.Application : Started Application in 6.547 seconds (JVM running for 9.851)
|
||
</code></pre>
|
||
<p>打开 POST MAN 工具,开发必备。进行下面操作:</p>
|
||
<p><strong>新增城市信息 POST http://127.0.0.1:8080/city</strong></p>
|
||
<p><img src="assets/f69fa5b09730de686ef4837824da48e71523883-1608971278890.png" alt="png" /></p>
|
||
<p><strong>获取城市信息 GET http://127.0.0.1:8080/city/2</strong></p>
|
||
<p><img src="assets/2e129648b8e574a54ff7940f00f1dc591524819-1608971278898.png" alt="png" /></p>
|
||
<p>再请求一次,获取城市信息会发现数据获取的耗时快了很多,服务端 Console 输出的日志:</p>
|
||
<pre><code>2017-04-13 18:29:00.273 INFO 13038 --- [nio-8080-exec-1] findCityById() : 城市插入缓存 >> City{id=12, provinceId=3, cityName='三亚', description='水好,天蓝'}
|
||
2017-04-13 18:29:03.145 INFO 13038 --- [nio-8080-exec-2] findCityById() : 从缓存中获取了城市 >> City{id=12, provinceId=3, cityName='三亚', description='水好,天蓝'}
|
||
</code></pre>
|
||
<p>可见,第一次是从数据库 MongoDB 获取数据,并插入缓存,第二次直接从缓存中取。</p>
|
||
<p>更新 / 删除城市信息,这两种操作中,如果缓存有对应的数据,则删除缓存。服务端 Console 输出的日志:</p>
|
||
<pre><code>2017-04-13 18:29:52.248 INFO 13038 --- [nio-8080-exec-9] deleteCity() : 从缓存中删除城市 ID >> 12
|
||
</code></pre>
|
||
<h3>总结</h3>
|
||
<p>这一讲,主要补充了 Redis 对响应式的支持操作,以及缓存更新策略及实际应用小例子。</p>
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<div style="float: left">
|
||
<a href="/专栏/案例上手 Spring Boot WebFlux(完)/07 WebFlux 整合 Redis.md.html">上一页</a>
|
||
</div>
|
||
<div style="float: right">
|
||
<a href="/专栏/案例上手 Spring Boot WebFlux(完)/09 WebFlux 中 WebSocket 实现通信.md.html">下一页</a>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<a class="off-canvas-overlay" onclick="hide_canvas()"></a>
|
||
</div>
|
||
<script defer src="https://static.cloudflareinsights.com/beacon.min.js/v652eace1692a40cfa3763df669d7439c1639079717194" integrity="sha512-Gi7xpJR8tSkrpF7aordPZQlW2DLtzUlZcumS8dMQjwDHEnw9I7ZLyiOj/6tZStRBGtGgN6ceN6cMH8z7etPGlw==" data-cf-beacon='{"rayId":"7099793bdb0e3cfa","version":"2021.12.0","r":1,"token":"1f5d475227ce4f0089a7cff1ab17c0f5","si":100}' crossorigin="anonymous"></script>
|
||
</body>
|
||
<!-- Global site tag (gtag.js) - Google Analytics -->
|
||
<script async src="https://www.googletagmanager.com/gtag/js?id=G-NPSEEVD756"></script>
|
||
<script>
|
||
window.dataLayer = window.dataLayer || [];
|
||
function gtag() {
|
||
dataLayer.push(arguments);
|
||
}
|
||
gtag('js', new Date());
|
||
gtag('config', 'G-NPSEEVD756');
|
||
var path = window.location.pathname
|
||
var cookie = getCookie("lastPath");
|
||
console.log(path)
|
||
if (path.replace("/", "") === "") {
|
||
if (cookie.replace("/", "") !== "") {
|
||
console.log(cookie)
|
||
document.getElementById("tip").innerHTML = "<a href='" + cookie + "'>跳转到上次进度</a>"
|
||
}
|
||
} else {
|
||
setCookie("lastPath", path)
|
||
}
|
||
function setCookie(cname, cvalue) {
|
||
var d = new Date();
|
||
d.setTime(d.getTime() + (180 * 24 * 60 * 60 * 1000));
|
||
var expires = "expires=" + d.toGMTString();
|
||
document.cookie = cname + "=" + cvalue + "; " + expires + ";path = /";
|
||
}
|
||
function getCookie(cname) {
|
||
var name = cname + "=";
|
||
var ca = document.cookie.split(';');
|
||
for (var i = 0; i < ca.length; i++) {
|
||
var c = ca[i].trim();
|
||
if (c.indexOf(name) === 0) return c.substring(name.length, c.length);
|
||
}
|
||
return "";
|
||
}
|
||
</script>
|
||
</html>
|