0

I have a module that defines caches as per similar to this blog, which is being brought in as a dependency in another module.
However the caching isn't being bootstrapped in the main application. How do I configure springboot app to bootstrap the cache(s) defined in the dependency?

Dark Star1
  • 6,986
  • 16
  • 73
  • 121

1 Answers1

0

Caching happens on two possible scenario

  1. On Method Invocation
  2. Manually Put the object in the cache

going by the example in your question, let's understand the method below

@Service
public class NumberService {

    // ...
    @Cacheable(
      value = "squareCache", 
      key = "#number", 
      condition = "#number>10")
    public BigDecimal square(Long number) {
        BigDecimal square = BigDecimal.valueOf(number)
          .multiply(BigDecimal.valueOf(number));
        log.info("square of {} is {}", number, square);
        return square;
    }
}

The method square will cache the result for all inputs(let's say 1 to 10 ) if you want it to cache square of all possible input values, all you need to do call the method with all possible value. You can do this at startup.

This URL How to load @Cache on startup in spring? will help you

Below is one sample

@Service
public class NumberCacheScheduler {
    @Autowired
    NumberService numberService;


    @PostConstruct
    public void init() {
        update();

    }

    public void update() {
        for (long l=1l; i <=10,l++) {
            numberService.square(l);
        }
    }
}
Shailesh Chandra
  • 2,164
  • 2
  • 17
  • 25
  • Thanks but I am asking how can I configure springboot to bootstrap caching defined in a **dependency module**. I don't want to have to redefine the caching in the application since the dependency module has already taken care of everything. – Dark Star1 Oct 11 '19 at 10:36
  • without knowing the dependency type The moment your application's initialization is complete the scheduler in your application can call the caching method in dependency and caching of dependency should serve the cached data (unless you are required to set up caching too) – Shailesh Chandra Oct 11 '19 at 10:40