Skip to content

Instantly share code, notes, and snippets.

@mp911de
Created November 7, 2018 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mp911de/ba9c46fe8e8bbe4558ca445e996f108a to your computer and use it in GitHub Desktop.
Save mp911de/ba9c46fe8e8bbe4558ca445e996f108a to your computer and use it in GitHub Desktop.
package com.example.demo;
import org.springframework.data.redis.core.RedisTemplate;
import reactor.core.publisher.Mono;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.util.function.Tuples;
@SpringBootApplication
public class DemoApplication {
@Autowired ReactiveRedisTemplate<Object, Object> template;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@PostConstruct
private void postConstruct() {
for (int i = 0; i < 4; i++) {
template.opsForHash().put("master-key", "entry-" + i, "use-key-" + i).subscribe();
template.opsForValue().set("use-key-" + i, "the value " + i).subscribe();
}
}
@RestController
static class ReactiveController {
@Autowired ReactiveRedisTemplate<Object, Object> template;
@GetMapping("/")
public Mono<Object> get() {
return template.<String, String> opsForHash().get("master-key", "entry-1")
.zipWith(template.<String, String> opsForHash().get("master-key", "entry-2")).flatMap(t -> {
return template.opsForValue().get(t.getT1()).zipWith(template.opsForValue().get(t.getT2()));
});
}
}
//@RestController
static class SynchronousController {
@Autowired RedisTemplate<Object, Object> template;
@GetMapping("/")
public Object get() {
String entry1 = template.<String, String>opsForHash().get("master-key", "entry-1");
String entry2 = template.<String, String> opsForHash().get("master-key", "entry-2");
Object v1 = template.opsForValue().get(entry1);
Object v2 = template.opsForValue().get(entry2);
return Tuples.of(v1, v2);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.1.3.BUILD-SNAPSHOT</version>
</dependency>
<!--
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment