Sobes.tech
Senior

Question 7. There is a service ExampleService (see figure). If an external service calls methods in sequence: exampleService.getCityFromCache(100000); exampleService.getCity(100000); How many times will there be a call to CityDao::getCity if the cache was empty? @Component @RequiredArgsConstructor public class ExampleService { private final CityDao dao; @Cacheable public String getCityFromCache(int index) { return dao.getCity(index); } public String getCity(int index) { return getCityFromCache(index); } } 0 1 2 Depends on the implementation of CacheManager

sobes.tech AI

Answer from AI

The call to CityDao::getCity will be executed once.

Explanation:

  • The method getCityFromCache is annotated with @Cacheable, which means that on the first call with the parameter 100000, the result will be obtained from dao.getCity(100000) and cached.
  • The second call exampleService.getCity(100000) internally calls getCityFromCache(100000), but since the result is already in the cache, dao.getCity is not called again.

Summary: with an empty cache, the first call invokes DAO, the second uses the cache.

Answer: 1.