1

私は次のようなことをしようとしています:

@HystrixCommand(fallbackMethod = "returnLastGoodCachedCopy")
void performRequest(String someArg) { 
    // Make HTTP request using RestTemplate configured with EHCache  
}

void returnLastGoodCachedCopy(String someArg) {
    // Return the last successful response 
}

もう少し背景として、私はSpring Bootを使用しており、 RestTemplateEHCacheを使用するように設定しています:

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate(clientHttpRequestFactory());
}

@Bean
public CloseableHttpClient httpClient() {
    CacheConfig cacheConfig = CacheConfig.custom()
            .setMaxCacheEntries(this.httpClientProperties.getMaxCacheEntries())
            .setMaxObjectSize(this.httpClientProperties.getMaxObjectSize())
            .setHeuristicCachingEnabled(this.httpClientProperties.getHeuristicLifetimeEnabled())
            .setHeuristicDefaultLifetime(this.httpClientProperties.getHeuristicLifetimeSeconds()).build();

    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(this.httpClientProperties.getConnectTimeout())
            .setSocketTimeout(this.httpClientProperties.getSocketTimeout()).build();

    Ehcache httpEhcache = (Ehcache) this.ehCacheManager.getCache(httpClientProperties.getCacheName())
            .getNativeCache();

    EhcacheHttpCacheStorage ehcacheHttpCacheStorage = new EhcacheHttpCacheStorage(httpEhcache);

    CloseableHttpClient cachingClient = CachingHttpClients.custom().setCacheConfig(cacheConfig)
            .setHttpCacheStorage(ehcacheHttpCacheStorage).setDefaultRequestConfig(requestConfig).build();

    return cachingClient;
}

private ClientHttpRequestFactory clientHttpRequestFactory() {
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient());
    return factory;
}

だから私の考えは同じリクエストEHCacheを使用することでしたが、キャッシュがキャッシュ制御ヘッダーに基づいていることを考えると、それが適切な解決策であるかどうかはわかりません。有効期限が切れているかどうか。

もう 1 つの考えは、別の EHCache キャッシュを構成し、応答を自分で保存することでした。そうすれば、キーと値の形式を設定することで、より簡単にアクセスできるようになります。

この道をたどる前に、Hystrix にこの状況を処理するものが既に組み込まれているかどうか、または他に推奨されるアプローチがあるかどうかを確認したいと思います。

4

0 に答える 0