Sobes.tech
Senior
46

Domanda 7. C'è un servizio ExampleService (vedi immagine). Se un servizio esterno chiama i metodi in sequenza: exampleService.getCityFromCache(100000); exampleService.getCity(100000); Quante volte verrà chiamato CityDao::getCity se la cache era vuota? @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 Dipende dall'implementazione di CacheManager

Le aziende dove è stato chiesto
RT Labs

Risposta dell'AI

sobes.tech AI

Обращение к CityDao::getCity будет выполнено один раз.

Пояснение:

  • Метод getCityFromCache помечен аннотацией @Cacheable, значит при первом вызове с параметром 100000 результат будет получен из dao.getCity(100000) и закэширован.
  • Второй вызов exampleService.getCity(100000) вызывает внутри себя getCityFromCache(100000), но так как результат уже в кэше, dao.getCity не вызывается повторно.

Итог: при пустом кэше первый вызов вызывает DAO, второй — использует кэш.

Ответ: 1.