Skip to content

Commit

Permalink
Add filter to AbstractCachingViewResolver
Browse files Browse the repository at this point in the history
This commit introduces a filter that specifies whether a View should be
cached by the AbstractCachingViewResolver or not.

Closes gh-22391
  • Loading branch information
fred84 authored and poutsma committed Aug 1, 2019
1 parent 4612544 commit 59aee92
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
Expand Up @@ -68,6 +68,9 @@ public void render(@Nullable Map<String, ?> model, HttpServletRequest request, H
/** Fast access cache for Views, returning already cached instances without a global lock. */
private final Map<Object, View> viewAccessCache = new ConcurrentHashMap<>(DEFAULT_CACHE_LIMIT);

/** Filter function which determines if view should be cached. */
private ViewCacheFilter viewCacheFilter = (viewName, view, locale) -> true;

/** Map from view key to View instance, synchronized for View creation. */
@SuppressWarnings("serial")
private final Map<Object, View> viewCreationCache =
Expand Down Expand Up @@ -134,6 +137,21 @@ public void setCacheUnresolved(boolean cacheUnresolved) {
this.cacheUnresolved = cacheUnresolved;
}

/**
* Filter function which determines if view should be cached.
* Default behaviour is to cache all views.
*/
public void setViewCacheFilter(ViewCacheFilter cacheFilter) {
this.viewCacheFilter = cacheFilter;
}

/**
* Return filter function which determines if view should be cached.
*/
public ViewCacheFilter getViewCacheFilter() {
return this.viewCacheFilter;
}

/**
* Return if caching of unresolved views is enabled.
*/
Expand All @@ -160,7 +178,7 @@ public View resolveViewName(String viewName, Locale locale) throws Exception {
if (view == null && this.cacheUnresolved) {
view = UNRESOLVED_VIEW;
}
if (view != null) {
if (view != null && this.viewCacheFilter.filter(viewName, view, locale)) {
this.viewAccessCache.put(cacheKey, view);
this.viewCreationCache.put(cacheKey, view);
}
Expand Down
@@ -0,0 +1,32 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.web.servlet.view;

import java.util.Locale;

import org.springframework.web.servlet.View;

/**
* Filter which determines if view should be cached in {@link AbstractCachingViewResolver}.
*
* @author Sergey Galkin
*/
@FunctionalInterface
public interface ViewCacheFilter {

boolean filter(String viewName, View view, Locale locale);
}
@@ -0,0 +1,86 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.web.servlet.view;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Locale;
import org.junit.Test;
import org.springframework.web.servlet.View;

public class ViewResolverCacheFilterTest {

private interface ViewLoader {
View load(String viewName);
}

private static class TestViewResolver extends AbstractCachingViewResolver {

private final ViewLoader viewLoader;

private TestViewResolver(ViewLoader viewLoader) {
this.viewLoader = viewLoader;
}

@Override
protected View loadView(String viewName, Locale locale) {
return viewLoader.load(viewName);
}
}

private final static String VIEW_NAME = "name";
private final ViewLoader viewLoader = mock(ViewLoader.class);
private final TestViewResolver resolver = new TestViewResolver(viewLoader);

@Test
public void viewWillBePlacedInCache() throws Exception {
resolver.setViewCacheFilter((n, v, l) -> true);

resolver.resolveViewName(VIEW_NAME, Locale.ENGLISH);
resolver.resolveViewName(VIEW_NAME, Locale.ENGLISH);

verify(viewLoader, times(1)).load(any());
}

@Test
public void viewWillNotBePlacedInCached() throws Exception {
resolver.setViewCacheFilter((n, v, l) -> false);

resolver.resolveViewName(VIEW_NAME, Locale.ENGLISH);
resolver.resolveViewName(VIEW_NAME, Locale.ENGLISH);

verify(viewLoader, times(2)).load(any());
}

@Test
public void verifyPassedParamsToFilter() throws Exception {
View view = mock(View.class);
when(viewLoader.load(any())).thenReturn(view);

ViewCacheFilter filter = mock(ViewCacheFilter.class);
resolver.setViewCacheFilter(filter);

resolver.resolveViewName(VIEW_NAME, Locale.ENGLISH);

verify(filter, times(1)).filter(eq(VIEW_NAME), eq(view), eq(Locale.ENGLISH));
}
}

0 comments on commit 59aee92

Please sign in to comment.