Skip to content

Commit

Permalink
[Change] Use Instance for CacheManager
Browse files Browse the repository at this point in the history
  • Loading branch information
sysnote8main committed Oct 6, 2024
1 parent 177d7fd commit c07553e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public void onEnable() {
// KDStatusReloaded API Setup
KDSAPI.loadPlugin(getLogger());

// Setup cache
cache = new CacheManager(getLogger());
// Setup cacheManager's logger
CacheManager.getInstance().setLogger(getLogger());

// Events
listener = new DisplayListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import jp.azisaba.lgw.kdstatus.sql.KillRankingData;
import jp.azisaba.lgw.rankingdisplayer.integration.KDSAPI;
import jp.azisaba.lgw.rankingdisplayer.ranking.RankingType;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
Expand All @@ -16,9 +18,16 @@

@RequiredArgsConstructor
public class CacheManager {
private final Logger logger;
@Getter
private static final CacheManager Instance = new CacheManager();
@Setter
private Logger logger;
private final long cacheHoldMilliSec = 1000 * 10;
private final HashMap<RankingType, List<KillRankingData>> cache = new HashMap<>();

private final HashMap<RankingType, List<KillRankingData>> killRankingCache = new HashMap<>();
private final HashMap<UUID, Integer> playerRankingCache = new HashMap<>();
private final HashMap<UUID, Integer> playerKillCountCache = new HashMap<>();

private final HashMap<RankingType, Long> lastUpdated = new HashMap<>();

// TODO move this to config
Expand All @@ -41,7 +50,7 @@ private synchronized void updateCache(RankingType type) {
if(dataList.isEmpty()) {
logger.warning("Failed to get top kill ranking");
} else {
cache.put(type, dataList);
killRankingCache.put(type, dataList);
}
lastUpdated.put(type, System.currentTimeMillis());
}
Expand All @@ -53,7 +62,7 @@ public List<String> getRankingAsArray(RankingType type, Player p) {
boolean containsHim = false;

if(isNeedToUpdateCache(type)) updateCache(type);
List<KillRankingData> dataList = cache.get(type);
List<KillRankingData> dataList = killRankingCache.get(type);
int count = 0;
for(KillRankingData data: dataList) {
if(count >= RANKING_SIZE) {
Expand Down

0 comments on commit c07553e

Please sign in to comment.