Files
web_toolset/src/main/java/com/yutou/tools/utils/RedisTools.java
2023-12-27 10:43:20 +08:00

355 lines
10 KiB
Java

package com.yutou.tools.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import com.alibaba.fastjson2.JSONObject;
import org.apache.ibatis.mapping.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
public class RedisTools {
private static boolean isNotInstallRedis = false;
private static String host;
private static int port;
private static String auth;
public static int TOKEN_TIMEOUT_DEFAULT = 360;
private RedisTools() {
}
// 写成静态代码块形式,只加载一次,节省资源
public static void initConfig(int port, String auth) {
host = "127.0.0.1";
RedisTools.port = port;
RedisTools.auth = auth;
}
public static boolean set(int dbIndex, int timeout, String key, String value) {
try {
if (isNotInstallRedis) {
return false;
}
Jedis jedis = getRedis();
jedis.select(dbIndex);
if (timeout == -1) {
jedis.set(key, value);
} else {
jedis.setex(key, timeout, value);
}
jedis.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
return true;
}
public static boolean set(int dbIndex, String key, String value) {
return set(dbIndex, -1, key, value);
}
public static boolean set(String key, String value) {
return set(0, key, value);
}
public static boolean set(String key, String value, int timeout) {
try {
if (isNotInstallRedis) {
return false;
}
Jedis jedis = getRedis();
if (timeout == -1) {
jedis.set(key, value);
} else {
jedis.setex(key, timeout, value);
}
jedis.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
return true;
}
public static String get(String key, int dbIndex) {
String value = null;
if (isNotInstallRedis) {
return null;
}
try (Jedis jedis = getRedis()) {
jedis.select(dbIndex);
value = jedis.get(key);
jedis.close();
} catch (Exception e) {
// TODO: handle exception
// e.printStackTrace();
}
return value;
}
public static String get(String key) {
return get(key, 0);
}
public static boolean remove(String key) {
return remove(key, 0);
}
public static void removeLoginState(String uid) {
Jedis jedis = getRedis();
Set<String> keys = jedis.keys("*");
for (String string : keys) {
if (string.equals(uid)) {
jedis.del(string);
}
}
}
public static String ping() {
Jedis jedis = getRedis();
String tmp = jedis.ping();
jedis.close();
return tmp;
}
public static boolean exists(String key, String value) {
if (isNotInstallRedis) {
return false;
}
Jedis jedis = getRedis();
boolean flag = value.equals(jedis.get(key));
jedis.close();
return flag;
}
public static boolean exists(String key) {
if (isNotInstallRedis) {
return false;
}
Jedis jedis = getRedis();
boolean flag = jedis.exists(key);
jedis.close();
return flag;
}
public static Jedis getRedis() {
Jedis jedis = new Jedis(host, port);
jedis.auth(auth);
return jedis;
}
public static boolean remove(String key, int index) {
if (isNotInstallRedis) {
return false;
}
Jedis jedis = getRedis();
jedis.select(index);
Long i = jedis.del(key);
jedis.close();
if (i > 0) {
return true;
} else {
return false;
}
}
public static long list_add(String listName, String... value) {
Jedis jedis = getRedis();
long index = jedis.sadd(listName, value);
jedis.close();
return index;
}
public static Set<String> list_get(String listName) {
Jedis jedis = getRedis();
Set<String> set = jedis.smembers(listName);
jedis.close();
if (set == null) {
set = new HashSet<>();
}
return set;
}
public static boolean list_remove(String listName, String... value) {
Jedis jedis = getRedis();
long index = jedis.srem(listName, value);
jedis.close();
return index != 0;
}
public static boolean list_isExist(String listName, String value) {
Jedis jedis = getRedis();
boolean flag = jedis.sismember(listName, value);
jedis.close();
return flag;
}
private static class PropertyUtil {
// 加载property文件到io流里面
public static Properties loadProperties(String propertyFile) {
Properties properties = new Properties();
try {
InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
if (is == null) {
is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
}
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
}
private static Jedis getPoolRedis() {
if (isNotInstallRedis) {
return null;
}
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(0);
poolConfig.setMaxWaitMillis(1000);
JedisPool pool = new JedisPool(poolConfig, host);
return pool.getResource();
}
public static void pullMsg(String channel, String msg) {
System.out.println("1");
Jedis jedis = getPoolRedis();
System.out.println("2");
jedis.publish(channel, msg);
System.out.println("3");
jedis.close();
System.out.println("4");
}
private static boolean init = false;
public static void initRedisPoolSub() {
if (init) {
return;
}
init = true;
System.out.println("初始化订阅");
new Thread(new Runnable() {
@Override
public void run() {
Jedis jedis = getPoolRedis();
if (jedis != null) {
jedis.psubscribe(new Consumer(), "*");
}
}
}).start();
}
protected static class Consumer extends JedisPubSub {
@Override
public void onPMessage(String pattern, String channel, String message) {
super.onPMessage(pattern, channel, message);
System.out.println("onPMessage: channel=" + channel + " msg=" + message + " pattern=" + pattern);
switch (channel) {
case "system":
switch (message) {
case "openPC":
system("openPC", null);
break;
case "updateIP":
system("updateIP", null);
break;
}
break;
case "bot":
bot(message);
break;
case "cmd":
system("cmd", message);
break;
case "msg":
Tools.sendServer("来自服务姬的通知~", message);
break;
}
}
@Override
public void onMessage(String channel, String message) {
super.onMessage(channel, message);
System.out.println("onMessage: channel=" + channel + " msg=" + message);
}
public static void system(String type, String value) {
try {
String exec = null;
switch (type) {
case "openPC":
exec = "wakeonlan 00:D8:61:6F:02:2F";
break;
case "cmd":
exec = value;
break;
case "updateIP":
exec = "python3 /media/yutou/disk_lvm/public/Python/tools/ip.py";
break;
}
if (exec != null) {
Process process = Runtime.getRuntime().exec(exec);
processOut(process.getInputStream());
processOut(process.getErrorStream());
process.destroy();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void bot(String value) {
switch (value) {
case "getip":
JSONObject json = JSONObject.parseObject(HttpTools.get("https://api.asilu.com/ip/"));
String ip = json.getString("ip");
break;
}
}
}
public static void processOut(InputStream inputStream) {
String tmp;
StringBuilder str = new StringBuilder("null");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
while ((tmp = reader.readLine()) != null) {
str.append(tmp).append("\n");
}
reader.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("cmd > " + str);
System.out.println("线程结束");
}
public static void main(String[] args) {
RedisTools.pullMsg("msg", "abc");
}
}