use expiring map for accounts
This commit is contained in:
@ -10,6 +10,8 @@ import cc.fascinated.utils.DiscordWebhook;
|
||||
import cc.fascinated.utils.Manager;
|
||||
import cc.fascinated.utils.Priority;
|
||||
import cc.fascinated.utils.Style;
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.viaversion.viaversion.api.Via;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -30,7 +32,16 @@ import java.util.concurrent.TimeUnit;
|
||||
public class AccountManager extends Manager implements Listener {
|
||||
|
||||
private final long SAVE_INTERVAL = 5; // in minutes
|
||||
private static final Map<UUID, Account> ACCOUNTS = new HashMap<>();
|
||||
private static final Cache<UUID, Account> ACCOUNTS = CacheBuilder.newBuilder()
|
||||
.expireAfterAccess(30, TimeUnit.MINUTES)
|
||||
.removalListener(notification -> {
|
||||
Account account = (Account) notification.getValue();
|
||||
if (account != null) {
|
||||
account.save(); // Save the account when it is removed from the cache
|
||||
log.info("Saved account for {} because it was removed from the cache.", account.getName());
|
||||
}
|
||||
})
|
||||
.build();
|
||||
|
||||
public AccountManager() {
|
||||
CommandManager.registerCommand(new SaveAccountsCommand());
|
||||
@ -57,7 +68,7 @@ public class AccountManager extends Manager implements Listener {
|
||||
* @return the account
|
||||
*/
|
||||
public static Account getAccount(UUID uuid) {
|
||||
return ACCOUNTS.get(uuid);
|
||||
return ACCOUNTS.getIfPresent(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,7 +78,7 @@ public class AccountManager extends Manager implements Listener {
|
||||
* @return true if the account is already registered, false otherwise
|
||||
*/
|
||||
private boolean isAccountLoaded(UUID uuid) {
|
||||
return ACCOUNTS.containsKey(uuid);
|
||||
return ACCOUNTS.getIfPresent(uuid) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -87,7 +98,8 @@ public class AccountManager extends Manager implements Listener {
|
||||
long before = System.currentTimeMillis(),
|
||||
saved = 0; // The amount of accounts that were saved
|
||||
log.info("Saving accounts...");
|
||||
for (Account account : ACCOUNTS.values()) {
|
||||
for (Map.Entry<UUID, Account> entry : ACCOUNTS.asMap().entrySet()) {
|
||||
Account account = entry.getValue();
|
||||
boolean didSave = account.save(); // Save the account
|
||||
if (didSave) {
|
||||
saved++;
|
||||
@ -148,13 +160,10 @@ public class AccountManager extends Manager implements Listener {
|
||||
.replace("%player%", event.getPlayer().getName())
|
||||
.replace("player-color", account.getPlayerColorProfile().getColor().toString())
|
||||
));
|
||||
account.save(); // Save the account
|
||||
ACCOUNTS.remove(account.getUuid()); // Remove the account from the cache
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAetheriaDisable() {
|
||||
saveAccounts(); // Save the accounts to disk
|
||||
ACCOUNTS.clear(); // Remove the accounts from the cache
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user