1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
24
24
import javax .servlet .http .HttpServletResponse ;
25
25
26
26
import org .springframework .lang .Nullable ;
27
+ import org .springframework .util .Assert ;
27
28
import org .springframework .web .context .support .WebApplicationObjectSupport ;
28
29
import org .springframework .web .servlet .View ;
29
30
import org .springframework .web .servlet .ViewResolver ;
@@ -58,13 +59,19 @@ public void render(@Nullable Map<String, ?> model, HttpServletRequest request, H
58
59
}
59
60
};
60
61
62
+ /** Default cache filter that always caches. */
63
+ private static final CacheFilter DEFAULT_CACHE_FILTER = (view , viewName , locale ) -> true ;
64
+
61
65
62
66
/** The maximum number of entries in the cache. */
63
67
private volatile int cacheLimit = DEFAULT_CACHE_LIMIT ;
64
68
65
69
/** Whether we should refrain from resolving views again if unresolved once. */
66
70
private boolean cacheUnresolved = true ;
67
71
72
+ /** Filter function that determines if view should be cached. */
73
+ private CacheFilter cacheFilter = DEFAULT_CACHE_FILTER ;
74
+
68
75
/** Fast access cache for Views, returning already cached instances without a global lock. */
69
76
private final Map <Object , View > viewAccessCache = new ConcurrentHashMap <>(DEFAULT_CACHE_LIMIT );
70
77
@@ -141,6 +148,21 @@ public boolean isCacheUnresolved() {
141
148
return this .cacheUnresolved ;
142
149
}
143
150
151
+ /**
152
+ * Sets the filter that determines if view should be cached.
153
+ * Default behaviour is to cache all views.
154
+ */
155
+ public void setCacheFilter (CacheFilter cacheFilter ) {
156
+ Assert .notNull (cacheFilter , "CacheFilter must not be null" );
157
+ this .cacheFilter = cacheFilter ;
158
+ }
159
+
160
+ /**
161
+ * Return filter function that determines if view should be cached.
162
+ */
163
+ public CacheFilter getCacheFilter () {
164
+ return this .cacheFilter ;
165
+ }
144
166
145
167
@ Override
146
168
@ Nullable
@@ -160,7 +182,7 @@ public View resolveViewName(String viewName, Locale locale) throws Exception {
160
182
if (view == null && this .cacheUnresolved ) {
161
183
view = UNRESOLVED_VIEW ;
162
184
}
163
- if (view != null ) {
185
+ if (view != null && this . cacheFilter . filter ( view , viewName , locale ) ) {
164
186
this .viewAccessCache .put (cacheKey , view );
165
187
this .viewCreationCache .put (cacheKey , view );
166
188
}
@@ -266,4 +288,26 @@ protected View createView(String viewName, Locale locale) throws Exception {
266
288
@ Nullable
267
289
protected abstract View loadView (String viewName , Locale locale ) throws Exception ;
268
290
291
+
292
+ /**
293
+ * Filter that determines if view should be cached.
294
+ *
295
+ * @author Sergey Galkin
296
+ * @author Arjen Poutsma
297
+ * @since 5.2
298
+ */
299
+ @ FunctionalInterface
300
+ public interface CacheFilter {
301
+
302
+ /**
303
+ * Indicates whether the given view should be cached. The name and
304
+ * locale used to resolve the view are also provided.
305
+ * @param view the view
306
+ * @param viewName the name used to resolve {@code view}
307
+ * @param locale the locale used to resolve {@code view}
308
+ * @return {@code true} if the view should be cached; {@code false}
309
+ * otherwise
310
+ */
311
+ boolean filter (View view , String viewName , Locale locale );
312
+ }
269
313
}
0 commit comments