|
1 | 1 | package fr.insee.onyxia.api.configuration; |
2 | 2 |
|
3 | 3 | import fr.insee.onyxia.model.region.Region; |
| 4 | +import io.micrometer.common.util.StringUtils; |
| 5 | +import java.io.File; |
4 | 6 | import java.io.IOException; |
5 | 7 | import okhttp3.*; |
6 | 8 | import okhttp3.logging.HttpLoggingInterceptor; |
7 | 9 | import org.jetbrains.annotations.NotNull; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 13 | +import org.springframework.beans.factory.annotation.Value; |
8 | 14 | import org.springframework.context.annotation.Bean; |
9 | 15 | import org.springframework.context.annotation.Configuration; |
10 | 16 |
|
11 | 17 | @Configuration |
12 | 18 | public class HttpClientProvider { |
13 | 19 |
|
| 20 | + @Value("${http.cacheEnabled}") |
| 21 | + private boolean cacheEnabled; |
| 22 | + |
| 23 | + @Value("${http.cacheMaxSizeMB}") |
| 24 | + private Integer cacheMaxSizeMB; |
| 25 | + |
| 26 | + @Value("${http.overrideCacheLocation}") |
| 27 | + private String overrideCacheLocation; |
| 28 | + |
| 29 | + private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientProvider.class); |
| 30 | + |
14 | 31 | @Bean |
15 | 32 | public OkHttpClient httpClient() { |
16 | 33 | OkHttpClient.Builder builder = new OkHttpClient.Builder(); |
17 | 34 | enableDebugFeatureIfEnabled(builder); |
18 | 35 | return builder.build(); |
19 | 36 | } |
20 | 37 |
|
| 38 | + @Bean |
| 39 | + @Qualifier("withCache") |
| 40 | + public OkHttpClient httpClientWithCache() { |
| 41 | + OkHttpClient.Builder builder = new OkHttpClient.Builder(); |
| 42 | + enableDebugFeatureIfEnabled(builder); |
| 43 | + if (cacheEnabled) { |
| 44 | + File httpCacheDirectory; |
| 45 | + if (StringUtils.isNotBlank(overrideCacheLocation)) { |
| 46 | + httpCacheDirectory = new File(overrideCacheLocation); |
| 47 | + } else { |
| 48 | + httpCacheDirectory = new File(System.getProperty("java.io.tmpdir"), "http_cache"); |
| 49 | + } |
| 50 | + LOGGER.info("Caching helm packages at {}", httpCacheDirectory.getAbsolutePath()); |
| 51 | + long cacheSize = cacheMaxSizeMB * 1024L * 1024L; |
| 52 | + Cache cache = new Cache(httpCacheDirectory, cacheSize); |
| 53 | + builder.cache(cache); |
| 54 | + } |
| 55 | + return builder.build(); |
| 56 | + } |
| 57 | + |
21 | 58 | public OkHttpClient getClientForRegion(Region region) { |
22 | 59 | OkHttpClient.Builder builder = |
23 | 60 | new OkHttpClient.Builder() |
@@ -71,4 +108,28 @@ private void enableDebugFeatureIfEnabled(OkHttpClient.Builder builder) { |
71 | 108 | builder.addInterceptor(logging); |
72 | 109 | } |
73 | 110 | } |
| 111 | + |
| 112 | + public void setCacheEnabled(boolean cacheEnabled) { |
| 113 | + this.cacheEnabled = cacheEnabled; |
| 114 | + } |
| 115 | + |
| 116 | + public boolean isCacheEnabled() { |
| 117 | + return cacheEnabled; |
| 118 | + } |
| 119 | + |
| 120 | + public void setCacheMaxSizeMB(Integer cacheMaxSizeMB) { |
| 121 | + this.cacheMaxSizeMB = cacheMaxSizeMB; |
| 122 | + } |
| 123 | + |
| 124 | + public Integer getCacheMaxSizeMB() { |
| 125 | + return cacheMaxSizeMB; |
| 126 | + } |
| 127 | + |
| 128 | + public void setOverrideCacheLocation(String overrideCacheLocation) { |
| 129 | + this.overrideCacheLocation = overrideCacheLocation; |
| 130 | + } |
| 131 | + |
| 132 | + public String getOverrideCacheLocation() { |
| 133 | + return overrideCacheLocation; |
| 134 | + } |
74 | 135 | } |
0 commit comments