mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
WebFlux 整合 Redis 实现缓存
This commit is contained in:
53
springboot-webflux-7-redis-cache/pom.xml
Executable file
53
springboot-webflux-7-redis-cache/pom.xml
Executable file
@@ -0,0 +1,53 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>springboot</groupId>
|
||||||
|
<artifactId>springboot-webflux-7-redis-cache</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>springboot-webflux-7-redis-cache :: Spring Boot WebFlux 整合 Redis 实现缓存</name>
|
||||||
|
|
||||||
|
<!-- Spring Boot 启动父依赖 -->
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.0.1.RELEASE</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- Spring Boot Web Flux 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Boot 响应式 MongoDB 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Boot 响应式 Redis 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Boot Test 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Junit -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package org.spring.springboot;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spring Boot 应用启动类
|
||||||
|
*/
|
||||||
|
// Spring Boot 应用的标识
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 程序启动入口
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package org.spring.springboot.dao;
|
||||||
|
|
||||||
|
import org.spring.springboot.domain.City;
|
||||||
|
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface CityRepository extends ReactiveMongoRepository<City, Long> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package org.spring.springboot.domain;
|
||||||
|
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 城市实体类
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class City implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -2081742442561524068L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 城市编号
|
||||||
|
*/
|
||||||
|
@Id
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 省份编号
|
||||||
|
*/
|
||||||
|
private Long provinceId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 城市名称
|
||||||
|
*/
|
||||||
|
private String cityName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 描述
|
||||||
|
*/
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getProvinceId() {
|
||||||
|
return provinceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProvinceId(Long provinceId) {
|
||||||
|
this.provinceId = provinceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCityName() {
|
||||||
|
return cityName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCityName(String cityName) {
|
||||||
|
this.cityName = cityName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "City{" +
|
||||||
|
"id=" + id +
|
||||||
|
", provinceId=" + provinceId +
|
||||||
|
", cityName='" + cityName + '\'' +
|
||||||
|
", description='" + description + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
package org.spring.springboot.handler;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.spring.springboot.dao.CityRepository;
|
||||||
|
import org.spring.springboot.domain.City;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.core.ValueOperations;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@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) {
|
||||||
|
|
||||||
|
// 缓存存在,删除缓存
|
||||||
|
String key = "city_" + city.getId();
|
||||||
|
boolean hasKey = redisTemplate.hasKey(key);
|
||||||
|
if (hasKey) {
|
||||||
|
redisTemplate.delete(key);
|
||||||
|
|
||||||
|
LOGGER.info("CityHandler.modifyCity() : 从缓存中删除城市 ID >> " + city.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
return cityRepository.save(city).cache();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mono<Long> deleteCity(Long id) {
|
||||||
|
|
||||||
|
// 缓存存在,删除缓存
|
||||||
|
String key = "city_" + id;
|
||||||
|
boolean hasKey = redisTemplate.hasKey(key);
|
||||||
|
if (hasKey) {
|
||||||
|
redisTemplate.delete(key);
|
||||||
|
|
||||||
|
LOGGER.info("CityHandler.deleteCity() : 从缓存中删除城市 ID >> " + id);
|
||||||
|
}
|
||||||
|
|
||||||
|
cityRepository.deleteById(id);
|
||||||
|
return Mono.create(cityMonoSink -> cityMonoSink.success(id));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package org.spring.springboot.webflux.controller;
|
||||||
|
|
||||||
|
import org.spring.springboot.domain.City;
|
||||||
|
import org.spring.springboot.handler.CityHandler;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
## Redis 配置
|
||||||
|
## Redis服务器地址
|
||||||
|
spring.redis.host=127.0.0.1
|
||||||
|
## Redis服务器连接端口
|
||||||
|
spring.redis.port=6379
|
||||||
|
## Redis服务器连接密码(默认为空)
|
||||||
|
spring.redis.password=
|
||||||
|
# 连接超时时间(毫秒)
|
||||||
|
spring.redis.timeout=5000
|
||||||
|
|
||||||
|
## MongoDB
|
||||||
|
spring.data.mongodb.host=localhost
|
||||||
|
spring.data.mongodb.database=admin
|
||||||
|
spring.data.mongodb.port=27017
|
||||||
|
spring.data.mongodb.username=admin
|
||||||
|
spring.data.mongodb.password=admin
|
||||||
Reference in New Issue
Block a user