|
15 | 15 | */ |
16 | 16 | package org.springframework.data.redis.cache; |
17 | 17 |
|
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
18 | 20 | import java.nio.charset.StandardCharsets; |
19 | 21 | import java.time.Duration; |
| 22 | +import java.util.Properties; |
20 | 23 | import java.util.function.Consumer; |
| 24 | +import java.util.logging.Logger; |
| 25 | +import java.io.FileNotFoundException; |
21 | 26 |
|
22 | 27 | import org.springframework.cache.Cache; |
23 | 28 | import org.springframework.cache.interceptor.SimpleKey; |
|
42 | 47 | * @author Christoph Strobl |
43 | 48 | * @author Mark Paluch |
44 | 49 | * @author John Blum |
| 50 | + * @author Chaelin Kwon |
45 | 51 | * @since 2.0 |
46 | 52 | */ |
47 | 53 | public class RedisCacheConfiguration { |
@@ -120,6 +126,106 @@ public static RedisCacheConfiguration defaultCacheConfig(@Nullable ClassLoader c |
120 | 126 | conversionService); |
121 | 127 | } |
122 | 128 |
|
| 129 | + private static final Logger logger = Logger.getLogger(RedisCacheConfiguration.class.getName()); |
| 130 | + |
| 131 | + public static RedisCacheConfiguration propertyCacheConfig(@Nullable ClassLoader classLoader) { |
| 132 | + |
| 133 | + DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(); |
| 134 | + registerDefaultConverters(conversionService); |
| 135 | + |
| 136 | + Properties properties = loadProperties(); |
| 137 | + |
| 138 | + Duration ttl = getValidatedDuration(properties, "ttl", Duration.ofSeconds(0)); |
| 139 | + boolean cacheNullValues = getValidatedBoolean(properties, "nullValues", true); |
| 140 | + String keyPrefix = getValidatedString(properties, "keyPrefix", "simple", new String[]{"simple", "none"}); |
| 141 | + SerializationPair<?> defaultSerializer = getValidatedSerializer(properties, "serializer", "raw", classLoader); |
| 142 | + SerializationPair<String> keySerializer = getValidatedSerializer(properties, "key.serializer", "string", classLoader); |
| 143 | + SerializationPair<?> valueSerializer = getValidatedSerializer(properties, "value.serializer", defaultSerializer, classLoader); |
| 144 | + |
| 145 | + return new RedisCacheConfiguration( |
| 146 | + TtlFunction.just(ttl), |
| 147 | + cacheNullValues, |
| 148 | + DEFAULT_ENABLE_TIME_TO_IDLE_EXPIRATION, |
| 149 | + DEFAULT_USE_PREFIX, |
| 150 | + CacheKeyPrefix.prefixed(keyPrefix), |
| 151 | + keySerializer, |
| 152 | + valueSerializer, |
| 153 | + conversionService |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + private static Properties loadProperties() { |
| 158 | + Properties properties = new Properties(); |
| 159 | + try (InputStream input = RedisCacheConfiguration.class.getClassLoader().getResourceAsStream("redis-cache.properties")) { |
| 160 | + if (input != null) { |
| 161 | + properties.load(input); |
| 162 | + } else { |
| 163 | + throw new FileNotFoundException("Property file 'redis-cache.properties' not found in the classpath"); |
| 164 | + } |
| 165 | + } catch (IOException e) { |
| 166 | + throw new RuntimeException("Failed to load redis-cache.properties", e); |
| 167 | + } |
| 168 | + return properties; |
| 169 | + } |
| 170 | + |
| 171 | + private static Duration getValidatedDuration(Properties properties, String key, Duration defaultValue) { |
| 172 | + String value = properties.getProperty(key, String.valueOf(defaultValue.getSeconds())); |
| 173 | + try { |
| 174 | + return Duration.ofSeconds(Long.parseLong(value)); |
| 175 | + } catch (NumberFormatException e) { |
| 176 | + logger.warning("Invalid " + key + " value: " + value + ". Expected a positive integer. Defaulting to " + defaultValue); |
| 177 | + return defaultValue; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private static boolean getValidatedBoolean(Properties properties, String key, boolean defaultValue) { |
| 182 | + String value = properties.getProperty(key, String.valueOf(defaultValue)); |
| 183 | + switch (value.toLowerCase()) { |
| 184 | + case "true": |
| 185 | + case "false": |
| 186 | + return Boolean.parseBoolean(value); |
| 187 | + default: |
| 188 | + logger.warning("Invalid " + key + " value: " + value + ". Expected 'true' or 'false'. Defaulting to " + defaultValue); |
| 189 | + return defaultValue; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private static String getValidatedString(Properties properties, String key, String defaultValue, String[] validValues) { |
| 194 | + String value = properties.getProperty(key, defaultValue); |
| 195 | + for (String validValue : validValues) { |
| 196 | + if (validValue.equalsIgnoreCase(value)) { |
| 197 | + return value.toLowerCase(); |
| 198 | + } |
| 199 | + } |
| 200 | + logger.warning("Invalid " + key + " value: " + value + ". Expected one of " + String.join(", ", validValues) + ". Defaulting to " + defaultValue); |
| 201 | + return defaultValue; |
| 202 | + } |
| 203 | + |
| 204 | + private static <T> SerializationPair<T> getValidatedSerializer(Properties properties, String key, Object defaultValue, @Nullable ClassLoader classLoader) { |
| 205 | + String value = properties.getProperty(key); |
| 206 | + |
| 207 | + if (value == null && defaultValue instanceof SerializationPair) { |
| 208 | + return (SerializationPair<T>) defaultValue; |
| 209 | + } |
| 210 | + if (value == null) { |
| 211 | + value = String.valueOf(defaultValue); |
| 212 | + } |
| 213 | + |
| 214 | + switch (value.toLowerCase()) { |
| 215 | + case "java": |
| 216 | + return (SerializationPair<T>) SerializationPair.fromSerializer(RedisSerializer.java(classLoader)); |
| 217 | + case "string": |
| 218 | + return (SerializationPair<T>) SerializationPair.fromSerializer(RedisSerializer.string()); |
| 219 | + case "json": |
| 220 | + return (SerializationPair<T>) SerializationPair.fromSerializer(RedisSerializer.json()); |
| 221 | + case "raw": |
| 222 | + return (SerializationPair<T>) SerializationPair.fromSerializer(RedisSerializer.byteArray()); |
| 223 | + default: |
| 224 | + logger.warning("Invalid " + key + " value: " + value + ". Expected 'raw', 'java', 'string', or 'json'. Defaulting to 'raw'."); |
| 225 | + return (SerializationPair<T>) SerializationPair.fromSerializer(RedisSerializer.byteArray()); |
| 226 | + } |
| 227 | + } |
| 228 | + |
123 | 229 | private final boolean cacheNullValues; |
124 | 230 | private final boolean enableTimeToIdle; |
125 | 231 | private final boolean usePrefix; |
|
0 commit comments