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