88 lines
2.6 KiB
Java
88 lines
2.6 KiB
Java
package com.io.yutian.aulib.redis;
|
|
|
|
import com.io.yutian.aulib.util.FileUtil;
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.bukkit.plugin.Plugin;
|
|
import redis.clients.jedis.Jedis;
|
|
import redis.clients.jedis.JedisPool;
|
|
import redis.clients.jedis.JedisPoolConfig;
|
|
|
|
import java.io.File;
|
|
import java.time.Duration;
|
|
import java.util.Set;
|
|
|
|
public class RedisIO implements IJedisGetter {
|
|
|
|
private JedisPool jedisPool;
|
|
|
|
public void init(Plugin plugin) {
|
|
File file = FileUtil.getFile(plugin, "", "redis.yml");
|
|
if (!file.exists()) {
|
|
plugin.saveResource("redis.yml", false);
|
|
}
|
|
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
|
|
String redisServer = configuration.getString("redis-server", "localhost");
|
|
int redisPort = configuration.getInt("redis-port", 6379);
|
|
String redisPassword = configuration.getString("redis-password");
|
|
if (redisPassword != null && (redisPassword.isEmpty() || redisPassword.equals("none"))) {
|
|
redisPassword = null;
|
|
}
|
|
try {
|
|
String finalRedisPassword = redisPassword;
|
|
JedisPoolConfig config = new JedisPoolConfig();
|
|
config.setMaxTotal(1024);
|
|
config.setMaxWait(Duration.ofMillis(10000));
|
|
jedisPool = new JedisPool(config, redisServer, redisPort, 0, finalRedisPassword);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void close() {
|
|
if (jedisPool != null && !jedisPool.isClosed()) {
|
|
jedisPool.close();
|
|
jedisPool.destroy();
|
|
}
|
|
}
|
|
|
|
public JedisPool getJedisPool() {
|
|
return jedisPool;
|
|
}
|
|
|
|
public Set<String> getKeys(String arg) {
|
|
try (Jedis resource = jedisPool.getResource()) {
|
|
return resource.keys(arg);
|
|
}
|
|
}
|
|
|
|
public void remove(String key) {
|
|
try (Jedis resource = jedisPool.getResource()) {
|
|
resource.del(key);
|
|
}
|
|
}
|
|
|
|
public void remove(String key, String field) {
|
|
try (Jedis resource = jedisPool.getResource()) {
|
|
resource.hdel(key, field);
|
|
}
|
|
}
|
|
|
|
public boolean has(String key) {
|
|
try (Jedis resource = jedisPool.getResource()) {
|
|
return resource.exists(key);
|
|
}
|
|
}
|
|
|
|
public boolean has(String key, String field) {
|
|
try (Jedis resource = jedisPool.getResource()) {
|
|
return resource.hexists(key, field);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Jedis getRedis() {
|
|
return jedisPool.getResource();
|
|
}
|
|
}
|