Skip to content

Commit e327731

Browse files
author
liqiangqiang
committed
WebFlux 整合 Redis 实现缓存
1 parent 27dea59 commit e327731

File tree

7 files changed

+310
-0
lines changed

7 files changed

+310
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>springboot</groupId>
7+
<artifactId>springboot-webflux-7-redis-cache</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>springboot-webflux-7-redis-cache :: Spring Boot WebFlux 整合 Redis 实现缓存</name>
10+
11+
<!-- Spring Boot 启动父依赖 -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>2.0.1.RELEASE</version>
16+
</parent>
17+
18+
<dependencies>
19+
20+
<!-- Spring Boot Web Flux 依赖 -->
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-webflux</artifactId>
24+
</dependency>
25+
26+
<!-- Spring Boot 响应式 MongoDB 依赖 -->
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
30+
</dependency>
31+
32+
<!-- Spring Boot 响应式 Redis 依赖 -->
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
36+
</dependency>
37+
38+
<!-- Spring Boot Test 依赖 -->
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
45+
<!-- Junit -->
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.12</version>
50+
</dependency>
51+
</dependencies>
52+
53+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.spring.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Spring Boot 应用启动类
8+
*/
9+
// Spring Boot 应用的标识
10+
@SpringBootApplication
11+
public class Application {
12+
13+
public static void main(String[] args) {
14+
// 程序启动入口
15+
SpringApplication.run(Application.class, args);
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.spring.springboot.dao;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface CityRepository extends ReactiveMongoRepository<City, Long> {
9+
10+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.spring.springboot.domain;
2+
3+
import org.springframework.data.annotation.Id;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* 城市实体类
9+
*
10+
*/
11+
public class City implements Serializable {
12+
13+
private static final long serialVersionUID = -2081742442561524068L;
14+
15+
/**
16+
* 城市编号
17+
*/
18+
@Id
19+
private Long id;
20+
21+
/**
22+
* 省份编号
23+
*/
24+
private Long provinceId;
25+
26+
/**
27+
* 城市名称
28+
*/
29+
private String cityName;
30+
31+
/**
32+
* 描述
33+
*/
34+
private String description;
35+
36+
public Long getId() {
37+
return id;
38+
}
39+
40+
public void setId(Long id) {
41+
this.id = id;
42+
}
43+
44+
public Long getProvinceId() {
45+
return provinceId;
46+
}
47+
48+
public void setProvinceId(Long provinceId) {
49+
this.provinceId = provinceId;
50+
}
51+
52+
public String getCityName() {
53+
return cityName;
54+
}
55+
56+
public void setCityName(String cityName) {
57+
this.cityName = cityName;
58+
}
59+
60+
public String getDescription() {
61+
return description;
62+
}
63+
64+
public void setDescription(String description) {
65+
this.description = description;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "City{" +
71+
"id=" + id +
72+
", provinceId=" + provinceId +
73+
", cityName='" + cityName + '\'' +
74+
", description='" + description + '\'' +
75+
'}';
76+
}
77+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.spring.springboot.handler;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.spring.springboot.dao.CityRepository;
6+
import org.spring.springboot.domain.City;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.data.redis.core.RedisTemplate;
9+
import org.springframework.data.redis.core.ValueOperations;
10+
import org.springframework.stereotype.Component;
11+
import reactor.core.publisher.Flux;
12+
import reactor.core.publisher.Mono;
13+
14+
@Component
15+
public class CityHandler {
16+
17+
private static final Logger LOGGER = LoggerFactory.getLogger(CityHandler.class);
18+
19+
@Autowired
20+
private RedisTemplate redisTemplate;
21+
22+
private final CityRepository cityRepository;
23+
24+
@Autowired
25+
public CityHandler(CityRepository cityRepository) {
26+
this.cityRepository = cityRepository;
27+
}
28+
29+
public Mono<City> save(City city) {
30+
return cityRepository.save(city);
31+
}
32+
33+
34+
public Mono<City> findCityById(Long id) {
35+
36+
// 从缓存中获取城市信息
37+
String key = "city_" + id;
38+
ValueOperations<String, City> operations = redisTemplate.opsForValue();
39+
40+
// 缓存存在
41+
boolean hasKey = redisTemplate.hasKey(key);
42+
if (hasKey) {
43+
City city = operations.get(key);
44+
45+
LOGGER.info("CityHandler.findCityById() : 从缓存中获取了城市 >> " + city.toString());
46+
return Mono.create(cityMonoSink -> cityMonoSink.success(city));
47+
}
48+
49+
// 从 MongoDB 中获取城市信息
50+
Mono<City> cityMono = cityRepository.findById(id);
51+
52+
if (cityMono == null)
53+
return cityMono;
54+
55+
// 插入缓存
56+
cityMono.subscribe(cityObj -> {
57+
operations.set(key, cityObj);
58+
LOGGER.info("CityHandler.findCityById() : 城市插入缓存 >> " + cityObj.toString());
59+
});
60+
61+
return cityMono;
62+
}
63+
64+
public Flux<City> findAllCity() {
65+
return cityRepository.findAll().cache();
66+
}
67+
68+
public Mono<City> modifyCity(City city) {
69+
70+
// 缓存存在,删除缓存
71+
String key = "city_" + city.getId();
72+
boolean hasKey = redisTemplate.hasKey(key);
73+
if (hasKey) {
74+
redisTemplate.delete(key);
75+
76+
LOGGER.info("CityHandler.modifyCity() : 从缓存中删除城市 ID >> " + city.getId());
77+
}
78+
79+
return cityRepository.save(city).cache();
80+
}
81+
82+
public Mono<Long> deleteCity(Long id) {
83+
84+
// 缓存存在,删除缓存
85+
String key = "city_" + id;
86+
boolean hasKey = redisTemplate.hasKey(key);
87+
if (hasKey) {
88+
redisTemplate.delete(key);
89+
90+
LOGGER.info("CityHandler.deleteCity() : 从缓存中删除城市 ID >> " + id);
91+
}
92+
93+
cityRepository.deleteById(id);
94+
return Mono.create(cityMonoSink -> cityMonoSink.success(id));
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.spring.springboot.webflux.controller;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.spring.springboot.handler.CityHandler;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.publisher.Mono;
9+
10+
@RestController
11+
@RequestMapping(value = "/city")
12+
public class CityWebFluxController {
13+
14+
@Autowired
15+
private CityHandler cityHandler;
16+
17+
@GetMapping(value = "/{id}")
18+
public Mono<City> findCityById(@PathVariable("id") Long id) {
19+
return cityHandler.findCityById(id);
20+
}
21+
22+
@GetMapping()
23+
public Flux<City> findAllCity() {
24+
return cityHandler.findAllCity();
25+
}
26+
27+
@PostMapping()
28+
public Mono<City> saveCity(@RequestBody City city) {
29+
return cityHandler.save(city);
30+
}
31+
32+
@PutMapping()
33+
public Mono<City> modifyCity(@RequestBody City city) {
34+
return cityHandler.modifyCity(city);
35+
}
36+
37+
@DeleteMapping(value = "/{id}")
38+
public Mono<Long> deleteCity(@PathVariable("id") Long id) {
39+
return cityHandler.deleteCity(id);
40+
}
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Redis 配置
2+
## Redis服务器地址
3+
spring.redis.host=127.0.0.1
4+
## Redis服务器连接端口
5+
spring.redis.port=6379
6+
## Redis服务器连接密码(默认为空)
7+
spring.redis.password=
8+
# 连接超时时间(毫秒)
9+
spring.redis.timeout=5000
10+
11+
## MongoDB
12+
spring.data.mongodb.host=localhost
13+
spring.data.mongodb.database=admin
14+
spring.data.mongodb.port=27017
15+
spring.data.mongodb.username=admin
16+
spring.data.mongodb.password=admin

0 commit comments

Comments
 (0)