Skip to content

Commit

Permalink
feat(database): 使用ConcurrentHashMap优化缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng committed Aug 28, 2024
1 parent 9b38925 commit 76a7948
Showing 1 changed file with 13 additions and 11 deletions.
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){
}
});
}
}
}

0 comments on commit 76a7948

Please sign in to comment.