org.springframework.data.redis.core.ReactiveValueOperations Java Examples

The following examples show how to use org.springframework.data.redis.core.ReactiveValueOperations. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CityWebFluxReactiveController.java    From springboot-learning-example with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #2
Source File: CityWebFluxReactiveController.java    From springboot-learning-example with Apache License 2.0 4 votes vote down vote up
@PostMapping
public Mono<City> saveCity(@RequestBody City city) {
    String key = "city_" + city.getId();
    ReactiveValueOperations<String, City> operations = reactiveRedisTemplate.opsForValue();
    return operations.getAndSet(key, city);
}
 
Example #3
Source File: ValueOperationsTests.java    From spring-data-examples with Apache License 2.0 3 votes vote down vote up
/**
 * Implement a simple caching sequence using {@code GET} and {@code SETEX} commands.
 */
@Test
public void shouldCacheValue() {

	String cacheKey = "foo";

	ReactiveValueOperations<String, String> valueOperations = operations.opsForValue();

	Mono<String> cachedMono = valueOperations.get(cacheKey) //
			.switchIfEmpty(cacheValue().flatMap(it -> {

				return valueOperations.set(cacheKey, it, Duration.ofSeconds(60)).then(Mono.just(it));
			}));

	log.info("Initial access (takes a while...)");

	StepVerifier.create(cachedMono).expectSubscription() //
			.expectNoEvent(Duration.ofSeconds(9)) //
			.expectNext("Hello, World!") //
			.verifyComplete();

	log.info("Subsequent access (use cached value)");

	Duration duration = StepVerifier.create(cachedMono) //
			.expectNext("Hello, World!") //
			.verifyComplete();

	log.info("Done");

	assertThat(duration).isLessThan(Duration.ofSeconds(2));
}