Spring 使用 EhCache 的引导缓存不起作用
Spring Boot Caching with EhCache doesn't work
我正在使用 EhCache 和 Spring 引导来缓存来自外部 API 的 HTTP 响应,但缓存似乎不起作用。
我正在添加 Thread.sleep (2000);
来模拟使用缓存响应时应该跳过的延迟。但事实并非如此,每次方法调用都会调用延迟和外部 API。
这是我的缓存配置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());
}
}
以及应该缓存的方法
@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());
}
你知道为什么这不起作用吗?
感谢您的建议。
当Cacheable注解中没有指定key时,所有的方法参数都被认为是一个key,这就是这里发生的。
key -
Default is "", meaning all method parameters are considered as a key, unless a custom keyGenerator() has been configured.
由于TRANSPORT_LOCATIONS缓存中只需要一个键,可以指定方法名作为键
@Cacheable(value = TRANSPORT_LOCATIONS, cacheManager = "cacheManager", key = "#root.methodName")
我正在使用 EhCache 和 Spring 引导来缓存来自外部 API 的 HTTP 响应,但缓存似乎不起作用。
我正在添加 Thread.sleep (2000);
来模拟使用缓存响应时应该跳过的延迟。但事实并非如此,每次方法调用都会调用延迟和外部 API。
这是我的缓存配置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());
}
}
以及应该缓存的方法
@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());
}
你知道为什么这不起作用吗?
感谢您的建议。
当Cacheable注解中没有指定key时,所有的方法参数都被认为是一个key,这就是这里发生的。
key - Default is "", meaning all method parameters are considered as a key, unless a custom keyGenerator() has been configured.
由于TRANSPORT_LOCATIONS缓存中只需要一个键,可以指定方法名作为键
@Cacheable(value = TRANSPORT_LOCATIONS, cacheManager = "cacheManager", key = "#root.methodName")