001/* 002 * Copyright 2002-2020 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.cache.concurrent; 018 019import java.util.Arrays; 020import java.util.Collection; 021import java.util.Collections; 022import java.util.Map; 023import java.util.concurrent.ConcurrentHashMap; 024import java.util.concurrent.ConcurrentMap; 025 026import org.springframework.beans.factory.BeanClassLoaderAware; 027import org.springframework.cache.Cache; 028import org.springframework.cache.CacheManager; 029import org.springframework.core.serializer.support.SerializationDelegate; 030import org.springframework.lang.Nullable; 031 032/** 033 * {@link CacheManager} implementation that lazily builds {@link ConcurrentMapCache} 034 * instances for each {@link #getCache} request. Also supports a 'static' mode where 035 * the set of cache names is pre-defined through {@link #setCacheNames}, with no 036 * dynamic creation of further cache regions at runtime. 037 * 038 * <p>Note: This is by no means a sophisticated CacheManager; it comes with no 039 * cache configuration options. However, it may be useful for testing or simple 040 * caching scenarios. For advanced local caching needs, consider 041 * {@link org.springframework.cache.jcache.JCacheCacheManager}, 042 * {@link org.springframework.cache.ehcache.EhCacheCacheManager}, 043 * {@link org.springframework.cache.caffeine.CaffeineCacheManager}. 044 * 045 * @author Juergen Hoeller 046 * @since 3.1 047 * @see ConcurrentMapCache 048 */ 049public class ConcurrentMapCacheManager implements CacheManager, BeanClassLoaderAware { 050 051 private final ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<>(16); 052 053 private boolean dynamic = true; 054 055 private boolean allowNullValues = true; 056 057 private boolean storeByValue = false; 058 059 @Nullable 060 private SerializationDelegate serialization; 061 062 063 /** 064 * Construct a dynamic ConcurrentMapCacheManager, 065 * lazily creating cache instances as they are being requested. 066 */ 067 public ConcurrentMapCacheManager() { 068 } 069 070 /** 071 * Construct a static ConcurrentMapCacheManager, 072 * managing caches for the specified cache names only. 073 */ 074 public ConcurrentMapCacheManager(String... cacheNames) { 075 setCacheNames(Arrays.asList(cacheNames)); 076 } 077 078 079 /** 080 * Specify the set of cache names for this CacheManager's 'static' mode. 081 * <p>The number of caches and their names will be fixed after a call to this method, 082 * with no creation of further cache regions at runtime. 083 * <p>Calling this with a {@code null} collection argument resets the 084 * mode to 'dynamic', allowing for further creation of caches again. 085 */ 086 public void setCacheNames(@Nullable Collection<String> cacheNames) { 087 if (cacheNames != null) { 088 for (String name : cacheNames) { 089 this.cacheMap.put(name, createConcurrentMapCache(name)); 090 } 091 this.dynamic = false; 092 } 093 else { 094 this.dynamic = true; 095 } 096 } 097 098 /** 099 * Specify whether to accept and convert {@code null} values for all caches 100 * in this cache manager. 101 * <p>Default is "true", despite ConcurrentHashMap itself not supporting {@code null} 102 * values. An internal holder object will be used to store user-level {@code null}s. 103 * <p>Note: A change of the null-value setting will reset all existing caches, 104 * if any, to reconfigure them with the new null-value requirement. 105 */ 106 public void setAllowNullValues(boolean allowNullValues) { 107 if (allowNullValues != this.allowNullValues) { 108 this.allowNullValues = allowNullValues; 109 // Need to recreate all Cache instances with the new null-value configuration... 110 recreateCaches(); 111 } 112 } 113 114 /** 115 * Return whether this cache manager accepts and converts {@code null} values 116 * for all of its caches. 117 */ 118 public boolean isAllowNullValues() { 119 return this.allowNullValues; 120 } 121 122 /** 123 * Specify whether this cache manager stores a copy of each entry ({@code true} 124 * or the reference ({@code false} for all of its caches. 125 * <p>Default is "false" so that the value itself is stored and no serializable 126 * contract is required on cached values. 127 * <p>Note: A change of the store-by-value setting will reset all existing caches, 128 * if any, to reconfigure them with the new store-by-value requirement. 129 * @since 4.3 130 */ 131 public void setStoreByValue(boolean storeByValue) { 132 if (storeByValue != this.storeByValue) { 133 this.storeByValue = storeByValue; 134 // Need to recreate all Cache instances with the new store-by-value configuration... 135 recreateCaches(); 136 } 137 } 138 139 /** 140 * Return whether this cache manager stores a copy of each entry or 141 * a reference for all its caches. If store by value is enabled, any 142 * cache entry must be serializable. 143 * @since 4.3 144 */ 145 public boolean isStoreByValue() { 146 return this.storeByValue; 147 } 148 149 @Override 150 public void setBeanClassLoader(ClassLoader classLoader) { 151 this.serialization = new SerializationDelegate(classLoader); 152 // Need to recreate all Cache instances with new ClassLoader in store-by-value mode... 153 if (isStoreByValue()) { 154 recreateCaches(); 155 } 156 } 157 158 159 @Override 160 public Collection<String> getCacheNames() { 161 return Collections.unmodifiableSet(this.cacheMap.keySet()); 162 } 163 164 @Override 165 @Nullable 166 public Cache getCache(String name) { 167 Cache cache = this.cacheMap.get(name); 168 if (cache == null && this.dynamic) { 169 synchronized (this.cacheMap) { 170 cache = this.cacheMap.get(name); 171 if (cache == null) { 172 cache = createConcurrentMapCache(name); 173 this.cacheMap.put(name, cache); 174 } 175 } 176 } 177 return cache; 178 } 179 180 private void recreateCaches() { 181 for (Map.Entry<String, Cache> entry : this.cacheMap.entrySet()) { 182 entry.setValue(createConcurrentMapCache(entry.getKey())); 183 } 184 } 185 186 /** 187 * Create a new ConcurrentMapCache instance for the specified cache name. 188 * @param name the name of the cache 189 * @return the ConcurrentMapCache (or a decorator thereof) 190 */ 191 protected Cache createConcurrentMapCache(String name) { 192 SerializationDelegate actualSerialization = (isStoreByValue() ? this.serialization : null); 193 return new ConcurrentMapCache(name, new ConcurrentHashMap<>(256), isAllowNullValues(), actualSerialization); 194 } 195 196}