From 82de22618ef0e95450ce43fa519ed1203d256674 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 24 Oct 2018 01:07:30 +0300 Subject: ActivityPub: cache requests --- .../configuration/ActivityPubClientConfig.java | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java (limited to 'juick-server/src/main/java/com/juick/server/configuration') diff --git a/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java b/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java new file mode 100644 index 00000000..b7762221 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java @@ -0,0 +1,40 @@ +package com.juick.server.configuration; + +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.cache.CacheConfig; +import org.apache.http.impl.client.cache.CachingHttpClientBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.BufferingClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class ActivityPubClientConfig { + @Bean + public RestTemplate apClient() { + SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); + RestTemplate restTemplate = new RestTemplate(requestFactory); + // BufferingClientHttpRequestFactory allows us to read the response more than once - Necessary for debugging. + restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient()))); + return restTemplate; + } + @Bean + public HttpClient httpClient() { + return CachingHttpClientBuilder + .create() + .setCacheConfig(cacheConfig()) + .build(); + } + @Bean + public CacheConfig cacheConfig() { + return CacheConfig + .custom() + .setMaxObjectSize(500000) // 500KB + .setMaxCacheEntries(2000) + // Set this to false and a response with queryString + // will be cached when it is explicitly cacheable .setNeverCacheHTTP10ResponsesWithQueryString(false) + .build(); + } +} \ No newline at end of file -- cgit v1.2.3