Ensure NameManager caches stay consistent with multiple threads accessing them
This commit is contained in:
parent
d52c329618
commit
013a21159f
|
@ -1,20 +1,21 @@
|
|||
package com.Acrobot.Breeze.Collection;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class SimpleCache<K, V> {
|
||||
private final LinkedHashMap<K, V> map;
|
||||
private final Map<K, V> map;
|
||||
|
||||
public SimpleCache(int cacheSize) {
|
||||
map = new LinkedHashMap<K, V>(cacheSize * 10/9, 0.7f, true) {
|
||||
map = Collections.synchronizedMap(new LinkedHashMap<K, V>(cacheSize * 10/9, 0.7f, true) {
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
||||
return size() > cacheSize;
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
public V put(K key, V value) {
|
||||
|
|
|
@ -36,6 +36,8 @@ import java.util.logging.Level;
|
|||
*/
|
||||
@SuppressWarnings("UnusedAssignment") // I deliberately set the variables to null while initializing
|
||||
public class NameManager implements Listener {
|
||||
private static final Object accountsLock = new Object();
|
||||
|
||||
private static Dao<Account, String> accounts;
|
||||
|
||||
private static SimpleCache<String, Account> usernameToAccount = new SimpleCache<>(Properties.CACHE_SIZE);
|
||||
|
@ -97,6 +99,7 @@ public class NameManager implements Listener {
|
|||
*/
|
||||
public static Account getAccount(UUID uuid) {
|
||||
try {
|
||||
synchronized (accountsLock) {
|
||||
return uuidToAccount.get(uuid, () -> {
|
||||
try {
|
||||
Account account = accounts.queryBuilder().orderBy("lastSeen", false).where().eq("uuid", new SelectArg(uuid)).queryForFirst();
|
||||
|
@ -111,6 +114,7 @@ public class NameManager implements Listener {
|
|||
}
|
||||
throw new Exception("Could not find account for " + uuid);
|
||||
});
|
||||
}
|
||||
} catch (ExecutionException ignored) {
|
||||
return null;
|
||||
}
|
||||
|
@ -127,6 +131,7 @@ public class NameManager implements Listener {
|
|||
Preconditions.checkNotNull(fullName, "fullName cannot be null!");
|
||||
Preconditions.checkArgument(!fullName.isEmpty(), "fullName cannot be empty!");
|
||||
try {
|
||||
synchronized (accountsLock) {
|
||||
return usernameToAccount.get(fullName, () -> {
|
||||
try {
|
||||
Account account = accounts.queryBuilder().orderBy("lastSeen", false).where().eq("name", new SelectArg(fullName)).queryForFirst();
|
||||
|
@ -140,6 +145,7 @@ public class NameManager implements Listener {
|
|||
}
|
||||
throw new Exception("Could not find account for " + fullName);
|
||||
});
|
||||
}
|
||||
} catch (ExecutionException ignored) {
|
||||
return null;
|
||||
}
|
||||
|
@ -167,6 +173,7 @@ public class NameManager implements Listener {
|
|||
Account account = null;
|
||||
|
||||
try {
|
||||
synchronized (accountsLock) {
|
||||
account = shortToAccount.get(shortName, () -> {
|
||||
try {
|
||||
Account a = accounts.queryBuilder().where().eq("shortName", new SelectArg(shortName)).queryForFirst();
|
||||
|
@ -179,6 +186,7 @@ public class NameManager implements Listener {
|
|||
}
|
||||
throw new Exception("Could not find account for " + shortName);
|
||||
});
|
||||
}
|
||||
} catch (ExecutionException ignored) {}
|
||||
return account;
|
||||
}
|
||||
|
@ -222,6 +230,7 @@ public class NameManager implements Listener {
|
|||
final UUID uuid = player.getUniqueId();
|
||||
|
||||
Account latestAccount = null;
|
||||
synchronized (accountsLock) {
|
||||
try {
|
||||
latestAccount = accounts.queryBuilder().where().eq("uuid", new SelectArg(uuid)).and().eq("name", new SelectArg(player.getName())).queryForFirst();
|
||||
} catch (SQLException e) {
|
||||
|
@ -243,6 +252,7 @@ public class NameManager implements Listener {
|
|||
usernameToAccount.put(latestAccount.getName(), latestAccount);
|
||||
uuidToAccount.put(uuid, latestAccount);
|
||||
shortToAccount.put(latestAccount.getShortName(), latestAccount);
|
||||
}
|
||||
|
||||
return latestAccount;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue