|
@@ -0,0 +1,58 @@
|
|
1
|
+package com.yaozhitech.spring5.controller;
|
|
2
|
+
|
|
3
|
+import java.util.concurrent.TimeUnit;
|
|
4
|
+
|
|
5
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
6
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
7
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
8
|
+import org.springframework.web.bind.annotation.DeleteMapping;
|
|
9
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
10
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
11
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
12
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
13
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
14
|
+import org.springframework.web.bind.annotation.RestController;
|
|
15
|
+
|
|
16
|
+import com.yaozhitech.spring5.domain.City;
|
|
17
|
+
|
|
18
|
+import reactor.core.publisher.Mono;
|
|
19
|
+
|
|
20
|
+@RestController
|
|
21
|
+@RequestMapping(value = "/city")
|
|
22
|
+public class CityWebFluxController {
|
|
23
|
+
|
|
24
|
+ @Autowired
|
|
25
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
26
|
+
|
|
27
|
+ @GetMapping(value = "/{id}")
|
|
28
|
+ public Mono<City> findCityById(@PathVariable("id") Long id) {
|
|
29
|
+ String key = "city_" + id;
|
|
30
|
+ ValueOperations<String, Object> operations = redisTemplate.opsForValue();
|
|
31
|
+ boolean hasKey = redisTemplate.hasKey(key);
|
|
32
|
+ City city = (City) operations.get(key);
|
|
33
|
+
|
|
34
|
+ if (!hasKey) {
|
|
35
|
+ return Mono.create(monoSink -> monoSink.success(null));
|
|
36
|
+ }
|
|
37
|
+ return Mono.create(monoSink -> monoSink.success(city));
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ @PostMapping()
|
|
41
|
+ public Mono<City> saveCity(@RequestBody City city) {
|
|
42
|
+ String key = "city_" + city.getId();
|
|
43
|
+ ValueOperations<String, Object> operations = redisTemplate.opsForValue();
|
|
44
|
+ operations.set(key, city, 160, TimeUnit.SECONDS);
|
|
45
|
+
|
|
46
|
+ return Mono.create(monoSink -> monoSink.success(city));
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ @DeleteMapping(value = "/{id}")
|
|
50
|
+ public Mono<Long> deleteCity(@PathVariable("id") Long id) {
|
|
51
|
+ String key = "city_" + id;
|
|
52
|
+ boolean hasKey = redisTemplate.hasKey(key);
|
|
53
|
+ if (hasKey) {
|
|
54
|
+ redisTemplate.delete(key);
|
|
55
|
+ }
|
|
56
|
+ return Mono.create(monoSink -> monoSink.success(id));
|
|
57
|
+ }
|
|
58
|
+}
|