Reduce the number of queries to resolve theme (#3238)

#### What type of PR is this?

/kind improvement
/area core

#### What this PR does / why we need it:

Save ThemeContext into ServerWebExchange to reduce the number of queries to resolve theme from 2(or 3) to 1.

#### Which issue(s) this PR fixes:

Fixes https://github.com/halo-dev/halo/issues/3162

#### Does this PR introduce a user-facing change?

```release-note
优化查询主题的查询次数
```
pull/3243/head
John Niang 2023-02-07 14:40:12 +08:00 committed by GitHub
parent df97ee8629
commit df9b04c4d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 27 deletions

View File

@ -12,6 +12,7 @@ import org.springframework.http.ProblemDetail;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.util.context.Context;
import run.halo.app.theme.ThemeContext; import run.halo.app.theme.ThemeContext;
import run.halo.app.theme.ThemeResolver; import run.halo.app.theme.ThemeResolver;
import run.halo.app.theme.engine.ThemeTemplateAvailabilityProvider; import run.halo.app.theme.engine.ThemeTemplateAvailabilityProvider;
@ -59,9 +60,9 @@ public class HaloErrorWebExceptionHandler extends DefaultErrorWebExceptionHandle
@Override @Override
protected Mono<ServerResponse> renderErrorView(ServerRequest request) { protected Mono<ServerResponse> renderErrorView(ServerRequest request) {
return themeResolver.getTheme(request.exchange().getRequest()) return themeResolver.getTheme(request.exchange())
.flatMap(themeContext -> super.renderErrorView(request) .flatMap(themeContext -> super.renderErrorView(request)
.contextWrite(context -> context.put(ThemeContext.class, themeContext))); .contextWrite(Context.of(ThemeContext.class, themeContext)));
} }
@Override @Override

View File

@ -50,7 +50,7 @@ public class HaloViewResolver extends ThymeleafReactiveViewResolver {
@Override @Override
public Mono<Void> render(Map<String, ?> model, MediaType contentType, public Mono<Void> render(Map<String, ?> model, MediaType contentType,
ServerWebExchange exchange) { ServerWebExchange exchange) {
return themeResolver.getTheme(exchange.getRequest()).flatMap(theme -> { return themeResolver.getTheme(exchange).flatMap(theme -> {
// calculate the engine before rendering // calculate the engine before rendering
setTemplateEngine(engineManager.getTemplateEngine(theme)); setTemplateEngine(engineManager.getTemplateEngine(theme));
return super.render(model, contentType, exchange) return super.render(model, contentType, exchange)

View File

@ -25,7 +25,7 @@ public class ThemeContextBasedVariablesAcquirer implements ViewContextBasedVaria
@Override @Override
public Mono<Map<String, Object>> acquire(ServerWebExchange exchange) { public Mono<Map<String, Object>> acquire(ServerWebExchange exchange) {
return themeResolver.getTheme(exchange.getRequest()) return themeResolver.getTheme(exchange)
.flatMap(themeContext -> { .flatMap(themeContext -> {
String name = themeContext.getName(); String name = themeContext.getName();
return themeFinder.getByName(name); return themeFinder.getByName(name);

View File

@ -2,9 +2,9 @@ package run.halo.app.theme;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher; import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting.Theme; import run.halo.app.infra.SystemSetting.Theme;
@ -37,24 +37,37 @@ public class ThemeResolver {
.map(ThemeContext.ThemeContextBuilder::build); .map(ThemeContext.ThemeContextBuilder::build);
} }
public Mono<ThemeContext> getTheme(ServerHttpRequest request) { public Mono<ThemeContext> getTheme(ServerWebExchange exchange) {
return environmentFetcher.fetch(Theme.GROUP, Theme.class) return fetchThemeFromExchange(exchange)
.map(Theme::getActive) .switchIfEmpty(Mono.defer(() -> environmentFetcher.fetch(Theme.GROUP, Theme.class)
.switchIfEmpty(Mono.error(() -> new IllegalArgumentException("No theme activated"))) .map(Theme::getActive)
.map(activatedTheme -> { .switchIfEmpty(
var builder = ThemeContext.builder(); Mono.error(() -> new IllegalArgumentException("No theme activated")))
var themeName = .map(activatedTheme -> {
request.getQueryParams().getFirst(ThemeContext.THEME_PREVIEW_PARAM_NAME); var builder = ThemeContext.builder();
if (StringUtils.isBlank(themeName)) { var themeName = exchange.getRequest().getQueryParams()
themeName = activatedTheme; .getFirst(ThemeContext.THEME_PREVIEW_PARAM_NAME);
} if (StringUtils.isBlank(themeName)) {
boolean active = StringUtils.equals(activatedTheme, themeName); themeName = activatedTheme;
var path = themeRoot.get().resolve(themeName); }
return builder.name(themeName) boolean active = StringUtils.equals(activatedTheme, themeName);
.path(path) var path = themeRoot.get().resolve(themeName);
.active(active) return builder.name(themeName)
.build(); .path(path)
}); .active(active)
.build();
})
.doOnNext(themeContext ->
exchange.getAttributes().put(ThemeContext.class.getName(), themeContext))
));
}
public Mono<ThemeContext> fetchThemeFromExchange(ServerWebExchange exchange) {
return Mono.justOrEmpty(exchange)
.map(ServerWebExchange::getAttributes)
.filter(attrs -> attrs.containsKey(ThemeContext.class.getName()))
.map(attrs -> attrs.get(ThemeContext.class.getName()))
.cast(ThemeContext.class);
} }
} }

View File

@ -1,12 +1,14 @@
package run.halo.app.theme.message; package run.halo.app.theme.message;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Path; import java.nio.file.Path;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@ -14,13 +16,13 @@ import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import run.halo.app.theme.ThemeContext; import run.halo.app.theme.ThemeContext;
import run.halo.app.theme.ThemeResolver; import run.halo.app.theme.ThemeResolver;
@ -50,7 +52,7 @@ public class ThemeMessageResolverIntegrationTest {
defaultThemeUrl = ResourceUtils.getURL("classpath:themes/default"); defaultThemeUrl = ResourceUtils.getURL("classpath:themes/default");
otherThemeUrl = ResourceUtils.getURL("classpath:themes/other"); otherThemeUrl = ResourceUtils.getURL("classpath:themes/other");
Mockito.when(themeResolver.getTheme(Mockito.any(ServerHttpRequest.class))) when(themeResolver.getTheme(any(ServerWebExchange.class)))
.thenReturn(Mono.just(createDefaultContext())); .thenReturn(Mono.just(createDefaultContext()));
} }
@ -150,7 +152,7 @@ public class ThemeMessageResolverIntegrationTest {
"""); """);
// For other theme // For other theme
Mockito.when(themeResolver.getTheme(Mockito.any(ServerHttpRequest.class))) when(themeResolver.getTheme(any(ServerWebExchange.class)))
.thenReturn(Mono.just(createOtherContext())); .thenReturn(Mono.just(createOtherContext()));
webTestClient.get() webTestClient.get()
.uri("/index?language=zh") .uri("/index?language=zh")