How to use "cache" method of Mono
Asked Answered
F

1

17

I'm a beginner of spring webflux. While researching I found some code like:

Mono result = someMethodThatReturnMono().cache();

The name "cache" tell me about caching something, but where is the cache and how to retrieve cached things? Is it something like caffeine?

Feldt answered 11/5, 2021 at 12:47 Comment(1)
Your mono is a rawtype - don’t do that. Read the docs please - every subscriber to a Mono would normally trigger a new subscription; when cache is called then once a value is returned, the same value will be returned to all subsequent subscribers. projectreactor.io/docs/core/release/api/reactor/core/publisher/…Jezabelle
M
28

It cache the result of the previous steps of the Flux/Mono until the cache() method is called, check the output of this code to see it in action:

import reactor.core.publisher.Mono;

public class CacheExample {

    public static void main(String[] args) {
        var mono = Mono.fromCallable(() -> {
            System.out.println("Go!");
            return 5;
        })
        .map(i -> {
            System.out.println("Double!");
            return i * 2;
        });

        var cached = mono.cache();

        System.out.println("Using cached");

        System.out.println("1. " + cached.block());
        System.out.println("2. " + cached.block());
        System.out.println("3. " + cached.block());

        System.out.println("Using NOT cached");


        System.out.println("1. " + mono.block());
        System.out.println("2. " + mono.block());
        System.out.println("3. " + mono.block());
    }
}

output:

Using cached
Go!
Double!
1. 10
2. 10
3. 10
Using NOT cached
Go!
Double!
1. 10
Go!
Double!
2. 10
Go!
Double!
3. 10
Make answered 11/5, 2021 at 13:1 Comment(5)
But for how long by default does this cache work ? projectreactor.io/docs/core/release/api/reactor/core/publisher/… Once the first subscription is made to this Mono, the source is subscribed to and the signal will be cached, indefinitely. This process cannot be cancelled. It's indefinitely ?Ivanovo
yes, it is. It doesn't have a time to live, this is used for different use cases. Imagine during the handling of a request for an http server, or during the running of a batch you have to access the same calculation multiple time, but you want to calculate it only once.Make
ok, but when i use HTTP request with other param value will the cache return the same result ? For example calculations are done based on some kind of param that is provided in http request.Ivanovo
nope I meant calculations in the scope of the same HTTP request, not shared among multiple HTTP requests. To implement a cache across different requests you need some other caching mechanism, the Mono/Flux cache is something also named memoization in other contextsMake
I had a use-case where my service made a REST call then: 1. do some calculation and save to db. 2. do some other calculations and return to user. I cached the response and seperately used .subscribe() for saving to db. This made db operation done on background while user have the response without wait for db ops to complete. I was confused on using .cache() but then @Make 's line : until the cache() method is called made everything clear. Thanks Rascio.Ocko

© 2022 - 2024 — McMap. All rights reserved.