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

feat: lettuce factory support pub/sub connection #918

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -5,6 +5,7 @@
import io.lettuce.core.cluster.api.async.RedisClusterAsyncCommands;
import io.lettuce.core.cluster.api.reactive.RedisClusterReactiveCommands;
import io.lettuce.core.cluster.api.sync.RedisClusterCommands;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

Expand Down Expand Up @@ -35,7 +36,9 @@ public LettuceFactory(String key, Class<?> clazz) {
key += ".client";
} else if (StatefulConnection.class.isAssignableFrom(clazz)) {
key += ".connection";
} else if (RedisClusterCommands.class.isAssignableFrom(clazz)) {
} else if (StatefulRedisPubSubConnection.class.isAssignableFrom(clazz)){
key += ".pubSubConnection";
}else if (RedisClusterCommands.class.isAssignableFrom(clazz)) {
// RedisCommands extends RedisClusterCommands
key += ".commands";
} else if (RedisClusterAsyncCommands.class.isAssignableFrom(clazz)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ protected CacheBuilder initCache(ConfigTree ct, String cacheAreaWithPrefix) {
autoConfigureBeans.getCustomContainer().put(cacheAreaWithPrefix + ".client", client);
LettuceConnectionManager m = LettuceConnectionManager.defaultManager();
m.init(client, connection);
if (enablePubSub) {
m.init(client, pubSubConnection);
autoConfigureBeans.getCustomContainer().put(cacheAreaWithPrefix + ".pubSubConnection", m.pubSubConnection(client));
}
autoConfigureBeans.getCustomContainer().put(cacheAreaWithPrefix + ".connection", m.connection(client));
autoConfigureBeans.getCustomContainer().put(cacheAreaWithPrefix + ".commands", m.commands(client));
autoConfigureBeans.getCustomContainer().put(cacheAreaWithPrefix + ".asyncCommands", m.asyncCommands(client));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public LettuceBroadcastManager(CacheManager cacheManager, RedisLettuceCacheConfi
this.channel = config.getBroadcastChannel().getBytes(StandardCharsets.UTF_8);
this.lettuceConnectionManager = config.getConnectionManager();
this.lettuceConnectionManager.init(config.getRedisClient(), config.getConnection());
this.lettuceConnectionManager.init(config.getRedisClient(), config.getPubSubConnection());
this.stringAsyncCommands = (BaseRedisAsyncCommands<byte[], byte[]>) lettuceConnectionManager
.asyncCommands(config.getRedisClient());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;

import java.util.Collections;
import java.util.Map;
Expand All @@ -22,6 +23,7 @@ public class LettuceConnectionManager {

private static class LettuceObjects {
private StatefulConnection connection;
private StatefulRedisPubSubConnection pubSubConnection;
private Object commands;
private Object asyncCommands;
private Object reactiveCommands;
Expand All @@ -47,11 +49,13 @@ private LettuceObjects getLettuceObjectsFromMap(AbstractRedisClient redisClient)
}

public void init(AbstractRedisClient redisClient, StatefulConnection connection) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init方法有一个就行了,用两个参数就可以。如果不想破坏兼容性,可以加一个双参数的,然后把原来的标记为废弃。这里把connection的赋值放在外面,有理论上的线程安全问题吧?

map.computeIfAbsent(redisClient, key -> {
LettuceObjects lo = new LettuceObjects();
lo.connection = connection;
return lo;
});
LettuceObjects lettuceObjects = map.computeIfAbsent(redisClient, key -> new LettuceObjects());
lettuceObjects.connection = connection;
}

public void init(AbstractRedisClient redisClient, StatefulRedisPubSubConnection pubSubConnection) {
LettuceObjects lettuceObjects = map.computeIfAbsent(redisClient, key -> new LettuceObjects());
lettuceObjects.pubSubConnection = pubSubConnection;
}

public StatefulConnection connection(AbstractRedisClient redisClient) {
Expand All @@ -68,6 +72,21 @@ public StatefulConnection connection(AbstractRedisClient redisClient) {
return lo.connection;
}

public StatefulRedisPubSubConnection pubSubConnection(AbstractRedisClient redisClient) {
LettuceObjects lo = getLettuceObjectsFromMap(redisClient);
if (lo.pubSubConnection == null) {
if (redisClient instanceof RedisClient) {
lo.pubSubConnection = ((RedisClient) redisClient).connectPubSub(new JetCacheCodec());
} else if (redisClient instanceof RedisClusterClient) {
lo.pubSubConnection = ((RedisClusterClient) redisClient).connectPubSub(new JetCacheCodec());
} else {
throw new CacheConfigException("type " + redisClient.getClass() + " is not supported");
}
}
return lo.pubSubConnection;
}


public Object commands(AbstractRedisClient redisClient) {
connection(redisClient);
LettuceObjects lo = getLettuceObjectsFromMap(redisClient);
Expand Down Expand Up @@ -133,6 +152,9 @@ public void removeAndClose(AbstractRedisClient redisClient) {
if (lo.connection != null) {
lo.connection.close();
}
if (lo.pubSubConnection != null){
lo.pubSubConnection.close();
}
redisClient.shutdown();
}
}
Loading