Java 我将方法移动到同一个类后,Spring Boot@Cacheable停止工作
我正在我的项目中进行一些重构,我面临着一个非常奇怪的问题 我有一个服务类,负责从API获取和解析数据。在重构之前,我只为这个缓存方法创建了一个特殊的类,现在我把它移到了这里,但它突然停止了工作。(当我在cache方法中设置断点时,每次调用此方法时都会调用断点,在此之前,它只是第一次被调用,而不是从缓存返回值) 这是我的服务课:Java 我将方法移动到同一个类后,Spring Boot@Cacheable停止工作,java,spring-boot,caching,Java,Spring Boot,Caching,我正在我的项目中进行一些重构,我面临着一个非常奇怪的问题 我有一个服务类,负责从API获取和解析数据。在重构之前,我只为这个缓存方法创建了一个特殊的类,现在我把它移到了这里,但它突然停止了工作。(当我在cache方法中设置断点时,每次调用此方法时都会调用断点,在此之前,它只是第一次被调用,而不是从缓存返回值) 这是我的服务课: private static final String TRANSPORT_LOCATIONS = "transportLocations";
private static final String TRANSPORT_LOCATIONS = "transportLocations";
private static final String CACHE_MANAGER = "cacheManager";
private static final String ROOT_METHOD_NAME = "#root.methodName";
private static final int FALLBACK_TIMEOUT = 1000;
...
@Override
public List<TransportLocation> fetchAllTransportsLocations() throws Exception {
final var bodyStructure = createTransportLocationBody();
final String body = buildBody(bodyStructure);
final String url = transportLocationApiUrl + getCurrentTimeStamp();
final HttpResponse<String> response = getTransportsLocations(url, body);
if (isResponseBad(response.statusCode())) {
LOG.error(GET_TRANSPORT_LOCATION_BAD_REQUEST_ERROR_MESSAGE);
return createEmptyList();
}
return mapTransportsLocationsResponse(response.body());
}
@Cacheable(value = TRANSPORT_LOCATIONS, cacheManager = CACHE_MANAGER, key = ROOT_METHOD_NAME, sync = true)
private HttpResponse<String> getTransportsLocations(final String url, final String body) throws Exception {
HttpResponse<String> response = httpService.sendPostRequestWithBody(url, body);
if (isResponseBad(response.statusCode())) {
response = handleBadRequest(url, body);
}
return response;
}
private HttpResponse<String> handleBadRequest(final String url, final String body) throws Exception {
LOG.error(GET_TRANSPORT_LOCATION_CACHE_BAD_REQUEST_ERROR_MESSAGE);
Thread.sleep(FALLBACK_TIMEOUT);
return httpService.sendPostRequestWithBody(url, body);
}
你知道会出什么问题吗
非常感谢。在查看可缓存源代码时,doc说:
Annotation indicating that the result of invoking a method (or all methods in a class)
can be cached.
Each time an advised method is invoked, caching behavior will be applied, checking
whether the method has been already invoked for the given arguments.
这段代码中与这个问题最相关的部分是建议的方法
。为了使AOP工作,您需要像以前一样组织代码
解释为什么私有方法不能与AOP一起使用:
Because private methods are not inherited by subclasses, i.e. there is no way to
intercept a private method and then delegate to it because the subclass cannot even
call that method. This is a normal Java limitation and has nothing to do with AOP specifically.
Pablo提供的链接解释了为什么在查看可缓存源代码时,调用方法不能与用
Cacheable
注释的方法位于同一类中,doc说:
Annotation indicating that the result of invoking a method (or all methods in a class)
can be cached.
Each time an advised method is invoked, caching behavior will be applied, checking
whether the method has been already invoked for the given arguments.
这段代码中与这个问题最相关的部分是建议的方法
。为了使AOP工作,您需要像以前一样组织代码
解释为什么私有方法不能与AOP一起使用:
Because private methods are not inherited by subclasses, i.e. there is no way to
intercept a private method and then delegate to it because the subclass cannot even
call that method. This is a normal Java limitation and has nothing to do with AOP specifically.
Pablo提供的链接解释了为什么调用方法不能与带有
Cacheable
注释的方法驻留在同一类中,也许这篇文章也能回答你的问题:也许这篇文章也能回答你的问题:谢谢你的解释。我把这个逻辑移回了额外的类,它工作得很好。谢谢你抽出时间!谢谢你的解释。我把这个逻辑移回了额外的类,它工作得很好。谢谢你抽出时间!