英文:
Spring Boot Caching with EhCache doesn't work
问题
以下是翻译好的部分:
我正在使用 EhCache 和 Spring Boot 来缓存来自外部 API 的 HTTP 响应,但缓存似乎无法正常工作。
我正在添加 Thread.sleep(2000);
来模拟在使用缓存响应时应该跳过的延迟。但实际情况是,每次方法调用都会出现延迟,并且还会调用外部 API。
这是我的缓存配置类
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
private static final String TRANSPORT_LOCATIONS = "transportLocations";
private static final int TTL_MILLISECONDS = 15;
@Bean
public net.sf.ehcache.CacheManager ehCacheManager() {
CacheConfiguration transportLocationCache = new CacheConfiguration();
transportLocationCache.setName(TRANSPORT_LOCATIONS);
transportLocationCache.setMaxEntriesLocalHeap(1000);
transportLocationCache.setMemoryStoreEvictionPolicy("LRU");
transportLocationCache.setTimeToLiveSeconds(TTL_MILLISECONDS);
net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
config.addCache(transportLocationCache);
return net.sf.ehcache.CacheManager.newInstance(config);
}
@Bean
@Override
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheManager());
}
}
还有一个应该被缓存的方法
@Cacheable(value = TRANSPORT_LOCATIONS, cacheManager = "cacheManager")
public HttpResponse<String> sendTransportLocationsPostRequest(
final LinkedHashMap<String, Object> bodyStructure,
final String url)
throws Exception {
Thread.sleep(2000);
final String body = buildBody(bodyStructure);
final HttpRequest request = buildPostRequest(url, body);
return client.send(request, HttpResponse.BodyHandlers.ofString());
}
你知道为什么这不起作用吗?
谢谢你的建议。
英文:
I'm using EhCache and Spring Boot to cache the HTTP response from the external API, but the cache doesn't seem to work.
I'm adding Thread.sleep (2000);
to simulate the delay that should be skipped when using a cached response. But it is not, and with each method call, a delay and an external API are also called.
Here is my cache configuration class
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
private static final String TRANSPORT_LOCATIONS = "transportLocations";
private static final int TTL_MILLISECONDS = 15;
@Bean
public net.sf.ehcache.CacheManager ehCacheManager() {
CacheConfiguration transportLocationCache = new CacheConfiguration();
transportLocationCache.setName(TRANSPORT_LOCATIONS);
transportLocationCache.setMaxEntriesLocalHeap(1000);
transportLocationCache.setMemoryStoreEvictionPolicy("LRU");
transportLocationCache.setTimeToLiveSeconds(TTL_MILLISECONDS);
net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
config.addCache(transportLocationCache);
return net.sf.ehcache.CacheManager.newInstance(config);
}
@Bean
@Override
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheManager());
}
}
And method, which should be cached
@Cacheable(value = TRANSPORT_LOCATIONS, cacheManager = "cacheManager")
public HttpResponse<String> sendTransportLocationsPostRequest(
final LinkedHashMap<String, Object> bodyStructure,
final String url)
throws Exception {
Thread.sleep(2000);
final String body = buildBody(bodyStructure);
final HttpRequest request = buildPostRequest(url, body);
return client.send(request, HttpResponse.BodyHandlers.ofString());
}
Do you have any idea, why this is not working?
Thanks for your advices.
答案1
得分: 3
当在Cacheable
注解中未指定键(key)时,所有方法参数都被视为一个键,这正是这里发生的情况。
key - 默认值为"",表示所有方法参数都被视为键,除非配置了自定义的keyGenerator()。
由于在TRANSPORT_LOCATIONS缓存中您只需要单个键,您可以将方法名指定为键:
@Cacheable(value = TRANSPORT_LOCATIONS, cacheManager = "cacheManager", key = "#root.methodName")
英文:
When key is not specified in the Cacheable annotation, then all the method parameters are considered as a key which is happening here.
Refer - https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/annotation/Cacheable.html
> key -
Default is "", meaning all method parameters are considered as a key, unless a custom keyGenerator() has been configured.
Since you only need single key in the TRANSPORT_LOCATIONS cache, you can specify method name as the key
@Cacheable(value = TRANSPORT_LOCATIONS, cacheManager = "cacheManager", key = "#root.methodName")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论