From 1606f47e954c6aa25254ac8c194a255f07c13302 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Wed, 29 Jan 2025 10:05:32 +0100 Subject: [PATCH] Convert Redis Cache to @ConfigMapping Part of #45446 --- extensions/redis-cache/deployment/pom.xml | 3 -- .../redis/deployment/RedisCacheProcessor.java | 22 ++++---- extensions/redis-cache/runtime/pom.xml | 3 -- .../runtime/RedisCacheBuildRecorder.java | 7 +-- .../runtime/RedisCacheBuildTimeConfig.java | 9 ++-- .../redis/runtime/RedisCacheInfoBuilder.java | 54 +++++++++---------- .../runtime/RedisCacheRuntimeConfig.java | 23 +++----- .../runtime/RedisCachesBuildTimeConfig.java | 19 +++---- ...fig.java => RedisCachesRuntimeConfig.java} | 16 +++--- 9 files changed, 72 insertions(+), 84 deletions(-) rename extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/{RedisCachesConfig.java => RedisCachesRuntimeConfig.java} (61%) diff --git a/extensions/redis-cache/deployment/pom.xml b/extensions/redis-cache/deployment/pom.xml index c5926b82942a2..4fa7d52ac6ef9 100644 --- a/extensions/redis-cache/deployment/pom.xml +++ b/extensions/redis-cache/deployment/pom.xml @@ -74,9 +74,6 @@ ${project.version} - - -AlegacyConfigRoot=true - diff --git a/extensions/redis-cache/deployment/src/main/java/io/quarkus/cache/redis/deployment/RedisCacheProcessor.java b/extensions/redis-cache/deployment/src/main/java/io/quarkus/cache/redis/deployment/RedisCacheProcessor.java index a37e01324d1b1..7da3836570848 100644 --- a/extensions/redis-cache/deployment/src/main/java/io/quarkus/cache/redis/deployment/RedisCacheProcessor.java +++ b/extensions/redis-cache/deployment/src/main/java/io/quarkus/cache/redis/deployment/RedisCacheProcessor.java @@ -59,7 +59,7 @@ UnremovableBeanBuildItem redisClientUnremoveable() { @BuildStep RequestedRedisClientBuildItem requestedRedisClientBuildItem(RedisCachesBuildTimeConfig buildConfig) { - return new RequestedRedisClientBuildItem(buildConfig.clientName.orElse(RedisConfig.DEFAULT_CLIENT_NAME)); + return new RequestedRedisClientBuildItem(buildConfig.clientName().orElse(RedisConfig.DEFAULT_CLIENT_NAME)); } @BuildStep @@ -75,14 +75,14 @@ void determineKeyValueTypes(RedisCacheBuildRecorder recorder, CombinedIndexBuild CacheNamesBuildItem cacheNamesBuildItem, RedisCachesBuildTimeConfig buildConfig) { Map keyTypes = new HashMap<>(); - RedisCacheBuildTimeConfig defaultBuildTimeConfig = buildConfig.defaultConfig; + RedisCacheBuildTimeConfig defaultBuildTimeConfig = buildConfig.defaultConfig(); for (String cacheName : cacheNamesBuildItem.getNames()) { - RedisCacheBuildTimeConfig namedBuildTimeConfig = buildConfig.cachesConfig.get(cacheName); + RedisCacheBuildTimeConfig namedBuildTimeConfig = buildConfig.cachesConfig().get(cacheName); - if (namedBuildTimeConfig != null && namedBuildTimeConfig.keyType.isPresent()) { - keyTypes.put(cacheName, TypeParser.parse(namedBuildTimeConfig.keyType.get())); - } else if (defaultBuildTimeConfig.keyType.isPresent()) { - keyTypes.put(cacheName, TypeParser.parse(defaultBuildTimeConfig.keyType.get())); + if (namedBuildTimeConfig != null && namedBuildTimeConfig.keyType().isPresent()) { + keyTypes.put(cacheName, TypeParser.parse(namedBuildTimeConfig.keyType().get())); + } else if (defaultBuildTimeConfig.keyType().isPresent()) { + keyTypes.put(cacheName, TypeParser.parse(defaultBuildTimeConfig.keyType().get())); } } recorder.setCacheKeyTypes(keyTypes); @@ -90,18 +90,18 @@ void determineKeyValueTypes(RedisCacheBuildRecorder recorder, CombinedIndexBuild Map resolvedValuesTypesFromAnnotations = valueTypesFromCacheResultAnnotation(combinedIndex); Map valueTypes = new HashMap<>(); - Optional defaultValueType = buildConfig.defaultConfig.valueType; + Optional defaultValueType = buildConfig.defaultConfig().valueType(); Set cacheNames = cacheNamesBuildItem.getNames(); for (String cacheName : cacheNames) { String valueType = null; - RedisCacheBuildTimeConfig cacheSpecificGroup = buildConfig.cachesConfig.get(cacheName); + RedisCacheBuildTimeConfig cacheSpecificGroup = buildConfig.cachesConfig().get(cacheName); if (cacheSpecificGroup == null) { if (defaultValueType.isPresent()) { valueType = defaultValueType.get(); } } else { - if (cacheSpecificGroup.valueType.isPresent()) { - valueType = cacheSpecificGroup.valueType.get(); + if (cacheSpecificGroup.valueType().isPresent()) { + valueType = cacheSpecificGroup.valueType().get(); } } diff --git a/extensions/redis-cache/runtime/pom.xml b/extensions/redis-cache/runtime/pom.xml index dd7a7439eb461..a0595a1e12263 100644 --- a/extensions/redis-cache/runtime/pom.xml +++ b/extensions/redis-cache/runtime/pom.xml @@ -76,9 +76,6 @@ ${project.version} - - -AlegacyConfigRoot=true - diff --git a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheBuildRecorder.java b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheBuildRecorder.java index 1ec0696eb0e9f..2ef0b11fac06a 100644 --- a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheBuildRecorder.java +++ b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheBuildRecorder.java @@ -19,12 +19,13 @@ public class RedisCacheBuildRecorder { private static final Logger LOGGER = Logger.getLogger(RedisCacheBuildRecorder.class); private final RedisCachesBuildTimeConfig buildConfig; - private final RuntimeValue redisCacheConfigRV; + private final RuntimeValue redisCacheConfigRV; private static Map keyTypes; private static Map valueTypes; - public RedisCacheBuildRecorder(RedisCachesBuildTimeConfig buildConfig, RuntimeValue redisCacheConfigRV) { + public RedisCacheBuildRecorder(RedisCachesBuildTimeConfig buildConfig, + RuntimeValue redisCacheConfigRV) { this.buildConfig = buildConfig; this.redisCacheConfigRV = redisCacheConfigRV; } @@ -56,7 +57,7 @@ public CacheManager get() { cacheInfo.valueType); } - RedisCacheImpl cache = new RedisCacheImpl(cacheInfo, buildConfig.clientName); + RedisCacheImpl cache = new RedisCacheImpl(cacheInfo, buildConfig.clientName()); caches.put(cacheInfo.name, cache); } return new CacheManagerImpl(caches); diff --git a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheBuildTimeConfig.java b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheBuildTimeConfig.java index 9db9560c408a7..369af985e11fe 100644 --- a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheBuildTimeConfig.java +++ b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheBuildTimeConfig.java @@ -3,20 +3,17 @@ import java.util.Optional; import io.quarkus.runtime.annotations.ConfigGroup; -import io.quarkus.runtime.annotations.ConfigItem; @ConfigGroup -public class RedisCacheBuildTimeConfig { +public interface RedisCacheBuildTimeConfig { /** * The default type of the value stored in the cache. */ - @ConfigItem - public Optional valueType; + Optional valueType(); /** * The key type, {@code String} by default. */ - @ConfigItem - public Optional keyType; + Optional keyType(); } diff --git a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheInfoBuilder.java b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheInfoBuilder.java index 6ec8ddbe13dc0..6867743eecd94 100644 --- a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheInfoBuilder.java +++ b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheInfoBuilder.java @@ -9,7 +9,7 @@ public class RedisCacheInfoBuilder { - public static Set build(Set cacheNames, RedisCachesConfig runtimeConfig, + public static Set build(Set cacheNames, RedisCachesRuntimeConfig runtimeConfig, Map keyTypes, Map valueTypes) { if (cacheNames.isEmpty()) { return Collections.emptySet(); @@ -21,32 +21,32 @@ public static Set build(Set cacheNames, RedisCachesConfi RedisCacheInfo cacheInfo = new RedisCacheInfo(); cacheInfo.name = cacheName; - RedisCacheRuntimeConfig defaultRuntimeConfig = runtimeConfig.defaultConfig; - RedisCacheRuntimeConfig namedRuntimeConfig = runtimeConfig.cachesConfig.get(cacheInfo.name); + RedisCacheRuntimeConfig defaultRuntimeConfig = runtimeConfig.defaultConfig(); + RedisCacheRuntimeConfig namedRuntimeConfig = runtimeConfig.cachesConfig().get(cacheInfo.name); - if (namedRuntimeConfig != null && namedRuntimeConfig.expireAfterAccess.isPresent()) { - cacheInfo.expireAfterAccess = namedRuntimeConfig.expireAfterAccess; - } else if (defaultRuntimeConfig.expireAfterAccess.isPresent()) { - cacheInfo.expireAfterAccess = defaultRuntimeConfig.expireAfterAccess; + if (namedRuntimeConfig != null && namedRuntimeConfig.expireAfterAccess().isPresent()) { + cacheInfo.expireAfterAccess = namedRuntimeConfig.expireAfterAccess(); + } else if (defaultRuntimeConfig.expireAfterAccess().isPresent()) { + cacheInfo.expireAfterAccess = defaultRuntimeConfig.expireAfterAccess(); } - if (namedRuntimeConfig != null && namedRuntimeConfig.expireAfterWrite.isPresent()) { - cacheInfo.expireAfterWrite = namedRuntimeConfig.expireAfterWrite; - } else if (defaultRuntimeConfig.expireAfterWrite.isPresent()) { - cacheInfo.expireAfterWrite = defaultRuntimeConfig.expireAfterWrite; + if (namedRuntimeConfig != null && namedRuntimeConfig.expireAfterWrite().isPresent()) { + cacheInfo.expireAfterWrite = namedRuntimeConfig.expireAfterWrite(); + } else if (defaultRuntimeConfig.expireAfterWrite().isPresent()) { + cacheInfo.expireAfterWrite = defaultRuntimeConfig.expireAfterWrite(); } // Handle the deprecated TTL - if (namedRuntimeConfig != null && namedRuntimeConfig.ttl.isPresent()) { - cacheInfo.expireAfterWrite = namedRuntimeConfig.ttl; - } else if (defaultRuntimeConfig.ttl.isPresent()) { - cacheInfo.expireAfterWrite = defaultRuntimeConfig.ttl; + if (namedRuntimeConfig != null && namedRuntimeConfig.ttl().isPresent()) { + cacheInfo.expireAfterWrite = namedRuntimeConfig.ttl(); + } else if (defaultRuntimeConfig.ttl().isPresent()) { + cacheInfo.expireAfterWrite = defaultRuntimeConfig.ttl(); } - if (namedRuntimeConfig != null && namedRuntimeConfig.prefix.isPresent()) { - cacheInfo.prefix = namedRuntimeConfig.prefix.get(); - } else if (defaultRuntimeConfig.prefix.isPresent()) { - cacheInfo.prefix = defaultRuntimeConfig.prefix.get(); + if (namedRuntimeConfig != null && namedRuntimeConfig.prefix().isPresent()) { + cacheInfo.prefix = namedRuntimeConfig.prefix().get(); + } else if (defaultRuntimeConfig.prefix().isPresent()) { + cacheInfo.prefix = defaultRuntimeConfig.prefix().get(); } cacheInfo.valueType = valueTypes.get(cacheName); @@ -55,16 +55,16 @@ public static Set build(Set cacheNames, RedisCachesConfi cacheInfo.keyType = keyTypes.get(cacheName); } - if (namedRuntimeConfig != null && namedRuntimeConfig.useOptimisticLocking.isPresent()) { - cacheInfo.useOptimisticLocking = namedRuntimeConfig.useOptimisticLocking.get(); - } else if (defaultRuntimeConfig.useOptimisticLocking.isPresent()) { - cacheInfo.useOptimisticLocking = defaultRuntimeConfig.useOptimisticLocking.get(); + if (namedRuntimeConfig != null && namedRuntimeConfig.useOptimisticLocking().isPresent()) { + cacheInfo.useOptimisticLocking = namedRuntimeConfig.useOptimisticLocking().get(); + } else if (defaultRuntimeConfig.useOptimisticLocking().isPresent()) { + cacheInfo.useOptimisticLocking = defaultRuntimeConfig.useOptimisticLocking().get(); } - if (namedRuntimeConfig != null && namedRuntimeConfig.invalidationScanSize.isPresent()) { - cacheInfo.invalidationScanSize = namedRuntimeConfig.invalidationScanSize; - } else if (defaultRuntimeConfig.invalidationScanSize.isPresent()) { - cacheInfo.invalidationScanSize = defaultRuntimeConfig.invalidationScanSize; + if (namedRuntimeConfig != null && namedRuntimeConfig.invalidationScanSize().isPresent()) { + cacheInfo.invalidationScanSize = namedRuntimeConfig.invalidationScanSize(); + } else if (defaultRuntimeConfig.invalidationScanSize().isPresent()) { + cacheInfo.invalidationScanSize = defaultRuntimeConfig.invalidationScanSize(); } result.add(cacheInfo); diff --git a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheRuntimeConfig.java b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheRuntimeConfig.java index 2a2afe6860483..82fea5f1d7918 100644 --- a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheRuntimeConfig.java +++ b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheRuntimeConfig.java @@ -5,40 +5,35 @@ import java.util.OptionalInt; import io.quarkus.runtime.annotations.ConfigGroup; -import io.quarkus.runtime.annotations.ConfigItem; @ConfigGroup -public class RedisCacheRuntimeConfig { +public interface RedisCacheRuntimeConfig { /** * The default time to live of the item stored in the cache. * * @deprecated Use {@link #expireAfterWrite} instead. */ - @ConfigItem - @Deprecated - public Optional ttl; + @Deprecated(forRemoval = true, since = "3.3.0") + Optional ttl(); /** * Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after * the entry's creation, or the most recent replacement of its value. */ - @ConfigItem - Optional expireAfterWrite; + Optional expireAfterWrite(); /** * Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after * the last access of its value. */ - @ConfigItem - Optional expireAfterAccess; + Optional expireAfterAccess(); /** * The key prefix allowing to identify the keys belonging to the cache. * If not set, the value "{@code cache:{cache-name}}" will be used. The variable * "{@code {cache-name}}" is resolved from the value set in the cache annotations. */ - @ConfigItem - public Optional prefix; + Optional prefix(); /** * Whether the access to the cache should be using optimistic locking. @@ -46,14 +41,12 @@ public class RedisCacheRuntimeConfig { * Locking for details. * Default is {@code false}. */ - @ConfigItem - public Optional useOptimisticLocking; + Optional useOptimisticLocking(); /** * If set, the {@code SCAN} command (used to implement invalidation) will have * the {@code COUNT} argument with given value. If not set (default), * no {@code COUNT} argument is present. */ - @ConfigItem - public OptionalInt invalidationScanSize; + OptionalInt invalidationScanSize(); } diff --git a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCachesBuildTimeConfig.java b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCachesBuildTimeConfig.java index ce9e2b3da345e..acbb824f990de 100644 --- a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCachesBuildTimeConfig.java +++ b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCachesBuildTimeConfig.java @@ -7,31 +7,32 @@ import io.quarkus.runtime.annotations.ConfigDocMapKey; import io.quarkus.runtime.annotations.ConfigDocSection; -import io.quarkus.runtime.annotations.ConfigItem; import io.quarkus.runtime.annotations.ConfigRoot; +import io.smallrye.config.ConfigMapping; +import io.smallrye.config.WithParentName; -@ConfigRoot(phase = BUILD_AND_RUN_TIME_FIXED, name = "cache.redis") -public class RedisCachesBuildTimeConfig { +@ConfigRoot(phase = BUILD_AND_RUN_TIME_FIXED) +@ConfigMapping(prefix = "quarkus.cache.redis") +public interface RedisCachesBuildTimeConfig { /** * The name of the named Redis client to be used for communicating with Redis. * If not set, use the default Redis client. */ - @ConfigItem - public Optional clientName; + Optional clientName(); /** * Default configuration applied to all Redis caches (lowest precedence) */ - @ConfigItem(name = ConfigItem.PARENT) - public RedisCacheBuildTimeConfig defaultConfig; + @WithParentName + RedisCacheBuildTimeConfig defaultConfig(); /** * Additional configuration applied to a specific Redis cache (highest precedence) */ - @ConfigItem(name = ConfigItem.PARENT) + @WithParentName @ConfigDocMapKey("cache-name") @ConfigDocSection - public Map cachesConfig; + Map cachesConfig(); } diff --git a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCachesConfig.java b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCachesRuntimeConfig.java similarity index 61% rename from extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCachesConfig.java rename to extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCachesRuntimeConfig.java index 3e0a13fc66308..3039d655244d2 100644 --- a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCachesConfig.java +++ b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCachesRuntimeConfig.java @@ -6,24 +6,26 @@ import io.quarkus.runtime.annotations.ConfigDocMapKey; import io.quarkus.runtime.annotations.ConfigDocSection; -import io.quarkus.runtime.annotations.ConfigItem; import io.quarkus.runtime.annotations.ConfigRoot; +import io.smallrye.config.ConfigMapping; +import io.smallrye.config.WithParentName; -@ConfigRoot(phase = RUN_TIME, name = "cache.redis") -public class RedisCachesConfig { +@ConfigRoot(phase = RUN_TIME) +@ConfigMapping(prefix = "quarkus.cache.redis") +public interface RedisCachesRuntimeConfig { /** * Default configuration applied to all Redis caches (lowest precedence) */ - @ConfigItem(name = ConfigItem.PARENT) - public RedisCacheRuntimeConfig defaultConfig; + @WithParentName + RedisCacheRuntimeConfig defaultConfig(); /** * Additional configuration applied to a specific Redis cache (highest precedence) */ - @ConfigItem(name = ConfigItem.PARENT) + @WithParentName @ConfigDocMapKey("cache-name") @ConfigDocSection - Map cachesConfig; + Map cachesConfig(); }