I am just starting to learn Spring Boot and am trying to use cache with the Google Sheets API to study Springboot. To use cache with Google Sheets in Spring Boot, I added the spring-boot-starter-cache dependency in my pom.xml file and enabled cache support by using the @EnableCaching annotation in my application's main class. I then used the @Cacheable annotation for caching the response from the Google Sheets API when calling a method to get a spreadsheet. However, the cache does not work, as the method is still being called every time.
Here is an example of the code I am using:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class GoogleSheetsService {
private static final Logger logger = LoggerFactory.getLogger(GoogleSheetsService.class);
@Cacheable(value = "spreadsheet", key = "#spreadsheetId")
public Spreadsheet getSpreadsheet(String spreadsheetId) {
logger.info("Getting spreadsheet from API for {}", spreadsheetId);
// Call Google Sheets API to get spreadsheet
return spreadsheet;
}
}
I expected the cache to return the cached value for the same spreadsheetId, but the method is being called every time and the "Getting spreadsheet from API for {}" log is being printed. Can you please help me understand what might be causing the cache to not work as expected?