|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2011 the original author or authors. |
| 2 | + * Copyright 2002-2012 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.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.web.servlet.view;
|
18 | 18 |
|
19 |
| -import java.util.HashMap; |
| 19 | +import java.util.LinkedHashMap; |
20 | 20 | import java.util.Locale;
|
21 | 21 | import java.util.Map;
|
22 | 22 |
|
|
39 | 39 | */
|
40 | 40 | public abstract class AbstractCachingViewResolver extends WebApplicationObjectSupport implements ViewResolver {
|
41 | 41 |
|
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; |
44 | 47 |
|
45 | 48 | /** Whether we should refrain from resolving views again if unresolved once */
|
46 | 49 | private boolean cacheUnresolved = true;
|
47 | 50 |
|
48 | 51 | /** 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 | + } |
50 | 68 |
|
| 69 | + /** |
| 70 | + * Return the maximum number of entries for the view cache. |
| 71 | + */ |
| 72 | + public int getCacheLimit() { |
| 73 | + return this.cacheLimit; |
| 74 | + } |
51 | 75 |
|
52 | 76 | /**
|
53 | 77 | * 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. |
54 | 80 | * <p>Default is "true": caching is enabled.
|
55 | 81 | * Disable this only for debugging and development.
|
56 |
| - * <p><b>Warning: Disabling caching can severely impact performance.</b> |
57 | 82 | */
|
58 | 83 | public void setCache(boolean cache) {
|
59 |
| - this.cache = cache; |
| 84 | + this.cacheLimit = (cache ? DEFAULT_CACHE_LIMIT : 0); |
60 | 85 | }
|
61 | 86 |
|
62 | 87 | /**
|
63 | 88 | * Return if caching is enabled.
|
64 | 89 | */
|
65 | 90 | public boolean isCache() {
|
66 |
| - return this.cache; |
| 91 | + return (this.cacheLimit > 0); |
67 | 92 | }
|
68 | 93 |
|
69 | 94 | /**
|
@@ -134,7 +159,7 @@ protected Object getCacheKey(String viewName, Locale locale) {
|
134 | 159 | * @param locale the locale for which the view object should be removed
|
135 | 160 | */
|
136 | 161 | public void removeFromCache(String viewName, Locale locale) {
|
137 |
| - if (!this.cache) { |
| 162 | + if (!isCache()) { |
138 | 163 | logger.warn("View caching is SWITCHED OFF -- removal not necessary");
|
139 | 164 | }
|
140 | 165 | else {
|
|
0 commit comments