Use Guava cache for caching PlayerAuth object.

This commit is contained in:
DNx5 2015-12-12 19:16:52 +07:00
parent a14e3260dc
commit 45f604534d
11 changed files with 229 additions and 317 deletions

View File

@ -603,7 +603,7 @@ public class AuthMe extends JavaPlugin {
} }
if (Settings.isCachingEnabled) { if (Settings.isCachingEnabled) {
database = new CacheDataSource(this, database); database = new CacheDataSource(database);
} }
if (Settings.getDataSource == DataSource.DataSourceType.FILE) { if (Settings.getDataSource == DataSource.DataSourceType.FILE) {

View File

@ -194,7 +194,7 @@ public class PlayerAuth {
this.setHash(auth.getHash()); this.setHash(auth.getHash());
this.setIp(auth.getIp()); this.setIp(auth.getIp());
this.setLastLogin(auth.getLastLogin()); this.setLastLogin(auth.getLastLogin());
this.setName(auth.getNickname()); this.setNickname(auth.getNickname());
this.setQuitLocX(auth.getQuitLocX()); this.setQuitLocX(auth.getQuitLocX());
this.setQuitLocY(auth.getQuitLocY()); this.setQuitLocY(auth.getQuitLocY());
this.setQuitLocZ(auth.getQuitLocZ()); this.setQuitLocZ(auth.getQuitLocZ());
@ -204,11 +204,11 @@ public class PlayerAuth {
} }
/** /**
* Method setName. * Method setNickname.
* *
* @param nickname String * @param nickname String
*/ */
public void setName(String nickname) { public void setNickname(String nickname) {
this.nickname = nickname.toLowerCase(); this.nickname = nickname.toLowerCase();
} }
@ -522,7 +522,7 @@ public class PlayerAuth {
public PlayerAuth build() { public PlayerAuth build() {
return new PlayerAuth( return new PlayerAuth(
checkNotNull(name).toLowerCase(), checkNotNull(name),
nullToEmpty(hash), nullToEmpty(hash),
nullToEmpty(salt), nullToEmpty(salt),
groupId, groupId,

View File

@ -40,8 +40,7 @@ public class PlayerCache {
* @param auth PlayerAuth * @param auth PlayerAuth
*/ */
public void updatePlayer(PlayerAuth auth) { public void updatePlayer(PlayerAuth auth) {
cache.remove(auth.getNickname().toLowerCase()); cache.put(auth.getNickname(), auth);
cache.put(auth.getNickname().toLowerCase(), auth);
} }
/** /**
@ -87,7 +86,7 @@ public class PlayerCache {
/** /**
* Method getCache. * Method getCache.
* *
* @return ConcurrentHashMap<String,PlayerAuth> * @return ConcurrentHashMap
*/ */
public ConcurrentHashMap<String, PlayerAuth> getCache() { public ConcurrentHashMap<String, PlayerAuth> getCache() {
return this.cache; return this.cache;

View File

@ -1,13 +1,11 @@
package fr.xephi.authme.command.executable.authme; package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandParts; import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages; import fr.xephi.authme.output.Messages;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.List; import java.util.List;
@ -26,30 +24,24 @@ public class AccountsCommand extends ExecutableCommand {
// Get the player query // Get the player query
String playerQuery = sender.getName(); String playerQuery = sender.getName();
if (commandArguments.getCount() >= 1) if (commandArguments.getCount() >= 1) {
playerQuery = commandArguments.get(0); playerQuery = commandArguments.get(0);
}
final String playerQueryFinal = playerQuery; final String playerQueryFinal = playerQuery;
// Command logic // Command logic
if (!playerQuery.contains(".")) { if (!playerQueryFinal.contains(".")) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
PlayerAuth auth; PlayerAuth auth = plugin.database.getAuth(playerQueryFinal.toLowerCase());
StringBuilder message = new StringBuilder("[AuthMe] ");
try {
auth = plugin.database.getAuth(playerQueryFinal.toLowerCase());
} catch (NullPointerException npe) {
m.send(sender, MessageKey.UNKNOWN_USER);
return;
}
if (auth == null) { if (auth == null) {
m.send(sender, MessageKey.UNKNOWN_USER); m.send(sender, MessageKey.UNKNOWN_USER);
return; return;
} }
StringBuilder message = new StringBuilder("[AuthMe] ");
List<String> accountList = plugin.database.getAllAuthsByName(auth); List<String> accountList = plugin.database.getAllAuthsByName(auth);
if (accountList == null || accountList.isEmpty()) { if (accountList.isEmpty()) {
m.send(sender, MessageKey.USER_NOT_REGISTERED); m.send(sender, MessageKey.USER_NOT_REGISTERED);
return; return;
} }
@ -67,28 +59,20 @@ public class AccountsCommand extends ExecutableCommand {
message.append('.'); message.append('.');
} }
} }
sender.sendMessage("[AuthMe] " + playerQueryFinal + " has " + String.valueOf(accountList.size()) + " accounts"); sender.sendMessage("[AuthMe] " + playerQueryFinal + " has "
+ String.valueOf(accountList.size()) + " accounts.");
sender.sendMessage(message.toString()); sender.sendMessage(message.toString());
} }
}); });
return true; return true;
} else { } else {
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
List<String> accountList; List<String> accountList = plugin.database.getAllAuthsByIp(playerQueryFinal);
try {
accountList = plugin.database.getAllAuthsByIp(playerQueryFinal);
} catch (Exception e) {
ConsoleLogger.showError(e.getMessage());
ConsoleLogger.writeStackTrace(e);
m.send(sender, MessageKey.ERROR);
return;
}
StringBuilder message = new StringBuilder("[AuthMe] "); StringBuilder message = new StringBuilder("[AuthMe] ");
if (accountList == null || accountList.isEmpty()) { if (accountList.isEmpty()) {
sender.sendMessage("[AuthMe] This IP does not exist in the database"); sender.sendMessage("[AuthMe] This IP does not exist in the database.");
return; return;
} }
if (accountList.size() == 1) { if (accountList.size() == 1) {
@ -105,7 +89,8 @@ public class AccountsCommand extends ExecutableCommand {
message.append('.'); message.append('.');
} }
} }
sender.sendMessage("[AuthMe] " + playerQueryFinal + " has " + String.valueOf(accountList.size()) + " accounts"); sender.sendMessage("[AuthMe] " + playerQueryFinal + " has "
+ String.valueOf(accountList.size()) + " accounts.");
sender.sendMessage(message.toString()); sender.sendMessage(message.toString());
} }
}); });

