Skip to content

Commit 27dea59

Browse files
author
liqiangqiang
committed
WebFlux 使用 ReactiveRedisTemplate
1 parent f2c7644 commit 27dea59

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.spring.springboot.webflux.controller;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.data.redis.core.ReactiveRedisTemplate;
6+
import org.springframework.data.redis.core.ReactiveValueOperations;
7+
import org.springframework.web.bind.annotation.*;
8+
import reactor.core.publisher.Mono;
9+
10+
@RestController
11+
@RequestMapping(value = "/city2")
12+
public class CityWebFluxReactiveController {
13+
14+
@Autowired
15+
private ReactiveRedisTemplate reactiveRedisTemplate;
16+
17+
@GetMapping(value = "/{id}")
18+
public Mono<City> findCityById(@PathVariable("id") Long id) {
19+
String key = "city_" + id;
20+
ReactiveValueOperations<String, City> operations = reactiveRedisTemplate.opsForValue();
21+
Mono<City> city = operations.get(key);
22+
return city;
23+
}
24+
25+
@PostMapping
26+
public Mono<City> saveCity(@RequestBody City city) {
27+
String key = "city_" + city.getId();
28+
ReactiveValueOperations<String, City> operations = reactiveRedisTemplate.opsForValue();
29+
return operations.getAndSet(key, city);
30+
}
31+
32+
@DeleteMapping(value = "/{id}")
33+
public Mono<Long> deleteCity(@PathVariable("id") Long id) {
34+
String key = "city_" + id;
35+
return reactiveRedisTemplate.delete(key);
36+
}
37+
}

0 commit comments

Comments
 (0)