Skip to content

Commit f19bc57

Browse files
committed
AbstractCachingViewResolver uses a cache limit of 1024 by default, avoiding overflow for redirect URLs
Issue: SPR-10065
1 parent 8134851 commit f19bc57

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/AbstractCachingViewResolver.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.web.servlet.view;
1818

19-
import java.util.HashMap;
19+
import java.util.LinkedHashMap;
2020
import java.util.Locale;
2121
import java.util.Map;
2222

@@ -39,31 +39,56 @@
3939
*/
4040
public abstract class AbstractCachingViewResolver extends WebApplicationObjectSupport implements ViewResolver {
4141

42-
/** Whether we should cache views, once resolved */
43-
private boolean cache = true;
42+
/** Default maximum number of entries for the view cache: 1024 */
43+
public static final int DEFAULT_CACHE_LIMIT = 1024;
44+
45+
46+
private volatile int cacheLimit = DEFAULT_CACHE_LIMIT;
4447

4548
/** Whether we should refrain from resolving views again if unresolved once */
4649
private boolean cacheUnresolved = true;
4750

4851
/** Map from view key to View instance */
49-
private final Map<Object, View> viewCache = new HashMap<Object, View>();
52+
private final Map<Object, View> viewCache =
53+
new LinkedHashMap<Object, View>(DEFAULT_CACHE_LIMIT, 0.75f, true) {
54+
@Override
55+
protected boolean removeEldestEntry(Map.Entry<Object, View> eldest) {
56+
return size() > getCacheLimit();
57+
}
58+
};
59+
60+
61+
/**
62+
* Specify the maximum number of entries for the view cache.
63+
* Default is 1024.
64+
*/
65+
public void setCacheLimit(int cacheLimit) {
66+
this.cacheLimit = cacheLimit;
67+
}
5068

69+
/**
70+
* Return the maximum number of entries for the view cache.
71+
*/
72+
public int getCacheLimit() {
73+
return this.cacheLimit;
74+
}
5175

5276
/**
5377
* Enable or disable caching.
78+
* <p>This is equivalent to setting the {@link #setCacheLimit "cacheLimit"}
79+
* property to the default limit (1024) or to 0, respectively.
5480
* <p>Default is "true": caching is enabled.
5581
* Disable this only for debugging and development.
56-
* <p><b>Warning: Disabling caching can severely impact performance.</b>
5782
*/
5883
public void setCache(boolean cache) {
59-
this.cache = cache;
84+
this.cacheLimit = (cache ? DEFAULT_CACHE_LIMIT : 0);
6085
}
6186

6287
/**
6388
* Return if caching is enabled.
6489
*/
6590
public boolean isCache() {
66-
return this.cache;
91+
return (this.cacheLimit > 0);
6792
}
6893

6994
/**
@@ -134,7 +159,7 @@ protected Object getCacheKey(String viewName, Locale locale) {
134159
* @param locale the locale for which the view object should be removed
135160
*/
136161
public void removeFromCache(String viewName, Locale locale) {
137-
if (!this.cache) {
162+
if (!isCache()) {
138163
logger.warn("View caching is SWITCHED OFF -- removal not necessary");
139164
}
140165
else {

0 commit comments

Comments
 (0)