44import cz .foresttech .forestredis .shared .models .RedisConfiguration ;
55import redis .clients .jedis .*;
66
7- import java .util .ArrayList ;
8- import java .util .Arrays ;
9- import java .util .List ;
7+ import java .util .*;
108
119/**
1210 * Class for maintaining and handling connection to Redis server.
1715public class RedisManager {
1816
1917 /**
20- * Singleton instance
18+ * Main instance
2119 */
2220 private static RedisManager api ;
2321
@@ -29,17 +27,17 @@ public class RedisManager {
2927 /**
3028 * Configuration object to store credentials
3129 */
32- private final RedisConfiguration redisConfiguration ;
30+ private RedisConfiguration redisConfiguration ;
3331
3432 /**
3533 * Current server's identifier. Shall be unique across your network.
3634 */
37- private final String serverIdentifier ;
35+ private String serverIdentifier ;
3836
3937 /**
40- * List of subscribed channels
38+ * Set of subscribed channels
4139 */
42- private final List <String > channels ;
40+ private final HashSet <String > channels ;
4341
4442 /**
4543 * List of current subscriptions
@@ -66,7 +64,7 @@ public class RedisManager {
6664 * @param serverIdentifier Identifier of the server (e.g. 'Bungee01'). Shall be unique to prevent bugs
6765 * @param redisConfiguration {@link RedisConfiguration} object with Redis server credentials
6866 */
69- private RedisManager (IForestRedisPlugin plugin , String serverIdentifier , RedisConfiguration redisConfiguration ) {
67+ public RedisManager (IForestRedisPlugin plugin , String serverIdentifier , RedisConfiguration redisConfiguration ) {
7068 this .plugin = plugin ;
7169 this .closing = false ;
7270
@@ -75,7 +73,37 @@ private RedisManager(IForestRedisPlugin plugin, String serverIdentifier, RedisCo
7573
7674 this .subscriptions = new ArrayList <>();
7775
78- this .channels = new ArrayList <>();
76+ this .channels = new HashSet <>();
77+ }
78+
79+ /*----------------------------------------------------------------------------------------------------------*/
80+
81+ /**
82+ * Reloads the manager while keeping already subscribed channels if set.
83+ *
84+ * @param serverIdentifier New server identifier (if null, already using server id will be used)
85+ * @param redisConfiguration New RedisConfiguration (if null, already using configuration will be used)
86+ * @param keepChannels Keep already subscribed channels
87+ */
88+ public void reload (String serverIdentifier , RedisConfiguration redisConfiguration , boolean keepChannels ) {
89+ this .close ();
90+ this .closing = false ;
91+
92+ if (serverIdentifier != null ) {
93+ this .serverIdentifier = serverIdentifier ;
94+ }
95+ if (redisConfiguration != null ) {
96+ this .redisConfiguration = redisConfiguration ;
97+ }
98+
99+ if (keepChannels ) {
100+ String [] channels = this .channels .toArray (String []::new );
101+ this .channels .clear ();
102+ this .setup (channels );
103+ return ;
104+ }
105+
106+ this .setup ();
79107 }
80108
81109 /*----------------------------------------------------------------------------------------------------------*/
@@ -108,7 +136,7 @@ public boolean setup(String... channels) {
108136
109137 // If channels were provided, add them to the list and subscribe to them
110138 if (channels != null && channels .length > 0 ) {
111- this .channels .addAll (List .of (channels ));
139+ this .channels .addAll (Set .of (channels ));
112140
113141 Subscription subscription = new Subscription (this .channels .toArray (new String [0 ]));
114142 this .plugin .runAsync (subscription );
@@ -137,14 +165,14 @@ public void unsubscribe(String... channels) {
137165 try {
138166 for (Subscription sub : this .subscriptions ) {
139167 sub .unsubscribe (channels );
140- this .plugin .logger ().info ("Successfully unsubscribed channels: " + Arrays .toString (channels ) + "!" );
141168 }
169+ this .plugin .logger ().info ("Successfully unsubscribed channels: " + Arrays .toString (channels ) + "!" );
142170 } catch (Exception ex ) {
143171 this .plugin .logger ().warning ("An error occurred while unsubscribing channels: " + Arrays .toString (channels ) + "!" );
144172 return ;
145173 }
146174
147- this .channels .removeAll (List .of (channels ));
175+ this .channels .removeAll (Set .of (channels ));
148176 }
149177
150178 /*----------------------------------------------------------------------------------------------------------*/
@@ -165,7 +193,7 @@ public boolean subscribe(String... channels) {
165193 return false ;
166194 }
167195
168- List <String > actualChannelsToAdd = new ArrayList <>();
196+ Set <String > actualChannelsToAdd = new HashSet <>();
169197
170198 for (String channel : channels ) {
171199 if (this .channels .contains (channel ) && channel != null ) {
@@ -271,6 +299,8 @@ public void close() {
271299 }
272300 }
273301
302+ this .subscriptions .clear ();
303+
274304 if (this .jedisPool == null ) {
275305 return ;
276306 }
@@ -308,7 +338,7 @@ public boolean isSubscribed(String channel) {
308338 *
309339 * @return List of all subscribed channels (case-sensitive)
310340 */
311- public List <String > getSubscribedChannels () {
341+ public Set <String > getSubscribedChannels () {
312342 return channels ;
313343 }
314344
@@ -386,7 +416,7 @@ public void onMessage(String channel, String message) {
386416 /*----------------------------------------------------------------------------------------------------------*/
387417
388418 /**
389- * Initialization method for creating {@link RedisManager} singleton instance. This won't start any
419+ * Initialization method for creating {@link RedisManager} main instance. This won't start any
390420 * connection or subscription.
391421 *
392422 * @param plugin Origin plugin which tries to obtain the instance
@@ -400,10 +430,10 @@ public static void init(IForestRedisPlugin plugin, String serverIdentifier, Redi
400430 /*----------------------------------------------------------------------------------------------------------*/
401431
402432 /**
403- * Gets the singleton instance of {@link RedisManager} object. This is the only
433+ * Gets the main instance of {@link RedisManager} object. This is the only
404434 * recommended approach to access the API methods.
405435 *
406- * @return Singleton instance of {@link RedisManager}
436+ * @return Main instance of {@link RedisManager}
407437 */
408438 public static RedisManager getAPI () {
409439 return api ;
0 commit comments