001/* 002 * Copyright 2012-2018 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 * http://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.boot.autoconfigure.cache; 018 019import java.time.Duration; 020import java.util.ArrayList; 021import java.util.List; 022 023import org.springframework.boot.context.properties.ConfigurationProperties; 024import org.springframework.core.io.Resource; 025import org.springframework.util.Assert; 026 027/** 028 * Configuration properties for the cache abstraction. 029 * 030 * @author Stephane Nicoll 031 * @author EddĂș MelĂ©ndez 032 * @author Ryon Day 033 * @since 1.3.0 034 */ 035@ConfigurationProperties(prefix = "spring.cache") 036public class CacheProperties { 037 038 /** 039 * Cache type. By default, auto-detected according to the environment. 040 */ 041 private CacheType type; 042 043 /** 044 * Comma-separated list of cache names to create if supported by the underlying cache 045 * manager. Usually, this disables the ability to create additional caches on-the-fly. 046 */ 047 private List<String> cacheNames = new ArrayList<>(); 048 049 private final Caffeine caffeine = new Caffeine(); 050 051 private final Couchbase couchbase = new Couchbase(); 052 053 private final EhCache ehcache = new EhCache(); 054 055 private final Infinispan infinispan = new Infinispan(); 056 057 private final JCache jcache = new JCache(); 058 059 private final Redis redis = new Redis(); 060 061 public CacheType getType() { 062 return this.type; 063 } 064 065 public void setType(CacheType mode) { 066 this.type = mode; 067 } 068 069 public List<String> getCacheNames() { 070 return this.cacheNames; 071 } 072 073 public void setCacheNames(List<String> cacheNames) { 074 this.cacheNames = cacheNames; 075 } 076 077 public Caffeine getCaffeine() { 078 return this.caffeine; 079 } 080 081 public Couchbase getCouchbase() { 082 return this.couchbase; 083 } 084 085 public EhCache getEhcache() { 086 return this.ehcache; 087 } 088 089 public Infinispan getInfinispan() { 090 return this.infinispan; 091 } 092 093 public JCache getJcache() { 094 return this.jcache; 095 } 096 097 public Redis getRedis() { 098 return this.redis; 099 } 100 101 /** 102 * Resolve the config location if set. 103 * @param config the config resource 104 * @return the location or {@code null} if it is not set 105 * @throws IllegalArgumentException if the config attribute is set to an unknown 106 * location 107 */ 108 public Resource resolveConfigLocation(Resource config) { 109 if (config != null) { 110 Assert.isTrue(config.exists(), () -> "Cache configuration does not exist '" 111 + config.getDescription() + "'"); 112 return config; 113 } 114 return null; 115 } 116 117 /** 118 * Caffeine specific cache properties. 119 */ 120 public static class Caffeine { 121 122 /** 123 * The spec to use to create caches. See CaffeineSpec for more details on the spec 124 * format. 125 */ 126 private String spec; 127 128 public String getSpec() { 129 return this.spec; 130 } 131 132 public void setSpec(String spec) { 133 this.spec = spec; 134 } 135 136 } 137 138 /** 139 * Couchbase specific cache properties. 140 */ 141 public static class Couchbase { 142 143 /** 144 * Entry expiration. By default the entries never expire. Note that this value is 145 * ultimately converted to seconds. 146 */ 147 private Duration expiration; 148 149 public Duration getExpiration() { 150 return this.expiration; 151 } 152 153 public void setExpiration(Duration expiration) { 154 this.expiration = expiration; 155 } 156 157 } 158 159 /** 160 * EhCache specific cache properties. 161 */ 162 public static class EhCache { 163 164 /** 165 * The location of the configuration file to use to initialize EhCache. 166 */ 167 private Resource config; 168 169 public Resource getConfig() { 170 return this.config; 171 } 172 173 public void setConfig(Resource config) { 174 this.config = config; 175 } 176 177 } 178 179 /** 180 * Infinispan specific cache properties. 181 */ 182 public static class Infinispan { 183 184 /** 185 * The location of the configuration file to use to initialize Infinispan. 186 */ 187 private Resource config; 188 189 public Resource getConfig() { 190 return this.config; 191 } 192 193 public void setConfig(Resource config) { 194 this.config = config; 195 } 196 197 } 198 199 /** 200 * JCache (JSR-107) specific cache properties. 201 */ 202 public static class JCache { 203 204 /** 205 * The location of the configuration file to use to initialize the cache manager. 206 * The configuration file is dependent of the underlying cache implementation. 207 */ 208 private Resource config; 209 210 /** 211 * Fully qualified name of the CachingProvider implementation to use to retrieve 212 * the JSR-107 compliant cache manager. Needed only if more than one JSR-107 213 * implementation is available on the classpath. 214 */ 215 private String provider; 216 217 public String getProvider() { 218 return this.provider; 219 } 220 221 public void setProvider(String provider) { 222 this.provider = provider; 223 } 224 225 public Resource getConfig() { 226 return this.config; 227 } 228 229 public void setConfig(Resource config) { 230 this.config = config; 231 } 232 233 } 234 235 /** 236 * Redis-specific cache properties. 237 */ 238 public static class Redis { 239 240 /** 241 * Entry expiration. By default the entries never expire. 242 */ 243 private Duration timeToLive; 244 245 /** 246 * Allow caching null values. 247 */ 248 private boolean cacheNullValues = true; 249 250 /** 251 * Key prefix. 252 */ 253 private String keyPrefix; 254 255 /** 256 * Whether to use the key prefix when writing to Redis. 257 */ 258 private boolean useKeyPrefix = true; 259 260 public Duration getTimeToLive() { 261 return this.timeToLive; 262 } 263 264 public void setTimeToLive(Duration timeToLive) { 265 this.timeToLive = timeToLive; 266 } 267 268 public boolean isCacheNullValues() { 269 return this.cacheNullValues; 270 } 271 272 public void setCacheNullValues(boolean cacheNullValues) { 273 this.cacheNullValues = cacheNullValues; 274 } 275 276 public String getKeyPrefix() { 277 return this.keyPrefix; 278 } 279 280 public void setKeyPrefix(String keyPrefix) { 281 this.keyPrefix = keyPrefix; 282 } 283 284 public boolean isUseKeyPrefix() { 285 return this.useKeyPrefix; 286 } 287 288 public void setUseKeyPrefix(boolean useKeyPrefix) { 289 this.useKeyPrefix = useKeyPrefix; 290 } 291 292 } 293 294}