<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
server:
port: 8080
spring:
application:
name: webflux
redis:
host: 127.0.0.1
port: 6379
password: pwd2020
timeout: 5000
lettuce:
pool:
max-active: 200
max-idle: 20
min-idle: 5
max-wait: 1000
@Bean
public ReactiveRedisTemplate<String, String> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
ReactiveRedisTemplate<String, String> reactiveRedisTemplate = new ReactiveRedisTemplate<>(factory,RedisSerializationContext.string());
return reactiveRedisTemplate;
}
@Service
public class RedisServiceImpl implements RedisService {
@Autowired
private ReactiveRedisTemplate<String, String> redisTemplate;
@Override
public Mono<String> get(String key) {
ReactiveValueOperations<String, String> operations = redisTemplate.opsForValue();
return operations.get(key);
}
@Override
public Mono<String> set(String key,User user) {
ReactiveValueOperations<String, String> operations = redisTemplate.opsForValue();
return operations.getAndSet(key, JSON.toJSONString(user));
}
@Override
public Mono<Boolean> delete(String key) {
ReactiveValueOperations<String, String> operations = redisTemplate.opsForValue();
return operations.delete(key);
}
@Override
public Mono<String> update(String key,User user) {
ReactiveValueOperations<String, String> operations = redisTemplate.opsForValue();
return operations.getAndSet(key, JSON.toJSONString(user));
}
@Override
public Flux<String> all(String key) {
ReactiveListOperations<String, String> operations = redisTemplate.opsForList();
return operations.range(key, 0, -1);
}
@Override
public Mono<Long> push(String key,List<String> list) {
ReactiveListOperations<String, String> operations = redisTemplate.opsForList();
return operations.leftPushAll(key, list);
}
@Override
public Flux<String> find(String key) {
ReactiveValueOperations<String, String> operations = redisTemplate.opsForValue();
return redisTemplate.keys(key).flatMap(keyId ->operations.get(keyId));
}
}
@RestController
@RequestMapping("/user")
public class UserController {
public final static String USER_KEY="user";
@Autowired
private RedisService redisService;
@GetMapping("/get/{key}")
public Mono<String> getUserByKey(@PathVariable("id")String key){
return redisService.get(key);
}
@GetMapping("/add")
public Mono<String> add(User user){
user = new User();
user.setAccount("neo");
user.setPassword("123456");
user.setNickname("netkiller");
user.setEmail("netkiller@msn.com");
user.setPhone("");
user.setGender(true);
user.setBirthday("1980-01-30");
user.setProvince("广东省");
user.setCity("深圳市");
user.setCounty("南山区");
user.setAddress("");
user.setState("Enabled");
System.out.println(JSON.toJSONString(user));
return redisService.set("neo",user);
}
@GetMapping("/addlist")
public Mono<Long> addlist(){
List<String> list=new ArrayList<String>();
User user = new User();
user.setAccount("neo");
user.setPassword("123456");
user.setNickname("netkiller");
user.setEmail("netkiller@msn.com");
user.setPhone("");
user.setGender(true);
user.setBirthday("1980-01-30");
user.setProvince("广东省");
user.setCity("深圳市");
user.setCounty("南山区");
user.setAddress("");
user.setState("Enabled");
//添加第一条数据
list.add(JSON.toJSONString(user));
//添加第二条数据
list.add(JSON.toJSONString(user));
//添加第三条数据
list.add(JSON.toJSONString(user));
return redisService.addlist("list", list);
}
@GetMapping(value="/findAll",produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<String> findAll(){
return redisService.all("list").delayElements(Duration.ofSeconds(2));
}
@GetMapping("/getUsers")
public Flux<String> findUsers() {
return redisService.find("*").delayElements(Duration.ofSeconds(2));
}
}