update:redis新增对map的支持

This commit is contained in:
Yutousama 2022-07-26 22:45:11 +08:00
parent 788bd24d92
commit af54377bb2

View File

@ -14,6 +14,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
@ -24,7 +25,7 @@ public class RedisTools {
private static String host; private static String host;
private static int port; private static int port;
public static int TOKEN_TIMEOUT_DEFAULT = 360; public static int TOKEN_TIMEOUT_DEFAULT = 360;
public final static int DATABASES_ALI_OSS=4; public final static int DATABASES_ALI_OSS = 4;
private RedisTools() { private RedisTools() {
@ -57,13 +58,13 @@ public class RedisTools {
return true; return true;
} }
public static boolean set (String key,String value, long timeout,int db){ public static boolean set(String key, String value, long timeout, int db) {
try { try {
if (isNotInstallRedis) { if (isNotInstallRedis) {
return false; return false;
} }
Jedis jedis = getRedis(); Jedis jedis = getRedis();
if(db>-1){ if (db > -1) {
jedis.select(db); jedis.select(db);
} }
if (timeout == -1) { if (timeout == -1) {
@ -81,11 +82,11 @@ public class RedisTools {
} }
public static boolean set(Object key, String value) { public static boolean set(Object key, String value) {
return set(QQBOT_USER, key+"", value); return set(QQBOT_USER, key + "", value);
} }
public static boolean set(String key, String value, long timeout) { public static boolean set(String key, String value, long timeout) {
return set(key, value, timeout,QQBOT_USER); return set(key, value, timeout, QQBOT_USER);
} }
public static String get(String key, int dbIndex) { public static String get(String key, int dbIndex) {
@ -105,19 +106,19 @@ public class RedisTools {
} }
public static String get(Object key) { public static String get(Object key) {
return get(key+"", QQBOT_USER); return get(key + "", QQBOT_USER);
} }
public static String get(Object key,String defValue){ public static String get(Object key, String defValue) {
String ret=get(key); String ret = get(key);
if(StringUtils.isEmpty(ret)){ if (StringUtils.isEmpty(ret)) {
return defValue; return defValue;
} }
return ret; return ret;
} }
public static boolean remove(String key) { public static boolean remove(String key) {
return remove(key,QQBOT_USER); return remove(key, QQBOT_USER);
} }
public static void removeLoginState(String uid) { public static void removeLoginState(String uid) {
@ -137,20 +138,22 @@ public class RedisTools {
jedis.close(); jedis.close();
return tmp; return tmp;
} }
public static boolean exists(Object key,String value){
return exists(QQBOT_USER,key,value); public static boolean exists(Object key, String value) {
return exists(QQBOT_USER, key, value);
} }
public static boolean exists(int db,Object key, String value) {
public static boolean exists(int db, Object key, String value) {
if (isNotInstallRedis) { if (isNotInstallRedis) {
return false; return false;
} }
Jedis jedis = getRedis(); Jedis jedis = getRedis();
jedis.select(db); jedis.select(db);
boolean flag=false; boolean flag = false;
if(value==null){ if (value == null) {
flag= jedis.exists(key+""); flag = jedis.exists(key + "");
}else { } else {
flag = value.equals(jedis.get(key+"")); flag = value.equals(jedis.get(key + ""));
} }
jedis.close(); jedis.close();
return flag; return flag;
@ -185,13 +188,45 @@ public class RedisTools {
public static boolean list_isExist(String listName, String value) { public static boolean list_isExist(String listName, String value) {
Jedis jedis = getRedis(); Jedis jedis = getRedis();
jedis.select(QQBOT_USER);
boolean flag = jedis.sismember(listName, value); boolean flag = jedis.sismember(listName, value);
jedis.close(); jedis.close();
return flag; return flag;
} }
public static String getHashMap(String hashKey, String key) {
Jedis jedis = getRedis();
return jedis.hget(hashKey, key);
}
public static Map<String, String> getHashMap(String hashKey) {
return getRedis().hgetAll(hashKey);
}
public static boolean setHashMap(String hashKey, String key, String value) {
Jedis jedis = getRedis();
long l = jedis.hsetnx(hashKey, key, value);
jedis.close();
return l > 0;
}
public static boolean removeHashMap(String hashKey, String key) {
Jedis jedis = getRedis();
long l = jedis.hdel(hashKey, key);
jedis.close();
return l > 0;
}
public static boolean removeHashMap(String hashKey) {
Jedis jedis = getRedis();
long del = jedis.del(hashKey);
return del > 0;
}
public static Jedis getRedis() { public static Jedis getRedis() {
return new Jedis(host, port); Jedis jedis = new Jedis(host, port);
jedis.select(QQBOT_USER);
return jedis;
} }
public static boolean remove(String key, int index) { public static boolean remove(String key, int index) {
@ -200,13 +235,9 @@ public class RedisTools {
} }
Jedis jedis = getRedis(); Jedis jedis = getRedis();
jedis.select(index); jedis.select(index);
Long i = jedis.del(key); long i = jedis.del(key);
jedis.close(); jedis.close();
if (i > 0) { return i > 0;
return true;
} else {
return false;
}
} }
private static class PropertyUtil { private static class PropertyUtil {
@ -287,7 +318,7 @@ public class RedisTools {
system("cmd", message); system("cmd", message);
break; break;
case "msg": case "msg":
AppTools.sendServer("来自服务姬的通知~",message); AppTools.sendServer("来自服务姬的通知~", message);
break; break;
} }
} }
@ -324,7 +355,6 @@ public class RedisTools {
} }
public static void bot(String value) { public static void bot(String value) {
switch (value) { switch (value) {
case "getip": case "getip":
@ -335,10 +365,12 @@ public class RedisTools {
} }
} }
} }
public static void processOut(InputStream inputStream) { public static void processOut(InputStream inputStream) {
processOut(inputStream,null,true); processOut(inputStream, null, true);
} }
public static void processOut(InputStream inputStream, ObjectInterface objectInterface, boolean isOutQQBot){
public static void processOut(InputStream inputStream, ObjectInterface objectInterface, boolean isOutQQBot) {
String tmp; String tmp;
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
try { try {
@ -351,14 +383,15 @@ public class RedisTools {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
if(objectInterface!=null){ if (objectInterface != null) {
objectInterface.out(str.toString()); objectInterface.out(str.toString());
} }
// Log.i("cmd > " + str); // Log.i("cmd > " + str);
if(isOutQQBot) { if (isOutQQBot) {
QQBotManager.getInstance().sendMessage(str.toString()); QQBotManager.getInstance().sendMessage(str.toString());
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
RedisTools.pullMsg("msg", "abc"); RedisTools.pullMsg("msg", "abc");
} }