spring-bootcachinghazelcast

Spring boot hazlecast returning same value even when parameter is different


I am using spring boot version 2.7.5 and hazlecast 4.1. I have an issue basically I have a search API where i can specify the number of records I want to return.

On the first call for example my API retrieve 50 records and on the second call even when I specify 5 records, 50 records is being retrieved. It looks like on the first call the data is cached and any subsequent call with different parameter it is returning same value that was defined in the first call.

Please find my application.yml config below:

    cache:
  hazelcast:
    amountToStartCaching: 50
    # Metrics
    metric:
      enabled: false
      frequency-seconds: 5

    # Client Connection Configurations
    cluster-name: hz-cluster
    instance-name: service-hz-client
    labels: DUMMY, JAVA_CLIENT
    # Properties
    # Event thread count -> -1 means to auto-calculate based on the number of cores
    event-thread-count: -1

     # Network Configurations
    network:
      server:
        address: 127.0.0.1

    # Near Cache Configurations
    near-cache:
      maps:
        - cacheName: DUMMY_CACHE
          timeToLiveSeconds: 72000
          maxIdleSeconds: 0
          inMemoryFormat: OBJECT
          maxSize: 10
          evictionPolicy: LRU

My Rest endpoint is as follows:

   @GetMapping("/call")
    public CallListResponse searchCallList( CallSearchRequest callSearchRequest) {
       
        return dummyClass.searchList(callSearchRequest);
    }

My class that is being cache is shown below:

 @Cacheable(value = DUMMY_CALL_NAME, key = DUMMY_CALL_KEY)
    public ListResponse searchList(CallSearchRequest callSearchRequest) {
       ......
        return ListResponse;
    }

My request is as follows:

Public class CallSearchRequest implements Serializable {
    private String portCode;
    private String portDescription;
    private @NotBlank String sortOrder;
    private @NotBlank String sortBy;
    private @NotNull Integer pageSize;
    private @NotNull Integer pageNumber;
}

So on the first call page size is 50 and i receive 50 records, and the second call page size is 10 I should receive 10 records but still receiving 50 record.

Any idea what i am doing wrong pls?


Solution

  • You are giving the same key for @Cacheable annotation.

    @Cacheable(value = DUMMY_CALL_NAME, key = DUMMY_CALL_KEY). 
    

    Spring can not differentiate between different callSearchRequest objects.

    1. You may need to use Spring Expression language and define a proper key. For example
    @Cacheable(value = DUMMY_CALL_NAME, key = "#callSearchRequest.pageNumber"). 
    
    1. If you don't define a key, Spring will create a key for you.

    Try both and see which one suits for needs.