CacheLoader doesn't accept null values.

So, use Optional instead.
This commit is contained in:
DNx5 2015-12-12 20:56:45 +07:00
parent 9f3b24db6f
commit 7b3b7766e6

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.datasource; package fr.xephi.authme.datasource;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
@ -21,7 +22,7 @@ public class CacheDataSource implements DataSource {
private final DataSource source; private final DataSource source;
private final ExecutorService exec; private final ExecutorService exec;
private final LoadingCache<String, PlayerAuth> cachedAuths; private final LoadingCache<String, Optional<PlayerAuth>> cachedAuths;
/** /**
* Constructor for CacheDataSource. * Constructor for CacheDataSource.
@ -33,9 +34,9 @@ public class CacheDataSource implements DataSource {
this.exec = Executors.newCachedThreadPool(); this.exec = Executors.newCachedThreadPool();
cachedAuths = CacheBuilder.newBuilder() cachedAuths = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES) .expireAfterWrite(5, TimeUnit.MINUTES)
.removalListener(RemovalListeners.asynchronous(new RemovalListener<String, PlayerAuth>() { .removalListener(RemovalListeners.asynchronous(new RemovalListener<String, Optional<PlayerAuth>>() {
@Override @Override
public void onRemoval(RemovalNotification<String, PlayerAuth> removalNotification) { public void onRemoval(RemovalNotification<String, Optional<PlayerAuth>> removalNotification) {
String name = removalNotification.getKey(); String name = removalNotification.getKey();
if (PlayerCache.getInstance().isAuthenticated(name)) { if (PlayerCache.getInstance().isAuthenticated(name)) {
cachedAuths.getUnchecked(name); cachedAuths.getUnchecked(name);
@ -43,9 +44,9 @@ public class CacheDataSource implements DataSource {
} }
}, exec)) }, exec))
.build( .build(
new CacheLoader<String, PlayerAuth>() { new CacheLoader<String, Optional<PlayerAuth>>() {
public PlayerAuth load(String key) { public Optional<PlayerAuth> load(String key) {
return source.getAuth(key); return Optional.fromNullable(source.getAuth(key));
} }
}); });
} }
@ -76,7 +77,7 @@ public class CacheDataSource implements DataSource {
@Override @Override
public synchronized PlayerAuth getAuth(String user) { public synchronized PlayerAuth getAuth(String user) {
user = user.toLowerCase(); user = user.toLowerCase();
return cachedAuths.getUnchecked(user); return cachedAuths.getUnchecked(user).orNull();
} }
/** /**
@ -178,9 +179,9 @@ public class CacheDataSource implements DataSource {
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 : cachedAuths.asMap().values()) { for (Optional<PlayerAuth> auth : cachedAuths.asMap().values()) {
if (auth != null && auth.getLastLogin() < until) { if (auth.isPresent() && auth.get().getLastLogin() < until) {
cachedAuths.invalidate(auth.getNickname()); cachedAuths.invalidate(auth.get().getNickname());
} }
} }
} }