1414import org .redisson .config .Config ;
1515
1616import java .time .Duration ;
17- import java .util .concurrent .CopyOnWriteArrayList ;
17+ import java .util .concurrent .ConcurrentHashMap ;
1818import java .util .logging .Level ;
1919import java .util .logging .Logger ;
2020
@@ -24,7 +24,7 @@ public class RedisManager {
2424 private static final Gson GSON = new GsonBuilder ().setPrettyPrinting ().enableComplexMapKeySerialization ()
2525 .registerTypeHierarchyAdapter (RedisTimedRequest .class , new TimedRequestSerializer ()).create ();
2626
27- private final CopyOnWriteArrayList < RedisClient > clients = new CopyOnWriteArrayList <>();
27+ private final ConcurrentHashMap < String , RedisClient > clients = new ConcurrentHashMap <>();
2828 private final String serverId ;
2929
3030 private RedissonClient redissonClient ;
@@ -88,14 +88,16 @@ public RedisManager(@NotNull Config redisConfig, @NotNull String serverId) {
8888 * @return the newly registered RedisClient instance, or the existing RedisClient
8989 * instance if a client with the same ID is already registered
9090 */
91- public RedisClient registerClient (@ NotNull String id ) {
92- if (clients .stream ().anyMatch (client -> client .getClientId ().equalsIgnoreCase (id ))) {
93- Logger .getGlobal ().warning (String .format ("[REDIS]: Attempted to register RedisClient with duplicate ID '%s', contact developer." , id ));
94- return clients .stream ().filter (client -> client .getClientId ().equalsIgnoreCase (id )).findFirst ().orElse (null );
91+ public @ NotNull RedisClient registerClient (@ NotNull String id ) {
92+ String key = id .toLowerCase ();
93+
94+ if (clients .containsKey (key )) {
95+ Logger .getGlobal ().warning ("[REDIS]: Attempted to register RedisClient with duplicate ID '" + id + "'" );
96+ return clients .get (key );
9597 }
9698
9799 RedisClient client = new RedisClient (this , id );
98- clients .add ( client );
100+ clients .put ( key , client );
99101
100102 return client ;
101103 }
0 commit comments