![](/img/trans.png)
[英]Spring boot Redis @Cacheable method not getting called from another class
[英]Spring Boot @Cacheable stopped working after I moved method to the same class
我在我的項目中做了一些重構,我面臨着非常奇怪的問題。
我有這個服務 class,它負責從 API 獲取和解析數據。在重構之前,我有一個特殊的 class 只用於這個緩存方法,現在我把它移到這里但它突然停止工作。 (當我在緩存方法中設置斷點時,每次調用此方法時都會調用它,之前它只是第一次調用,而不是從緩存中返回值)
這是我的服務 class:
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);
}
這是我的緩存配置 class
@Bean
public net.sf.ehcache.CacheManager ehCacheManager() {
final CacheConfiguration transportLocationCache = new CacheConfiguration();
final net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
transportLocationCache.setName(CACHE_NAME);
transportLocationCache.setMaxEntriesLocalHeap(ENTRIES_LOCAL_HEAP);
transportLocationCache.setMemoryStoreEvictionPolicy(LRU);
transportLocationCache.setTimeToLiveSeconds(TTL_SECONDS);
config.addCache(transportLocationCache);
return net.sf.ehcache.CacheManager.newInstance(config);
}
@Bean
@Override
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheManager());
}
你有什么想法,什么可能是錯的?
非常感謝你。
在查看可緩存源時,文檔說:
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.
此片段中與此問題最相關的部分是advised method
。 為了使 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.
https://stackoverflow.com/a/59961404/14072498
Pablo 提供的鏈接解釋了為什么調用方法不能與使用Cacheable
注釋的方法駐留在同一個 class 中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.