mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
WebFlux 使用 ReactiveRedisTemplate
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package org.spring.springboot.webflux.controller;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.ReactiveRedisTemplate;
|
||||
import org.springframework.data.redis.core.ReactiveValueOperations;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user