Remove wrong cache of pattern matchers

pull/7430/head
John Niang 2025-05-14 16:49:11 +08:00
parent 18105cbe44
commit b4ac91df20
No known key found for this signature in database
GPG Key ID: D7363C015BBCAA59
3 changed files with 10 additions and 22 deletions

View File

@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.SmartLifecycle; import org.springframework.context.SmartLifecycle;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
@ -99,11 +98,6 @@ public class ThemeCompositeRouterFunction
this.cachedRouters = routerFunctions(); this.cachedRouters = routerFunctions();
} }
@EventListener
public void onApplicationStarted(ApplicationReadyEvent event) {
this.cachedRouters = routerFunctions();
}
@Override @Override
public void start() { public void start() {
if (running) { if (running) {

View File

@ -6,14 +6,10 @@ import static org.springframework.web.reactive.function.server.RequestPredicates
import static org.springframework.web.reactive.function.server.RequestPredicates.accept; import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static run.halo.app.content.permalinks.PostPermalinkPolicy.DEFAULT_CATEGORY; import static run.halo.app.content.permalinks.PostPermalinkPolicy.DEFAULT_CATEGORY;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
@ -241,9 +237,6 @@ public class PostRouteFactory implements RouteFactory {
@Getter @Getter
static class PatternParser { static class PatternParser {
private static final Pattern PATTERN_COMPILE = Pattern.compile("([^&?]*)=\\{(.*?)\\}(&|$)"); private static final Pattern PATTERN_COMPILE = Pattern.compile("([^&?]*)=\\{(.*?)\\}(&|$)");
private static final Cache<String, Matcher> MATCHER_CACHE = CacheBuilder.newBuilder()
.maximumSize(5)
.build();
private final String pattern; private final String pattern;
private String paramName; private String paramName;
@ -252,7 +245,7 @@ public class PostRouteFactory implements RouteFactory {
PatternParser(String pattern) { PatternParser(String pattern) {
this.pattern = pattern; this.pattern = pattern;
Matcher matcher = patternToMatcher(pattern); var matcher = PATTERN_COMPILE.matcher(pattern);
if (matcher.find()) { if (matcher.find()) {
this.paramName = matcher.group(1); this.paramName = matcher.group(1);
this.placeholderName = matcher.group(2); this.placeholderName = matcher.group(2);
@ -262,14 +255,6 @@ public class PostRouteFactory implements RouteFactory {
} }
} }
Matcher patternToMatcher(String pattern) {
try {
return MATCHER_CACHE.get(pattern, () -> PATTERN_COMPILE.matcher(pattern));
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
RequestPredicate toRequestPredicate() { RequestPredicate toRequestPredicate() {
if (!this.isQueryParamPattern) { if (!this.isQueryParamPattern) {
throw new IllegalStateException("Not a query param pattern: " + pattern); throw new IllegalStateException("Not a query param pattern: " + pattern);

View File

@ -7,6 +7,7 @@ import static org.mockito.Mockito.when;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
@ -66,6 +67,14 @@ class PostRouteFactoryTest extends RouteFactoryTestSuite {
@InjectMocks @InjectMocks
private PostRouteFactory postRouteFactory; private PostRouteFactory postRouteFactory;
@Test
void shouldBeSameResultWhenParsePattenMultiply() {
var parser = new PostRouteFactory.PatternParser("/?p={slug}");
Assertions.assertTrue(parser.isQueryParamPattern());
parser = new PostRouteFactory.PatternParser("/?p={slug}");
Assertions.assertTrue(parser.isQueryParamPattern());
}
@Test @Test
void create() { void create() {
Post post = TestPost.postV1(); Post post = TestPost.postV1();