0

I'm using Spring Cache for Redis. I've 2 methods each returns policyTest object but are invoked with different parameters as shown below.

//  @Cacheable(value = "policyTest", key = "#policyNo")
@Cacheable(value = "policyTest")
  public PolicyTest getPolicyByNo(String policyNo) {
    log.info("RedisServiceImpl::getPolicyByNo() fetching TestPolicy from service ...");
    return new PolicyTest(policyNo, "1005", "200555");
  }




 //  @Cacheable(value = "policyTest", key = "#policyNo")    
  @Cacheable(value = "policyTest")
  public PolicyTest getPolicyByCustomerNo(String customerNo) {
    log.info("RedisServiceImpl::getPolicyByCustomerNo() fetching TestPolicy from service ...");
    return new PolicyTest("125", "1005", customerNo);
  }

For both methods, specified policyNo as key, but it is failing for getPolicyByCustomerNo(String customerNo) with error Null key returned for cache operation (maybe you are using named params on classes without debug info?) Without any key specified, the cache has dulicate PolicyTest object that are returned from both methods.

What I want is

  1. Have a single copy of customer in the cache, irresective of the input. But now the same object are cached twice
  2. want to specify @Cacheable with key, but it throws error - Null key returned for cache operation (maybe you are using named params on classes without debug info?)
user957183
  • 417
  • 3
  • 10
  • 20

1 Answers1

0

You can try to use key ="#p0"

J.N
  • 31
  • 4
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 12 '22 at 05:12