View File

@ -11,6 +11,7 @@ import java.util.List;
/** /**
*/ */
// TODO: remove if not used
public class ResetNameCommand extends ExecutableCommand { public class ResetNameCommand extends ExecutableCommand {
/** /**
@ -28,7 +29,7 @@ public class ResetNameCommand extends ExecutableCommand {
final AuthMe plugin = AuthMe.getInstance(); final AuthMe plugin = AuthMe.getInstance();
// Command logic // Command logic
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
List<PlayerAuth> authentications = plugin.database.getAllAuths(); List<PlayerAuth> authentications = plugin.database.getAllAuths();

View File

@ -24,16 +24,16 @@ public class SetEmailCommand extends ExecutableCommand {
* @return True if the command was executed successfully, false otherwise. * @return True if the command was executed successfully, false otherwise.
*/ */
@Override @Override
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) { public boolean executeCommand(final CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
// AuthMe plugin instance // AuthMe plugin instance
AuthMe plugin = AuthMe.getInstance(); final AuthMe plugin = AuthMe.getInstance();
// Messages instance // Messages instance
Messages m = plugin.getMessages(); final Messages m = plugin.getMessages();
// Get the player name and email address // Get the player name and email address
String playerName = commandArguments.get(0); final String playerName = commandArguments.get(0);
String playerEmail = commandArguments.get(1); final String playerEmail = commandArguments.get(1);
// Validate the email address // Validate the email address
if (!Settings.isEmailCorrect(playerEmail)) { if (!Settings.isEmailCorrect(playerEmail)) {
@ -41,26 +41,34 @@ public class SetEmailCommand extends ExecutableCommand {
return true; return true;
} }
// Validate the user plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
PlayerAuth auth = plugin.database.getAuth(playerName.toLowerCase()); @Override
if (auth == null) { public void run() {
m.send(sender, MessageKey.UNKNOWN_USER); // Validate the user
return true; PlayerAuth auth = plugin.database.getAuth(playerName);
} if (auth == null) {
m.send(sender, MessageKey.UNKNOWN_USER);
return;
}
// Set the email address // Set the email address
auth.setEmail(playerEmail);
if (!plugin.database.updateEmail(auth)) {
m.send(sender, MessageKey.ERROR);
return true;
}
// Update the player cache auth.setEmail(playerEmail);
if (PlayerCache.getInstance().getAuth(playerName.toLowerCase()) != null) if (!plugin.database.updateEmail(auth)) {
PlayerCache.getInstance().updatePlayer(auth); m.send(sender, MessageKey.ERROR);
return;
}
// Show a status message // Update the player cache
m.send(sender, MessageKey.EMAIL_CHANGED_SUCCESS); if (PlayerCache.getInstance().getAuth(playerName) != null) {
PlayerCache.getInstance().updatePlayer(auth);
}
// Show a status message
m.send(sender, MessageKey.EMAIL_CHANGED_SUCCESS);
}
});
return true; return true;
} }
} }

