From e0831822cd16f0cd54fd58febc85f4ad1f592dcd Mon Sep 17 00:00:00 2001 From: Shuiling <1668589925@qq.com> Date: Wed, 28 Aug 2024 02:58:24 -0700 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BA=86org.apac?= =?UTF-8?q?he.commons.lang3=E4=BE=9D=E8=B5=96=EF=BC=8C=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=86=E9=9A=8F=E6=9C=BA=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=94=9F?= =?UTF-8?q?=E6=88=90=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 ++++++- src/main/java/cc/baka9/catseedlogin/util/Util.java | 14 ++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index 9459b71..b3e97ff 100644 --- a/pom.xml +++ b/pom.xml @@ -95,7 +95,12 @@ org.apache.commons commons-email - 1.5 + 1.5 + + + org.apache.commons + commons-lang3 + 3.12.0 diff --git a/src/main/java/cc/baka9/catseedlogin/util/Util.java b/src/main/java/cc/baka9/catseedlogin/util/Util.java index ebeae8b..55f1771 100644 --- a/src/main/java/cc/baka9/catseedlogin/util/Util.java +++ b/src/main/java/cc/baka9/catseedlogin/util/Util.java @@ -6,6 +6,8 @@ import java.util.Random; import java.util.regex.Pattern; +import org.apache.commons.lang3.RandomStringUtils; + public class Util { private static final Pattern passwordDifficultyRegex = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$"); private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @@ -26,19 +28,11 @@ public static boolean checkMail(String e_mail) { } public static String randomStr() { - int leftLimit = 97; // letter 'a' - int rightLimit = 122; // letter 'z' - int targetStringLength = 10; - StringBuilder buffer = new StringBuilder(targetStringLength); - for (int i = 0; i < targetStringLength; i++) { - int randomLimitedInt = leftLimit + random.nextInt(rightLimit - leftLimit + 1); - buffer.append((char) randomLimitedInt); - } - return buffer.toString(); + return RandomStringUtils.randomAlphanumeric(10); } public static boolean isOSLinux() { String os = System.getProperty("os.name"); return os != null && os.toLowerCase().contains("linux"); } -} \ No newline at end of file +} From a7d0248a8e13ce02d598a76867e45bd1b6729d9c Mon Sep 17 00:00:00 2001 From: Shuiling <1668589925@qq.com> Date: Wed, 28 Aug 2024 05:07:36 -0700 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=E9=87=8D=E6=9E=84=20BufferStatemen?= =?UTF-8?q?t=20=E7=B1=BB=E4=BB=A5=E4=BC=98=E5=8C=96=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E5=92=8C=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bukkit/database/BufferStatement.java | 90 +++++-------------- 1 file changed, 24 insertions(+), 66 deletions(-) diff --git a/src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java b/src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java index fe6c6c9..61dbcf5 100644 --- a/src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java +++ b/src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java @@ -6,73 +6,31 @@ import java.util.Arrays; public class BufferStatement { - private Object[] values; - private String query; - private Exception stacktrace; + private final Object[] values; + private final String query; + private final Exception stacktrace; - /** - * Represents a PreparedStatement in a state before preparing it (E.g. No - * file I/O Required) - * - * @param query - * The query to execute. E.g. INSERT INTO accounts (user, passwd) - * VALUES (?, ?) - * @param values - * The values to replace ? with in - * query. These are in order. - */ - private static final Exception sharedException = new Exception(); + public BufferStatement(String query, Object... values) { + this.query = query; + this.values = values; + this.stacktrace = new Exception(); + this.stacktrace.fillInStackTrace(); + } - public BufferStatement(String query, Object... values) { - this.query = query; - this.values = values; - this.stacktrace = sharedException; // 重复利用一个已存在的 Exception 对象 - this.stacktrace.fillInStackTrace(); - } + public PreparedStatement prepareStatement(Connection con) throws SQLException { + PreparedStatement ps = con.prepareStatement(query); + for (int i = 0; i < values.length; i++) { + ps.setObject(i + 1, values[i]); + } + return ps; + } - /** - * Returns a prepared statement using the given connection. Will try to - * return an empty statement if something went wrong. If that fails, returns - * null. - * - * This method escapes everything automatically. - * - * @param con - * The connection to prepare this on using - * con.prepareStatement(..) - * @return The prepared statement, ready for execution. - */ - public PreparedStatement prepareStatement(Connection con) throws SQLException { - PreparedStatement ps; - int valuesLength = values.length; // 缓存数组长度 - ps = con.prepareStatement(query); - for (int i = 0; i < valuesLength; i++) { - ps.setObject(i + 1, values[i]); - } - return ps; - } + public StackTraceElement[] getStackTrace() { + return stacktrace.getStackTrace(); + } - /** - * Used for debugging. This stacktrace is recorded when the statement is - * created, so printing it to the screen will provide useful debugging - * information about where the query came from, if something went wrong - * while executing it. - * - * @return The stacktrace elements. - */ - public StackTraceElement[] getStackTrace() { - return stacktrace.getStackTrace(); - } - - /** - * @return A string representation of this statement. Returns - * "Query: " + query + ", values: " + - * Arrays.toString(values). - */ - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("Query: ").append(query).append(", values: ").append(Arrays.toString(values)); - return sb.toString(); - } -} \ No newline at end of file + @Override + public String toString() { + return "Query: " + query + ", values: " + Arrays.toString(values); + } +} From 9b389253fa590db82335eb6c6a747d71f1dc42c1 Mon Sep 17 00:00:00 2001 From: Shuiling <1668589925@qq.com> Date: Wed, 28 Aug 2024 05:13:46 -0700 Subject: [PATCH 3/4] =?UTF-8?q?feat(BufferStatement):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E4=BA=86=E5=86=97=E4=BD=99=E7=9A=84=E5=A0=86=E6=A0=88=E8=B7=9F?= =?UTF-8?q?=E8=B8=AA=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cc/baka9/catseedlogin/bukkit/database/BufferStatement.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java b/src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java index 61dbcf5..b6c55e9 100644 --- a/src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java +++ b/src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java @@ -14,7 +14,6 @@ public BufferStatement(String query, Object... values) { this.query = query; this.values = values; this.stacktrace = new Exception(); - this.stacktrace.fillInStackTrace(); } public PreparedStatement prepareStatement(Connection con) throws SQLException { From 76a7948bc4e402a52c928051a955b8e2d3cd2f81 Mon Sep 17 00:00:00 2001 From: Shuiling <1668589925@qq.com> Date: Wed, 28 Aug 2024 05:45:49 -0700 Subject: [PATCH 4/4] =?UTF-8?q?feat(database):=20=E4=BD=BF=E7=94=A8Concurr?= =?UTF-8?q?entHashMap=E4=BC=98=E5=8C=96=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../catseedlogin/bukkit/database/Cache.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/cc/baka9/catseedlogin/bukkit/database/Cache.java b/src/main/java/cc/baka9/catseedlogin/bukkit/database/Cache.java index 59ec127..6403c80 100644 --- a/src/main/java/cc/baka9/catseedlogin/bukkit/database/Cache.java +++ b/src/main/java/cc/baka9/catseedlogin/bukkit/database/Cache.java @@ -1,27 +1,27 @@ package cc.baka9.catseedlogin.bukkit.database; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + import cc.baka9.catseedlogin.bukkit.CatSeedLogin; import cc.baka9.catseedlogin.bukkit.object.LoginPlayer; -import java.util.*; - public class Cache { - private static final Hashtable PLAYER_HASHTABLE = new Hashtable<>(); + private static final Map PLAYER_HASHTABLE = new ConcurrentHashMap<>(); public static volatile boolean isLoaded = false; public static List getAllLoginPlayer(){ synchronized (PLAYER_HASHTABLE) { return new ArrayList<>(PLAYER_HASHTABLE.values()); } - } public static LoginPlayer getIgnoreCase(String name){ - return PLAYER_HASHTABLE.get(name.toLowerCase()); } - public static void refreshAll(){ isLoaded = false; CatSeedLogin.instance.runTaskAsync(() -> { @@ -45,10 +45,12 @@ public static void refresh(String name){ try { LoginPlayer newLp = CatSeedLogin.sql.get(name); String key = name.toLowerCase(); - if (newLp != null) { - PLAYER_HASHTABLE.put(key, newLp); - } else { - PLAYER_HASHTABLE.remove(key); + synchronized (PLAYER_HASHTABLE) { + if (newLp != null) { + PLAYER_HASHTABLE.put(key, newLp); + } else { + PLAYER_HASHTABLE.remove(key); + } } CatSeedLogin.instance.getLogger().info("缓存加载 " + PLAYER_HASHTABLE.size() + " 个数据"); } catch (Exception e) { @@ -57,4 +59,4 @@ public static void refresh(String name){ } }); } -} \ No newline at end of file +}