I am using Spring Boot with Redis and I want to cache the individual elements of a list separately based on unique keys.
I have a method that generates a list of values:
@Cacheable(value = "fundYields", key = "#colName + ':' + #code + ':' + #firstDate + ':' + #dateList")
public List<BigDecimal> calculateYields(String colName, String code, String firstDate, List<String> dateList) {
List<BigDecimal> results = new ArrayList<>();
for (String date : dateList) {
// creating a nosql custom aggregation operation to fetch data with a single database request
}
// ..
return results;
}
Caching Requirement: Instead of caching the entire list under one key, I want to cache each element individually using unique keys like:
{
"fund_price:ABC:2023-01-01:2023-02-15": 10.5
}
{
"fund_price:ABC:2023-01-01:2023-03-10": 12.8
}
So that if a request is later made with only "fund_price:ABC:2023-01-01:2023-02-15", it can reuse the cached value instead of recalculating same value.
How can I cache each list element separately in Redis using @Cacheable? If it is not possible what is the best approach to achieve this efficiently?