Spring Boot使用EhCache进行缓存未生效。

huangapple go评论73阅读模式
英文:

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 = &quot;transportLocations&quot;;
    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(&quot;LRU&quot;);
        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 = &quot;cacheManager&quot;)
    public HttpResponse&lt;String&gt; sendTransportLocationsPostRequest(
            final LinkedHashMap&lt;String, Object&gt; 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)时,所有方法参数都被视为一个键,这正是这里发生的情况。

参考 - https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/annotation/Cacheable.html

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 = &quot;cacheManager&quot;, key = &quot;#root.methodName&quot;)

huangapple
  • 本文由 发表于 2020年8月29日 18:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63645997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定