97 lines
2.4 KiB
Java
97 lines
2.4 KiB
Java
package me.unurled.sacredrealms.sr.data;
|
|
|
|
import static me.unurled.sacredrealms.sr.utils.Logger.error;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import me.unurled.sacredrealms.sr.SR;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import redis.clients.jedis.JedisPooled;
|
|
import redis.clients.jedis.params.ScanParams;
|
|
import redis.clients.jedis.resps.ScanResult;
|
|
|
|
public class Redis implements DataHandler {
|
|
|
|
JedisPooled client;
|
|
|
|
public Redis() {
|
|
DataManager dm = DataManager.getInstance(DataManager.class);
|
|
if (dm != null) {
|
|
String host = dm.getConfig().getString("redis.host", "127.0.0.1");
|
|
int port = dm.getConfig().getInt("redis.port", 6379);
|
|
try {
|
|
client = new JedisPooled(host, port);
|
|
client.get("test");
|
|
} catch (Exception e) {
|
|
error("Failed to connect to Redis, shutting down server.");
|
|
SR.getInstance().getServer().shutdown();
|
|
}
|
|
} else {
|
|
error("Failed to get DataManager instance. Can't connect to Redis, shutting down server.");
|
|
SR.getInstance().getServer().shutdown();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get a value from the data source
|
|
*
|
|
* @param key The key to get the value from
|
|
* @return The value of the key
|
|
*/
|
|
@Override
|
|
public String get(@NotNull String key) {
|
|
return client.get(key);
|
|
}
|
|
|
|
/**
|
|
* Set a value in the data source
|
|
*
|
|
* @param key The key to set the value to
|
|
* @param value The value to set
|
|
*/
|
|
@Override
|
|
public void set(@NotNull String key, String value) {
|
|
client.set(key, value);
|
|
}
|
|
|
|
/**
|
|
* Remove a value from the data source
|
|
*
|
|
* @param key The key to remove the value from
|
|
*/
|
|
@Override
|
|
public void remove(@NotNull String key) {
|
|
client.del(key);
|
|
}
|
|
|
|
/**
|
|
* Check if a key exists
|
|
* @param key The key to check
|
|
* @return If the key exists
|
|
*/
|
|
@Override
|
|
public boolean exists(@NotNull String key) {
|
|
return client.exists(key);
|
|
}
|
|
|
|
/**
|
|
* Get all keys from the data source
|
|
*
|
|
* @param key
|
|
* @return All keys
|
|
*/
|
|
@Override
|
|
public List<String> getKeysAll(@NotNull String key) {
|
|
String cursor = "0";
|
|
ScanParams params = new ScanParams().match(key);
|
|
List<String> keys = new ArrayList<>();
|
|
|
|
do {
|
|
ScanResult<String> result = client.scan(cursor, params);
|
|
cursor = result.getCursor();
|
|
keys.addAll(result.getResult());
|
|
} while (!cursor.equals("0"));
|
|
|
|
return keys;
|
|
}
|
|
}
|