我使用的是 Spring Boot 2.7.5 版本和 hazlecast 4.1。我有一个问题,基本上我有一个搜索 API,我可以在其中指定我想要返回的记录数。
例如,在第一次调用时,我的 API 检索 50 条记录,而在第二次调用时,即使我指定 5 条记录,也会检索 50 条记录。看起来在第一次调用时数据被缓存,并且任何具有不同参数的后续调用都会返回在第一次调用中定义的相同值。
请在下面找到我的 application.yml 配置:
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
我的休息终点如下:
@GetMapping("/call")
public CallListResponse searchCallList( CallSearchRequest callSearchRequest) {
return dummyClass.searchList(callSearchRequest);
}
我正在缓存的类如下所示:
@Cacheable(value = DUMMY_CALL_NAME, key = DUMMY_CALL_KEY)
public ListResponse searchList(CallSearchRequest callSearchRequest) {
......
return ListResponse;
}
我的要求如下:
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;
}
因此,第一次调用页面大小为 50,我收到 50 条记录,第二次调用页面大小为 10,我应该收到 10 条记录,但仍然收到 50 条记录。
知道我做错了什么吗?
您为 @Cacheable 注释提供了相同的密钥。
@Cacheable(value = DUMMY_CALL_NAME, key = DUMMY_CALL_KEY).
Spring 无法区分不同的 callSearchRequest 对象。
@Cacheable(value = DUMMY_CALL_NAME, key = "#callSearchRequest.pageNumber").
两种都尝试一下,看看哪一种适合需求。