最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to Cache List Elements Separately Based on Unique Keys in Spring Boot with Redis? - Stack Overflow

programmeradmin0浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论