Skip to content

Commit 8d6548e

Browse files
committed
Cache resolved error template view names
Fixes gh-5933
1 parent db96919 commit 8d6548e

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultErrorViewResolver.java

+45-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import java.util.Collections;
2020
import java.util.HashMap;
21+
import java.util.LinkedHashMap;
2122
import java.util.List;
2223
import java.util.Map;
24+
import java.util.concurrent.ConcurrentHashMap;
2325

2426
import javax.servlet.http.HttpServletRequest;
2527
import javax.servlet.http.HttpServletResponse;
@@ -66,6 +68,10 @@ public class DefaultErrorViewResolver implements ErrorViewResolver, Ordered {
6668
SERIES_VIEWS = Collections.unmodifiableMap(views);
6769
}
6870

71+
private static final int CACHE_LIMIT = 1024;
72+
73+
private static final Object UNRESOLVED = new Object();
74+
6975
private ApplicationContext applicationContext;
7076

7177
private final ResourceProperties resourceProperties;
@@ -74,6 +80,30 @@ public class DefaultErrorViewResolver implements ErrorViewResolver, Ordered {
7480

7581
private int order = Ordered.LOWEST_PRECEDENCE;
7682

83+
/**
84+
* resolved template views, returning already cached instances without a global lock.
85+
*/
86+
private final Map<Object, Object> resolved = new ConcurrentHashMap<Object, Object>(
87+
CACHE_LIMIT);
88+
89+
/**
90+
* Map from view name resolve template view, synchronized when accessed.
91+
*/
92+
@SuppressWarnings("serial")
93+
private final Map<Object, Object> cache = new LinkedHashMap<Object, Object>(
94+
CACHE_LIMIT, 0.75f, true) {
95+
96+
@Override
97+
protected boolean removeEldestEntry(Map.Entry<Object, Object> eldest) {
98+
if (size() > CACHE_LIMIT) {
99+
DefaultErrorViewResolver.this.resolved.remove(eldest.getKey());
100+
return true;
101+
}
102+
return false;
103+
}
104+
105+
};
106+
77107
/**
78108
* Create a new {@link DefaultErrorViewResolver} instance.
79109
* @param applicationContext the source application context
@@ -120,11 +150,25 @@ private ModelAndView resolve(String viewName, Map<String, Object> model) {
120150
}
121151

122152
private ModelAndView resolveTemplate(String viewName, Map<String, Object> model) {
153+
Object resolved = this.resolved.get(viewName);
154+
if (resolved == null) {
155+
synchronized (this.cache) {
156+
resolved = resolveTemplateViewName(viewName);
157+
resolved = (resolved == null ? UNRESOLVED : resolved);
158+
this.resolved.put(viewName, resolved);
159+
this.cache.put(viewName, resolved);
160+
}
161+
}
162+
return (resolved == UNRESOLVED ? null
163+
: new ModelAndView((String) resolved, model));
164+
}
165+
166+
private String resolveTemplateViewName(String viewName) {
123167
for (TemplateAvailabilityProvider templateAvailabilityProvider : this.templateAvailabilityProviders) {
124168
if (templateAvailabilityProvider.isTemplateAvailable("error/" + viewName,
125169
this.applicationContext.getEnvironment(),
126170
this.applicationContext.getClassLoader(), this.applicationContext)) {
127-
return new ModelAndView("error/" + viewName, model);
171+
return "error/" + viewName;
128172
}
129173
}
130174
return null;

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DefaultErrorViewResolverTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import static org.mockito.Matchers.any;
4848
import static org.mockito.Matchers.eq;
4949
import static org.mockito.Mockito.mock;
50+
import static org.mockito.Mockito.times;
5051
import static org.mockito.Mockito.verify;
5152
import static org.mockito.Mockito.verifyNoMoreInteractions;
5253

@@ -198,6 +199,21 @@ public void resolveWhenExactResourceMatchAndSeriesTemplateMatchShouldFavorResour
198199
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE);
199200
}
200201

202+
@Test
203+
public void resolveShouldCacheTemplate() throws Exception {
204+
given(this.templateAvailabilityProvider.isTemplateAvailable(eq("error/4xx"),
205+
any(Environment.class), any(ClassLoader.class),
206+
any(ResourceLoader.class))).willReturn(true);
207+
for (int i = 0; i < 10; i++) {
208+
ModelAndView resolved = this.resolver.resolveErrorView(this.request,
209+
HttpStatus.NOT_FOUND, this.model);
210+
assertThat(resolved.getViewName()).isEqualTo("error/4xx");
211+
}
212+
verify(this.templateAvailabilityProvider, times(1)).isTemplateAvailable(
213+
eq("error/4xx"), any(Environment.class), any(ClassLoader.class),
214+
any(ResourceLoader.class));
215+
}
216+
201217
@Test
202218
public void orderShouldBeLowest() throws Exception {
203219
assertThat(this.resolver.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE);

0 commit comments

Comments
 (0)