Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using provided codec in DefaultRedisClusterClientFactory #571

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import io.lettuce.core.resource.ClientResources;
import io.micronaut.context.annotation.Bean;
Expand All @@ -37,12 +38,19 @@
* Allows connecting to a Redis cluster via the {@code "redis.uris"} setting.
*
* @author Graeme Rocher
* @param <K> Key type
* @param <V> Value type
* @since 1.0
*/
@Requires(property = RedisSetting.REDIS_URIS)
@Singleton
@Factory
public class DefaultRedisClusterClientFactory {
public class DefaultRedisClusterClientFactory<K, V> {
private final RedisCodec<K, V> defaultCodec;

public DefaultRedisClusterClientFactory(@Primary RedisCodec<K, V> codec) {
this.defaultCodec = codec;
}

/**
* Create the client based on config URIs.
Expand Down Expand Up @@ -93,8 +101,11 @@ public RedisClusterClient redisClient(@Primary AbstractRedisConfiguration config
@Bean(preDestroy = "close")
@Singleton
@Primary
public StatefulRedisClusterConnection<String, String> redisConnection(@Primary RedisClusterClient redisClient, @Primary AbstractRedisConfiguration config) {
StatefulRedisClusterConnection<String, String> connection = redisClient.connect();
public StatefulRedisClusterConnection<K, V> redisConnection(
@Primary RedisClusterClient redisClient,
@Primary AbstractRedisConfiguration config
) {
StatefulRedisClusterConnection<K, V> connection = redisClient.connect(defaultCodec);
if (config.getReadFrom().isPresent()) {
connection.setReadFrom(config.getReadFrom().get());
}
Expand All @@ -108,8 +119,8 @@ public StatefulRedisClusterConnection<String, String> redisConnection(@Primary R
* @deprecated use {@link #redisConnection(RedisClusterClient, AbstractRedisConfiguration)} instead
*/
@Deprecated(since = "6.5.0", forRemoval = true)
public StatefulRedisClusterConnection<String, String> redisConnection(@Primary RedisClusterClient redisClient) {
return redisClient.connect();
public StatefulRedisClusterConnection<K, V> redisConnection(@Primary RedisClusterClient redisClient) {
return redisClient.connect(defaultCodec);
}

/**
Expand All @@ -119,7 +130,7 @@ public StatefulRedisClusterConnection<String, String> redisConnection(@Primary R
*/
@Bean(preDestroy = "close")
@Singleton
public StatefulRedisPubSubConnection<String, String> redisPubSubConnection(@Primary RedisClusterClient redisClient) {
return redisClient.connectPubSub();
public StatefulRedisPubSubConnection<K, V> redisPubSubConnection(@Primary RedisClusterClient redisClient) {
return redisClient.connectPubSub(defaultCodec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package io.micronaut.configuration.lettuce
import io.lettuce.core.ReadFrom
import io.lettuce.core.cluster.RedisClusterClient
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection
import io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection
import io.micrometer.core.instrument.MeterRegistry
import io.micronaut.context.ApplicationContext
import io.micronaut.context.exceptions.ConfigurationException
import io.micronaut.context.exceptions.NoSuchBeanException
import io.micronaut.core.type.Argument

class DefaultRedisClusterClientFactorySpec extends RedisClusterSpec {

Expand Down Expand Up @@ -122,4 +125,41 @@ class DefaultRedisClusterClientFactorySpec extends RedisClusterSpec {
then:
thrown(ConfigurationException)
}

void "test redis client uses defined codec"() {
when:
ApplicationContext applicationContext = ApplicationContext.run(
'redis.uris': redisClusterUris,
'spec.name': ByteArrayCodecReplacementFactory.SPEC_NAME,
)
StatefulRedisClusterConnection connection = applicationContext.getBean(StatefulRedisClusterConnection)

then:
// tag::commands[]
final key = "foo".bytes
final value = "bar".bytes
RedisAdvancedClusterCommands<byte[], byte[]> commands = connection.sync()
commands.set(key, value)
commands.get(key) == value
// end::commands[]

cleanup:
applicationContext.stop()
}

void "throw error for no codec provided"() {
when:
ApplicationContext applicationContext = ApplicationContext.run(
'redis.uris': redisClusterUris,
)
applicationContext.getBean(Argument.of(StatefulRedisClusterConnection, byte.class, byte.class))

then:
// tag::commands[]
thrown(NoSuchBeanException)
// end::commands[]

cleanup:
applicationContext.stop()
}
}
Loading