Skip to content

Commit

Permalink
Merge pull request #99 from Foxikle/fix/general-api
Browse files Browse the repository at this point in the history
Redis interface for Plugins
  • Loading branch information
webhead1104 authored Jun 16, 2024
2 parents e69d474 + 9a7215e commit 47cc9dc
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion src/main/java/net/cytonic/cytosis/data/RedisDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RedisDatabase extends JedisPubSub {
private final String PLAYER_STATUS_CHANNEL = "player_status";
private final String SERVER_SHUTDOWN_KEY = "server_shutdown";
private final Jedis jedis;
private final ExecutorService worker = Executors.newSingleThreadExecutor(Thread.ofVirtual().name("CytosisRedisWorker").factory());
private final ExecutorService worker = Executors.newCachedThreadPool(Thread.ofVirtual().name("CytosisRedisWorker").factory());
@Getter
private final Set<String> onlinePlayers;
@Getter
Expand Down Expand Up @@ -75,5 +75,67 @@ public void onMessage(String channel, String message) {
*/
public void disconnect() {
jedis.disconnect();
worker.shutdown();
jedis.close();
}

/**
* Gets a set from the redis server
*
* @param key key
* @return the set
*/
public Set<String> getSet(String key) {
return jedis.smembers(key);
}

/**
* Set a key equal to a value
*
* @param key key
* @param value value
*/
public void setValue(String key, String value) {
jedis.set(key, value);
}

/**
* Adds a value to a set
*
* @param key key
* @param value value(s)
*/
public void addValue(String key, String... value) {
jedis.sadd(key, value);
}

/**
* Remove a value from a set
*
* @param key key
* @param value value(s)
*/
public void removeValue(String key, String... value) {
jedis.srem(key, value);
}

/**
* Registers a pubsub listener
*
* @param jedisPubSub the class to listen to
* @param channel the channel to listen on
*/
public void registerPubSub(JedisPubSub jedisPubSub, String channel) {
worker.submit(() -> jedis.subscribe(jedisPubSub, channel));
}

/**
* Publishes a message to the specified channel
*
* @param channel the channel
* @param message the message
*/
public void publish(String channel, String message) {
jedis.publish(channel, message);
}
}

0 comments on commit 47cc9dc

Please sign in to comment.