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,19 +59,22 @@ 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
71
- /** Filter function which determines if view should be cached. */
72
- private ViewCacheFilter viewCacheFilter = (viewName , view , locale ) -> true ;
73
-
74
78
/** Map from view key to View instance, synchronized for View creation. */
75
79
@ SuppressWarnings ("serial" )
76
80
private final Map <Object , View > viewCreationCache =
@@ -138,28 +142,28 @@ public void setCacheUnresolved(boolean cacheUnresolved) {
138
142
}
139
143
140
144
/**
141
- * Filter function which determines if view should be cached.
142
- * Default behaviour is to cache all views.
145
+ * Return if caching of unresolved views is enabled.
143
146
*/
144
- public void setViewCacheFilter ( ViewCacheFilter cacheFilter ) {
145
- this .viewCacheFilter = cacheFilter ;
147
+ public boolean isCacheUnresolved ( ) {
148
+ return this .cacheUnresolved ;
146
149
}
147
150
148
151
/**
149
- * Return filter function which determines if view should be cached.
152
+ * Sets the filter that determines if view should be cached.
153
+ * Default behaviour is to cache all views.
150
154
*/
151
- public ViewCacheFilter getViewCacheFilter () {
152
- return this .viewCacheFilter ;
155
+ public void setCacheFilter (CacheFilter cacheFilter ) {
156
+ Assert .notNull (cacheFilter , "CacheFilter must not be null" );
157
+ this .cacheFilter = cacheFilter ;
153
158
}
154
159
155
160
/**
156
- * Return if caching of unresolved views is enabled .
161
+ * Return filter function that determines if view should be cached .
157
162
*/
158
- public boolean isCacheUnresolved () {
159
- return this .cacheUnresolved ;
163
+ public CacheFilter getCacheFilter () {
164
+ return this .cacheFilter ;
160
165
}
161
166
162
-
163
167
@ Override
164
168
@ Nullable
165
169
public View resolveViewName (String viewName , Locale locale ) throws Exception {
@@ -178,7 +182,7 @@ public View resolveViewName(String viewName, Locale locale) throws Exception {
178
182
if (view == null && this .cacheUnresolved ) {
179
183
view = UNRESOLVED_VIEW ;
180
184
}
181
- if (view != null && this .viewCacheFilter .filter (viewName , view , locale )) {
185
+ if (view != null && this .cacheFilter .filter (view , viewName , locale )) {
182
186
this .viewAccessCache .put (cacheKey , view );
183
187
this .viewCreationCache .put (cacheKey , view );
184
188
}
@@ -284,4 +288,26 @@ protected View createView(String viewName, Locale locale) throws Exception {
284
288
@ Nullable
285
289
protected abstract View loadView (String viewName , Locale locale ) throws Exception ;
286
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
+ }
287
313
}
0 commit comments