Skip to content

Commit

Permalink
优化 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng authored Aug 28, 2024
1 parent 4cd8b75 commit 6dbcc69
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 88 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5</version> <!-- 请使用最新的版本号 -->
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,30 @@
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 <bold>?</bold> with in
* <bold>query</bold>. 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();
}

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
* <italic>"Query: " + query + ", values: " +
* Arrays.toString(values).</italic>
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Query: ").append(query).append(", values: ").append(Arrays.toString(values));
return sb.toString();
}
}
@Override
public String toString() {
return "Query: " + query + ", values: " + Arrays.toString(values);
}
}
24 changes: 13 additions & 11 deletions src/main/java/cc/baka9/catseedlogin/bukkit/database/Cache.java
Original file line number Diff line number Diff line change
@@ -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<String, LoginPlayer> PLAYER_HASHTABLE = new Hashtable<>();
private static final Map<String, LoginPlayer> PLAYER_HASHTABLE = new ConcurrentHashMap<>();
public static volatile boolean isLoaded = false;

public static List<LoginPlayer> 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(() -> {
Expand All @@ -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) {
Expand All @@ -57,4 +59,4 @@ public static void refresh(String name){
}
});
}
}
}
14 changes: 4 additions & 10 deletions src/main/java/cc/baka9/catseedlogin/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
}
}
}

0 comments on commit 6dbcc69

Please sign in to comment.