From 9deaefe74d9b79d22328ae0f1ede0830ac30ce20 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 12 Dec 2012 03:12:49 +0100 Subject: [PATCH] AbstractCachingViewResolver uses a cache limit of 1024 by default, avoiding overflow for redirect URLs Issue: SPR-10065 --- .../view/AbstractCachingViewResolver.java | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/AbstractCachingViewResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/AbstractCachingViewResolver.java index 7bf24fba2d4..71706e80523 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/AbstractCachingViewResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/AbstractCachingViewResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -16,7 +16,7 @@ package org.springframework.web.servlet.view; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; @@ -39,31 +39,56 @@ import org.springframework.web.servlet.ViewResolver; */ public abstract class AbstractCachingViewResolver extends WebApplicationObjectSupport implements ViewResolver { - /** Whether we should cache views, once resolved */ - private boolean cache = true; + /** Default maximum number of entries for the view cache: 1024 */ + public static final int DEFAULT_CACHE_LIMIT = 1024; + + + private volatile int cacheLimit = DEFAULT_CACHE_LIMIT; /** Whether we should refrain from resolving views again if unresolved once */ private boolean cacheUnresolved = true; /** Map from view key to View instance */ - private final Map viewCache = new HashMap(); + private final Map viewCache = + new LinkedHashMap(DEFAULT_CACHE_LIMIT, 0.75f, true) { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > getCacheLimit(); + } + }; + /** + * Specify the maximum number of entries for the view cache. + * Default is 1024. + */ + public void setCacheLimit(int cacheLimit) { + this.cacheLimit = cacheLimit; + } + + /** + * Return the maximum number of entries for the view cache. + */ + public int getCacheLimit() { + return this.cacheLimit; + } + /** * Enable or disable caching. + *

This is equivalent to setting the {@link #setCacheLimit "cacheLimit"} + * property to the default limit (1024) or to 0, respectively. *

Default is "true": caching is enabled. * Disable this only for debugging and development. - *

Warning: Disabling caching can severely impact performance. */ public void setCache(boolean cache) { - this.cache = cache; + this.cacheLimit = (cache ? DEFAULT_CACHE_LIMIT : 0); } /** * Return if caching is enabled. */ public boolean isCache() { - return this.cache; + return (this.cacheLimit > 0); } /** @@ -134,7 +159,7 @@ public abstract class AbstractCachingViewResolver extends WebApplicationObjectSu * @param locale the locale for which the view object should be removed */ public void removeFromCache(String viewName, Locale locale) { - if (!this.cache) { + if (!isCache()) { logger.warn("View caching is SWITCHED OFF -- removal not necessary"); } else {