diff --git a/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java b/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java index a6e9ee02ac35ecccaed4e9c264fb66a7e7253e4e..df41bf5f50d26dfcd69d634c02af04177edec482 100644 --- a/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java +++ b/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -95,6 +95,7 @@ public class ConcurrentMapCache extends AbstractValueAdaptingCache { * (adapting them to an internal null holder value) * @param serialization the {@link SerializationDelegate} to use * to serialize cache entry or {@code null} to store the reference + * @since 4.3 */ protected ConcurrentMapCache(String name, ConcurrentMap store, boolean allowNullValues, SerializationDelegate serialization) { @@ -107,13 +108,15 @@ public class ConcurrentMapCache extends AbstractValueAdaptingCache { this.serialization = serialization; } + /** * Return whether this cache stores a copy of each entry ({@code true}) or * a reference ({@code false}, default). If store by value is enabled, each * entry in the cache must be serializable. + * @since 4.3 */ public final boolean isStoreByValue() { - return this.serialization != null; + return (this.serialization != null); } @Override diff --git a/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheManager.java b/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheManager.java index b4584f5ed19526c10d59655bd117fbee3a32ba82..cce957b3779ac3f6c040b47b37f7662b77cbd7ec 100644 --- a/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheManager.java +++ b/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheManager.java @@ -39,7 +39,7 @@ import org.springframework.core.serializer.support.SerializationDelegate; * caching scenarios. For advanced local caching needs, consider * {@link org.springframework.cache.jcache.JCacheCacheManager}, * {@link org.springframework.cache.ehcache.EhCacheCacheManager}, - * {@link com.github.benmanes.caffeine.cache.CaffeineCacheManager} or + * {@link org.springframework.cache.caffeine.CaffeineCacheManager} or * {@link org.springframework.cache.guava.GuavaCacheManager}. * * @author Juergen Hoeller @@ -106,9 +106,7 @@ public class ConcurrentMapCacheManager implements CacheManager, BeanClassLoaderA if (allowNullValues != this.allowNullValues) { this.allowNullValues = allowNullValues; // Need to recreate all Cache instances with the new null-value configuration... - for (Map.Entry entry : this.cacheMap.entrySet()) { - entry.setValue(createConcurrentMapCache(entry.getKey())); - } + recreateCaches(); } } @@ -127,14 +125,13 @@ public class ConcurrentMapCacheManager implements CacheManager, BeanClassLoaderA * contract is required on cached values. *

Note: A change of the store-by-value setting will reset all existing caches, * if any, to reconfigure them with the new store-by-value requirement. + * @since 4.3 */ public void setStoreByValue(boolean storeByValue) { if (storeByValue != this.storeByValue) { this.storeByValue = storeByValue; // Need to recreate all Cache instances with the new store-by-value configuration... - for (Map.Entry entry : this.cacheMap.entrySet()) { - entry.setValue(createConcurrentMapCache(entry.getKey())); - } + recreateCaches(); } } @@ -142,6 +139,7 @@ public class ConcurrentMapCacheManager implements CacheManager, BeanClassLoaderA * Return whether this cache manager stores a copy of each entry or * a reference for all its caches. If store by value is enabled, any * cache entry must be serializable. + * @since 4.3 */ public boolean isStoreByValue() { return this.storeByValue; @@ -150,8 +148,13 @@ public class ConcurrentMapCacheManager implements CacheManager, BeanClassLoaderA @Override public void setBeanClassLoader(ClassLoader classLoader) { this.serialization = new SerializationDelegate(classLoader); + // Need to recreate all Cache instances with new ClassLoader in store-by-value mode... + if (isStoreByValue()) { + recreateCaches(); + } } + @Override public Collection getCacheNames() { return Collections.unmodifiableSet(this.cacheMap.keySet()); @@ -172,14 +175,19 @@ public class ConcurrentMapCacheManager implements CacheManager, BeanClassLoaderA return cache; } + private void recreateCaches() { + for (Map.Entry entry : this.cacheMap.entrySet()) { + entry.setValue(createConcurrentMapCache(entry.getKey())); + } + } + /** * Create a new ConcurrentMapCache instance for the specified cache name. * @param name the name of the cache * @return the ConcurrentMapCache (or a decorator thereof) */ protected Cache createConcurrentMapCache(String name) { - SerializationDelegate actualSerialization = - this.storeByValue ? this.serialization : null; + SerializationDelegate actualSerialization = (isStoreByValue() ? this.serialization : null); return new ConcurrentMapCache(name, new ConcurrentHashMap(256), isAllowNullValues(), actualSerialization);