From bf6bf8cd7c41788d4ca3919f14ed9485007202dc Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Fri, 23 Feb 2024 17:30:41 +0800 Subject: [PATCH] refactor: add cache control for reverse proxy resources (#5392) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area core /milestone 2.13.x #### What this PR does / why we need it: 为插件静态代理资源增加缓存控制以减少不必要的请求 #### Which issue(s) this PR fixes: Fixes #5357 #### Does this PR introduce a user-facing change? ```release-note 为插件静态代理资源增加缓存控制以减少不必要的请求 ``` --- .../ReverseProxyRouterFunctionFactory.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/run/halo/app/plugin/resources/ReverseProxyRouterFunctionFactory.java b/application/src/main/java/run/halo/app/plugin/resources/ReverseProxyRouterFunctionFactory.java index 907c8eadf..af0cd442f 100644 --- a/application/src/main/java/run/halo/app/plugin/resources/ReverseProxyRouterFunctionFactory.java +++ b/application/src/main/java/run/halo/app/plugin/resources/ReverseProxyRouterFunctionFactory.java @@ -4,11 +4,14 @@ import static org.springframework.http.MediaType.ALL; import static org.springframework.web.reactive.function.server.RequestPredicates.GET; import static org.springframework.web.reactive.function.server.RequestPredicates.accept; +import java.io.IOException; +import java.time.Instant; import java.util.List; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.pf4j.PluginManager; +import org.springframework.boot.autoconfigure.web.WebProperties; import org.springframework.context.ApplicationContext; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; @@ -18,11 +21,13 @@ import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; import org.springframework.util.Assert; +import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.util.pattern.PathPatternParser; +import reactor.core.publisher.Mono; import run.halo.app.core.extension.ReverseProxy; import run.halo.app.core.extension.ReverseProxy.FileReverseProxyProvider; import run.halo.app.core.extension.ReverseProxy.ReverseProxyRule; @@ -45,6 +50,7 @@ public class ReverseProxyRouterFunctionFactory { private final PluginManager pluginManager; private final ApplicationContext applicationContext; + private final WebProperties webProperties; /** *
Create {@link RouterFunction} according to the {@link ReverseProxy} custom resource @@ -76,8 +82,23 @@ public class ReverseProxyRouterFunctionFactory { if (!resource.exists()) { return ServerResponse.notFound().build(); } - return ServerResponse.ok() - .bodyValue(resource); + var cacheProperties = webProperties.getResources().getCache(); + var useLastModified = cacheProperties.isUseLastModified(); + var bodyBuilder = ServerResponse.ok() + .cacheControl(cacheProperties.getCachecontrol().toHttpCacheControl()); + try { + if (useLastModified) { + var lastModified = Instant.ofEpochMilli(resource.lastModified()); + return request.checkNotModified(lastModified) + .switchIfEmpty(Mono.defer( + () -> bodyBuilder.lastModified(lastModified) + .body(BodyInserters.fromResource(resource))) + ); + } + return bodyBuilder.body(BodyInserters.fromResource(resource)); + } catch (IOException e) { + throw new RuntimeException(e); + } }); }).reduce(RouterFunction::and).orElse(null); }