Skip to content

Commit

Permalink
Merge pull request quarkusio#45942 from gsmet/config-mapping-redis-cache
Browse files Browse the repository at this point in the history
Convert Redis Cache to @ConfigMapping
  • Loading branch information
gsmet authored Jan 29, 2025
2 parents 1471e59 + 1606f47 commit 9aa4b7e
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 84 deletions.
3 changes: 0 additions & 3 deletions extensions/redis-cache/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -75,33 +75,33 @@ void determineKeyValueTypes(RedisCacheBuildRecorder recorder, CombinedIndexBuild
CacheNamesBuildItem cacheNamesBuildItem, RedisCachesBuildTimeConfig buildConfig) {

Map<String, java.lang.reflect.Type> 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);

Map<String, Type> resolvedValuesTypesFromAnnotations = valueTypesFromCacheResultAnnotation(combinedIndex);

Map<String, java.lang.reflect.Type> valueTypes = new HashMap<>();
Optional<String> defaultValueType = buildConfig.defaultConfig.valueType;
Optional<String> defaultValueType = buildConfig.defaultConfig().valueType();
Set<String> 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();
}
}

Expand Down
3 changes: 0 additions & 3 deletions extensions/redis-cache/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ public class RedisCacheBuildRecorder {
private static final Logger LOGGER = Logger.getLogger(RedisCacheBuildRecorder.class);

private final RedisCachesBuildTimeConfig buildConfig;
private final RuntimeValue<RedisCachesConfig> redisCacheConfigRV;
private final RuntimeValue<RedisCachesRuntimeConfig> redisCacheConfigRV;

private static Map<String, Type> keyTypes;
private static Map<String, Type> valueTypes;

public RedisCacheBuildRecorder(RedisCachesBuildTimeConfig buildConfig, RuntimeValue<RedisCachesConfig> redisCacheConfigRV) {
public RedisCacheBuildRecorder(RedisCachesBuildTimeConfig buildConfig,
RuntimeValue<RedisCachesRuntimeConfig> redisCacheConfigRV) {
this.buildConfig = buildConfig;
this.redisCacheConfigRV = redisCacheConfigRV;
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> valueType;
Optional<String> valueType();

/**
* The key type, {@code String} by default.
*/
@ConfigItem
public Optional<String> keyType;
Optional<String> keyType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class RedisCacheInfoBuilder {

public static Set<RedisCacheInfo> build(Set<String> cacheNames, RedisCachesConfig runtimeConfig,
public static Set<RedisCacheInfo> build(Set<String> cacheNames, RedisCachesRuntimeConfig runtimeConfig,
Map<String, Type> keyTypes, Map<String, Type> valueTypes) {
if (cacheNames.isEmpty()) {
return Collections.emptySet();
Expand All @@ -21,32 +21,32 @@ public static Set<RedisCacheInfo> build(Set<String> 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);
Expand All @@ -55,16 +55,16 @@ public static Set<RedisCacheInfo> build(Set<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,48 @@
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<Duration> ttl;
@Deprecated(forRemoval = true, since = "3.3.0")
Optional<Duration> 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<Duration> expireAfterWrite;
Optional<Duration> 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<Duration> expireAfterAccess;
Optional<Duration> 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<String> prefix;
Optional<String> prefix();

/**
* Whether the access to the cache should be using optimistic locking.
* See <a href="https://redis.io/docs/manual/transactions/#optimistic-locking-using-check-and-set">Redis Optimistic
* Locking</a> for details.
* Default is {@code false}.
*/
@ConfigItem
public Optional<Boolean> useOptimisticLocking;
Optional<Boolean> 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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> clientName;
Optional<String> 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<String, RedisCacheBuildTimeConfig> cachesConfig;
Map<String, RedisCacheBuildTimeConfig> cachesConfig();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, RedisCacheRuntimeConfig> cachesConfig;
Map<String, RedisCacheRuntimeConfig> cachesConfig();

}

0 comments on commit 9aa4b7e

Please sign in to comment.