Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "cache" method of Mono

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?

like image 962
SoT Avatar asked Dec 03 '25 00:12

SoT


1 Answers

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
like image 197
rascio Avatar answered Dec 05 '25 16:12

rascio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!