Skip to content

Commit

Permalink
WebFlux 使用 ReactiveRedisTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
liqiangqiang committed May 2, 2018
1 parent f2c7644 commit 27dea59
Showing 1 changed file with 37 additions and 0 deletions.
@@ -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);
}
}

0 comments on commit 27dea59

Please sign in to comment.