View File

@ -1,18 +1,16 @@
package fr.xephi.authme.datasource; package fr.xephi.authme.datasource;
import fr.xephi.authme.AuthMe; import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/** /**
*/ */
@ -20,37 +18,24 @@ public class CacheDataSource implements DataSource {
private final DataSource source; private final DataSource source;
private final ExecutorService exec; private final ExecutorService exec;
private final ConcurrentHashMap<String, PlayerAuth> cache = new ConcurrentHashMap<>(); private final LoadingCache<String, PlayerAuth> cachedAuths;
/** /**
* Constructor for CacheDataSource. * Constructor for CacheDataSource.
* *
* @param pl AuthMe
* @param src DataSource * @param src DataSource
*/ */
public CacheDataSource(final AuthMe pl, DataSource src) { public CacheDataSource(DataSource src) {
this.source = src; this.source = src;
this.exec = Executors.newCachedThreadPool(); this.exec = Executors.newCachedThreadPool();
pl.setCanConnect(false); cachedAuths = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
/* .build(
* We need to load all players in cache ... It will took more time to new CacheLoader<String, PlayerAuth>() {
* load the server, but it will be much easier to check for an public PlayerAuth load(String key) {
* isAuthAvailable ! return source.getAuth(key);
*/ }
exec.execute(new Runnable() { });
@Override
public void run() {
for (PlayerAuth auth : source.getAllAuths()) {
cache.put(auth.getNickname().toLowerCase(), auth);
}
pl.setCanConnect(true);
}
});
}
public void addAuthToCache(PlayerAuth auth) {
cache.put(auth.getNickname().toLowerCase(), auth);
} }
/** /**
@ -58,11 +43,13 @@ public class CacheDataSource implements DataSource {
* *
* @param user String * @param user String
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#isAuthAvailable(String) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#isAuthAvailable(String)
*/ */
@Override @Override
public synchronized boolean isAuthAvailable(String user) { public synchronized boolean isAuthAvailable(String user) {
return cache.containsKey(user.toLowerCase()); return getAuth(user) != null;
} }
/** /**
@ -70,15 +57,14 @@ public class CacheDataSource implements DataSource {
* *
* @param user String * @param user String
* *
* @return PlayerAuth * @see fr.xephi.authme.datasource.DataSource#getAuth(String) * @return PlayerAuth
*
* @see fr.xephi.authme.datasource.DataSource#getAuth(String)
*/ */
@Override @Override
public synchronized PlayerAuth getAuth(String user) { public synchronized PlayerAuth getAuth(String user) {
user = user.toLowerCase(); user = user.toLowerCase();
if (cache.containsKey(user)) { return cachedAuths.getUnchecked(user);
return cache.get(user);
}
return null;
} }
/** /**
@ -86,20 +72,17 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#saveAuth(PlayerAuth) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#saveAuth(PlayerAuth)
*/ */
@Override @Override
public synchronized boolean saveAuth(final PlayerAuth auth) { public synchronized boolean saveAuth(PlayerAuth auth) {
cache.put(auth.getNickname(), auth); boolean result = source.saveAuth(auth);
exec.execute(new Runnable() { if (result) {
@Override cachedAuths.refresh(auth.getNickname());
public void run() { }
if (!source.saveAuth(auth)) { return result;
cache.remove(auth.getNickname());
}
}
});
return true;
} }
/** /**
@ -107,26 +90,17 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updatePassword(PlayerAuth) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#updatePassword(PlayerAuth)
*/ */
@Override @Override
public synchronized boolean updatePassword(final PlayerAuth auth) { public synchronized boolean updatePassword(PlayerAuth auth) {
if (!cache.containsKey(auth.getNickname())) { boolean result = source.updatePassword(auth);
return false; if (result) {
cachedAuths.refresh(auth.getNickname());
} }
final String oldHash = cache.get(auth.getNickname()).getHash(); return result;
cache.get(auth.getNickname()).setHash(auth.getHash());
exec.execute(new Runnable() {
@Override
public void run() {
if (!source.updatePassword(auth)) {
if (cache.containsKey(auth.getNickname())) {
cache.get(auth.getNickname()).setHash(oldHash);
}
}
}
});
return true;
} }
/** /**
@ -134,35 +108,17 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth)
*/ */
@Override @Override
public boolean updateSession(final PlayerAuth auth) { public boolean updateSession(PlayerAuth auth) {
if (!cache.containsKey(auth.getNickname())) { boolean result = source.updateSession(auth);
return false; if (result) {
cachedAuths.refresh(auth.getNickname());
} }
PlayerAuth cachedAuth = cache.get(auth.getNickname()); return result;
final String oldIp = cachedAuth.getIp();
final long oldLastLogin = cachedAuth.getLastLogin();
final String oldRealName = cachedAuth.getRealName();
cachedAuth.setIp(auth.getIp());
cachedAuth.setLastLogin(auth.getLastLogin());
cachedAuth.setRealName(auth.getRealName());
exec.execute(new Runnable() {
@Override
public void run() {
if (!source.updateSession(auth)) {
if (cache.containsKey(auth.getNickname())) {
PlayerAuth cachedAuth = cache.get(auth.getNickname());
cachedAuth.setIp(oldIp);
cachedAuth.setLastLogin(oldLastLogin);
cachedAuth.setRealName(oldRealName);
}
}
}
});
return true;
} }
/** /**
@ -170,38 +126,17 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth)
*/ */
@Override @Override
public boolean updateQuitLoc(final PlayerAuth auth) { public boolean updateQuitLoc(final PlayerAuth auth) {
if (!cache.containsKey(auth.getNickname())) { boolean result = source.updateSession(auth);
return false; if (result) {
cachedAuths.refresh(auth.getNickname());
} }
final PlayerAuth cachedAuth = cache.get(auth.getNickname()); return result;
final double oldX = cachedAuth.getQuitLocX();
final double oldY = cachedAuth.getQuitLocY();
final double oldZ = cachedAuth.getQuitLocZ();
final String oldWorld = cachedAuth.getWorld();
cachedAuth.setQuitLocX(auth.getQuitLocX());
cachedAuth.setQuitLocY(auth.getQuitLocY());
cachedAuth.setQuitLocZ(auth.getQuitLocZ());
cachedAuth.setWorld(auth.getWorld());
exec.execute(new Runnable() {
@Override
public void run() {
if (!source.updateQuitLoc(auth)) {
if (cache.containsKey(auth.getNickname())) {
PlayerAuth cachedAuth = cache.get(auth.getNickname());
cachedAuth.setQuitLocX(oldX);
cachedAuth.setQuitLocY(oldY);
cachedAuth.setQuitLocZ(oldZ);
cachedAuth.setWorld(oldWorld);
}
}
}
});
return true;
} }
/** /**
@ -209,17 +144,13 @@ public class CacheDataSource implements DataSource {
* *
* @param ip String * @param ip String
* *
* @return int * @see fr.xephi.authme.datasource.DataSource#getIps(String) * @return int
*
* @see fr.xephi.authme.datasource.DataSource#getIps(String)
*/ */
@Override @Override
public int getIps(String ip) { public int getIps(String ip) {
int count = 0; return source.getIps(ip);
for (Map.Entry<String, PlayerAuth> p : cache.entrySet()) {
if (p.getValue().getIp().equals(ip)) {
count++;
}
}
return count;
} }
/** /**
@ -227,15 +158,17 @@ public class CacheDataSource implements DataSource {
* *
* @param until long * @param until long
* *
* @return int * @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long) * @return int
*
* @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long)
*/ */
@Override @Override
public int purgeDatabase(long until) { public int purgeDatabase(long until) {
int cleared = source.purgeDatabase(until); int cleared = source.purgeDatabase(until);
if (cleared > 0) { if (cleared > 0) {
for (PlayerAuth auth : cache.values()) { for (PlayerAuth auth : cachedAuths.asMap().values()) {
if (auth.getLastLogin() < until) { if (auth != null && auth.getLastLogin() < until) {
cache.remove(auth.getNickname()); cachedAuths.invalidate(auth.getNickname());
} }
} }
} }
@ -247,17 +180,15 @@ public class CacheDataSource implements DataSource {
* *
* @param until long * @param until long
* *
* @return List<String> * @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long) * @return List
*
* @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long)
*/ */
@Override @Override
public List<String> autoPurgeDatabase(long until) { public List<String> autoPurgeDatabase(long until) {
List<String> cleared = source.autoPurgeDatabase(until); List<String> cleared = source.autoPurgeDatabase(until);
if (cleared.size() > 0) { for (String name : cleared) {
for (PlayerAuth auth : cache.values()) { cachedAuths.invalidate(name);
if (auth.getLastLogin() < until) {
cache.remove(auth.getNickname());
}
}
} }
return cleared; return cleared;
} }
@ -265,24 +196,20 @@ public class CacheDataSource implements DataSource {
/** /**
* Method removeAuth. * Method removeAuth.
* *
* @param username String * @param name String
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#removeAuth(String) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#removeAuth(String)
*/ */
@Override @Override
public synchronized boolean removeAuth(String username) { public synchronized boolean removeAuth(String name) {
final String user = username.toLowerCase(); name = name.toLowerCase();
final PlayerAuth auth = cache.get(user); boolean result = source.removeAuth(name);
cache.remove(user); if (result) {
exec.execute(new Runnable() { cachedAuths.invalidate(name);
@Override }
public void run() { return result;
if (!source.removeAuth(user)) {
cache.put(user, auth);
}
}
});
return true;
} }
/** /**
@ -302,19 +229,12 @@ public class CacheDataSource implements DataSource {
* @see fr.xephi.authme.datasource.DataSource#reload() * @see fr.xephi.authme.datasource.DataSource#reload()
*/ */
@Override @Override
public void reload() { public void reload() { // unused method
exec.execute(new Runnable() { exec.execute(new Runnable() {
@Override @Override
public void run() { public void run() {
cache.clear();
source.reload(); source.reload();
for (Player player : Utils.getOnlinePlayers()) { cachedAuths.invalidateAll();
String user = player.getName().toLowerCase();
if (PlayerCache.getInstance().isAuthenticated(user)) {
PlayerAuth auth = source.getAuth(user);
cache.put(user, auth);
}
}
} }
}); });
} }
@ -324,19 +244,17 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth)
*/ */
@Override @Override
public synchronized boolean updateEmail(final PlayerAuth auth) { public synchronized boolean updateEmail(final PlayerAuth auth) {
try { boolean result = source.updateEmail(auth);
return exec.submit(new Callable<Boolean>() { if (result) {
public Boolean call() { cachedAuths.refresh(auth.getNickname());
return source.updateEmail(auth);
}
}).get();
} catch (Exception e) {
return false;
} }
return result;
} }
/** /**
@ -344,27 +262,17 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth)
*/ */
@Override @Override
public synchronized boolean updateSalt(final PlayerAuth auth) { public synchronized boolean updateSalt(final PlayerAuth auth) {
if (!cache.containsKey(auth.getNickname())) { boolean result = source.updateSalt(auth);
return false; if (result) {
cachedAuths.refresh(auth.getNickname());
} }
PlayerAuth cachedAuth = cache.get(auth.getNickname()); return result;
final String oldSalt = cachedAuth.getSalt();
cachedAuth.setSalt(auth.getSalt());
exec.execute(new Runnable() {
@Override
public void run() {
if (!source.updateSalt(auth)) {
if (cache.containsKey(auth.getNickname())) {
cache.get(auth.getNickname()).setSalt(oldSalt);
}
}
}
});
return true;
} }
/** /**
@ -372,17 +280,13 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return List<String> * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth) * @return List
*
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth)
*/ */
@Override @Override
public synchronized List<String> getAllAuthsByName(PlayerAuth auth) { public synchronized List<String> getAllAuthsByName(PlayerAuth auth) {
List<String> result = new ArrayList<>(); return source.getAllAuthsByName(auth);
for (Map.Entry<String, PlayerAuth> stringPlayerAuthEntry : cache.entrySet()) {
PlayerAuth p = stringPlayerAuthEntry.getValue();
if (p.getIp().equals(auth.getIp()))
result.add(p.getNickname());
}
return result;
} }
/** /**
@ -390,15 +294,13 @@ public class CacheDataSource implements DataSource {
* *
* @param ip String * @param ip String
* *
* @return List<String> * @throws Exception * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String) * @return List
*
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String)
*/ */
@Override @Override
public synchronized List<String> getAllAuthsByIp(final String ip) throws Exception { public synchronized List<String> getAllAuthsByIp(final String ip) {
return exec.submit(new Callable<List<String>>() { return source.getAllAuthsByIp(ip);
public List<String> call() throws Exception {
return source.getAllAuthsByIp(ip);
}
}).get();
} }
/** /**
@ -406,15 +308,13 @@ public class CacheDataSource implements DataSource {
* *
* @param email String * @param email String
* *
* @return List<String> * @throws Exception * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String) * @return List
*
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String)
*/ */
@Override @Override
public synchronized List<String> getAllAuthsByEmail(final String email) throws Exception { public synchronized List<String> getAllAuthsByEmail(final String email) {
return exec.submit(new Callable<List<String>>() { return source.getAllAuthsByEmail(email);
public List<String> call() throws Exception {
return source.getAllAuthsByEmail(email);
}
}).get();
} }
/** /**
@ -422,7 +322,7 @@ public class CacheDataSource implements DataSource {
* *
* @param banned List<String> * @param banned List<String>
* *
* @see fr.xephi.authme.datasource.DataSource#purgeBanned(List<String>) * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List)
*/ */
@Override @Override
public synchronized void purgeBanned(final List<String> banned) { public synchronized void purgeBanned(final List<String> banned) {
@ -430,11 +330,7 @@ public class CacheDataSource implements DataSource {
@Override @Override
public void run() { public void run() {
source.purgeBanned(banned); source.purgeBanned(banned);
for (PlayerAuth auth : cache.values()) { cachedAuths.invalidateAll(banned);
if (banned.contains(auth.getNickname())) {
cache.remove(auth.getNickname());
}
}
} }
}); });
} }
@ -442,7 +338,9 @@ public class CacheDataSource implements DataSource {
/** /**
* Method getType. * Method getType.
* *
* @return DataSourceType * @see fr.xephi.authme.datasource.DataSource#getType() * @return DataSourceType
*
* @see fr.xephi.authme.datasource.DataSource#getType()
*/ */
@Override @Override
public DataSourceType getType() { public DataSourceType getType() {
@ -454,12 +352,13 @@ public class CacheDataSource implements DataSource {
* *
* @param user String * @param user String
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#isLogged(String) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#isLogged(String)
*/ */
@Override @Override
public boolean isLogged(String user) { public boolean isLogged(String user) {
user = user.toLowerCase(); return PlayerCache.getInstance().isAuthenticated(user);
return PlayerCache.getInstance().getCache().containsKey(user);
} }
/** /**
@ -507,6 +406,7 @@ public class CacheDataSource implements DataSource {
@Override @Override
public void run() { public void run() {
source.purgeLogged(); source.purgeLogged();
cachedAuths.invalidateAll();
} }
}); });
} }
@ -514,11 +414,13 @@ public class CacheDataSource implements DataSource {
/** /**
* Method getAccountsRegistered. * Method getAccountsRegistered.
* *
* @return int * @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered() * @return int
*
* @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered()
*/ */
@Override @Override
public int getAccountsRegistered() { public int getAccountsRegistered() {
return cache.size(); return source.getAccountsRegistered();
} }
/** /**
@ -531,14 +433,11 @@ public class CacheDataSource implements DataSource {
*/ */
@Override @Override
public void updateName(final String oldOne, final String newOne) { public void updateName(final String oldOne, final String newOne) {
if (cache.containsKey(oldOne)) {
cache.put(newOne, cache.get(oldOne));
cache.remove(oldOne);
}
exec.execute(new Runnable() { exec.execute(new Runnable() {
@Override @Override
public void run() { public void run() {
source.updateName(oldOne, newOne); source.updateName(oldOne, newOne);
cachedAuths.invalidate(oldOne);
} }
}); });
} }
@ -546,17 +445,21 @@ public class CacheDataSource implements DataSource {
/** /**
* Method getAllAuths. * Method getAllAuths.
* *
* @return List<PlayerAuth> * @see fr.xephi.authme.datasource.DataSource#getAllAuths() * @return List
*
* @see fr.xephi.authme.datasource.DataSource#getAllAuths()
*/ */
@Override @Override
public List<PlayerAuth> getAllAuths() { public List<PlayerAuth> getAllAuths() {
return new ArrayList<>(cache.values()); return source.getAllAuths();
} }
/** /**
* Method getLoggedPlayers. * Method getLoggedPlayers.
* *
* @return List<PlayerAuth> * @see fr.xephi.authme.datasource.DataSource#getLoggedPlayers() * @return List
*
* @see fr.xephi.authme.datasource.DataSource#getLoggedPlayers()
*/ */
@Override @Override
public List<PlayerAuth> getLoggedPlayers() { public List<PlayerAuth> getLoggedPlayers() {

View File

@ -114,7 +114,7 @@ public interface DataSource {
* *
* @return List<String> * @throws Exception * @return List<String> * @throws Exception
*/ */
List<String> getAllAuthsByIp(String ip) throws Exception; List<String> getAllAuthsByIp(String ip);
/** /**
* Method getAllAuthsByEmail. * Method getAllAuthsByEmail.
@ -123,7 +123,7 @@ public interface DataSource {
* *
* @return List<String> * @throws Exception * @return List<String> * @throws Exception
*/ */
List<String> getAllAuthsByEmail(String email) throws Exception; List<String> getAllAuthsByEmail(String email);
/** /**
* Method updateEmail. * Method updateEmail.

View File

@ -6,7 +6,13 @@ import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import java.io.*; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -564,7 +570,7 @@ public class FlatFile implements DataSource {
BufferedReader br = null; BufferedReader br = null;
try { try {
br = new BufferedReader(new FileReader(source)); br = new BufferedReader(new FileReader(source));
String line = ""; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] args = line.split(":"); String[] args = line.split(":");
if (args[0].equals(auth.getNickname())) { if (args[0].equals(auth.getNickname())) {
@ -721,7 +727,7 @@ public class FlatFile implements DataSource {
* *
* @param banned List<String> * @param banned List<String>
* *
* @see fr.xephi.authme.datasource.DataSource#purgeBanned(List<String>) * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List)
*/ */
@Override @Override
public void purgeBanned(List<String> banned) { public void purgeBanned(List<String> banned) {
@ -856,7 +862,7 @@ public class FlatFile implements DataSource {
@Override @Override
public void updateName(String oldOne, String newOne) { public void updateName(String oldOne, String newOne) {
PlayerAuth auth = this.getAuth(oldOne); PlayerAuth auth = this.getAuth(oldOne);
auth.setName(newOne); auth.setNickname(newOne);
this.saveAuth(auth); this.saveAuth(auth);
this.removeAuth(oldOne); this.removeAuth(oldOne);
} }

View File

@ -655,6 +655,7 @@ public class MySQL implements DataSource {
while (rs.next()) { while (rs.next()) {
list.add(rs.getString(columnName)); list.add(rs.getString(columnName));
} }
rs.close();
sql = "DELETE FROM " + tableName + " WHERE " + columnLastLogin + "<" + until; sql = "DELETE FROM " + tableName + " WHERE " + columnLastLogin + "<" + until;
st.executeUpdate(sql); st.executeUpdate(sql);
st.close(); st.close();
@ -915,11 +916,10 @@ public class MySQL implements DataSource {
* *
* @return List * @return List
* *
* @throws SQLException
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String) * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String)
*/ */
@Override @Override
public synchronized List<String> getAllAuthsByEmail(String email) throws SQLException { public synchronized List<String> getAllAuthsByEmail(String email){
List<String> countEmail = new ArrayList<>(); List<String> countEmail = new ArrayList<>();
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnEmail + "=?;"; String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnEmail + "=?;";
@ -931,8 +931,11 @@ public class MySQL implements DataSource {
} }
rs.close(); rs.close();
pst.close(); pst.close();
return countEmail; } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
} }
return countEmail;
} }
/** /**

View File

@ -49,25 +49,32 @@ public class BungeeCordMessage implements PluginMessageListener {
if (subChannel.equalsIgnoreCase("AuthMe")) { if (subChannel.equalsIgnoreCase("AuthMe")) {
String str = in.readUTF(); String str = in.readUTF();
String[] args = str.split(";"); String[] args = str.split(";");
String name = args[1]; final String act = args[0];
PlayerAuth auth = plugin.database.getAuth(name); final String name = args[1];
if (auth == null) { plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
return; @Override
} public void run() {
if ("login".equals(args[0])) { PlayerAuth auth = plugin.database.getAuth(name);
PlayerCache.getInstance().updatePlayer(auth); if (auth == null) {
plugin.database.setLogged(name); return;
ConsoleLogger.info("Player " + auth.getNickname() + " has logged in from one of your server!"); }
} else if ("logout".equals(args[0])) { if ("login".equals(act)) {
PlayerCache.getInstance().removePlayer(name); PlayerCache.getInstance().updatePlayer(auth);
plugin.database.setUnlogged(name); plugin.database.setLogged(name);
ConsoleLogger.info("Player " + auth.getNickname() + " has logged out from one of your server!"); ConsoleLogger.info("Player " + auth.getNickname()
} else if ("register".equals(args[0])) { + " has logged in from one of your server!");
if (plugin.database instanceof CacheDataSource) { } else if ("logout".equals(act)) {
((CacheDataSource)plugin.database).addAuthToCache(auth); PlayerCache.getInstance().removePlayer(name);
plugin.database.setUnlogged(name);
ConsoleLogger.info("Player " + auth.getNickname()
+ " has logged out from one of your server!");
} else if ("register".equals(act)) {
ConsoleLogger.info("Player " + auth.getNickname()
+ " has registered out from one of your server!");
}
} }
ConsoleLogger.info("Player " + auth.getNickname() + " has registered out from one of your server!"); });
}
} }
} }