Async refresh.

This commit is contained in:
DNx5 2016-02-26 11:37:47 +07:00
parent ed0ed2faf7
commit 775e2f7039

View File

@ -4,14 +4,19 @@ 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;
import com.google.common.cache.RemovalListener; import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.cache.RemovalNotification; import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import fr.xephi.authme.ConsoleLogger;
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.security.crypts.HashedPassword; import fr.xephi.authme.security.crypts.HashedPassword;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -20,6 +25,7 @@ public class CacheDataSource implements DataSource {
private final DataSource source; private final DataSource source;
private final LoadingCache<String, Optional<PlayerAuth>> cachedAuths; private final LoadingCache<String, Optional<PlayerAuth>> cachedAuths;
private final ListeningExecutorService executorService;
/** /**
* Constructor for CacheDataSource. * Constructor for CacheDataSource.
@ -27,25 +33,31 @@ public class CacheDataSource implements DataSource {
* @param src DataSource * @param src DataSource
*/ */
public CacheDataSource(DataSource src) { public CacheDataSource(DataSource src) {
this.source = src; source = src;
this.cachedAuths = CacheBuilder.newBuilder() executorService = MoreExecutors.listeningDecorator(
.expireAfterWrite(8, TimeUnit.MINUTES) Executors.newCachedThreadPool(new ThreadFactoryBuilder()
.removalListener(new RemovalListener<String, Optional<PlayerAuth>>() { .setDaemon(true)
.setNameFormat("AuthMe-CacheLoader")
.build())
);
cachedAuths = CacheBuilder.newBuilder()
.refreshAfterWrite(8, TimeUnit.MINUTES)
.build(new CacheLoader<String, Optional<PlayerAuth>>() {
@Override @Override
public void onRemoval(RemovalNotification<String, Optional<PlayerAuth>> removalNotification) { public Optional<PlayerAuth> load(String key) {
String name = removalNotification.getKey(); return Optional.fromNullable(source.getAuth(key));
if (PlayerCache.getInstance().isAuthenticated(name)) {
cachedAuths.getUnchecked(name);
}
} }
})
.build( @Override
new CacheLoader<String, Optional<PlayerAuth>>() { public ListenableFuture<Optional<PlayerAuth>> reload(final String key, Optional<PlayerAuth> oldValue) {
@Override return executorService.submit(new Callable<Optional<PlayerAuth>>() {
public Optional<PlayerAuth> load(String key) { @Override
return Optional.fromNullable(source.getAuth(key)); public Optional<PlayerAuth> call() {
} return load(key);
}); }
});
}
});
} }
@Override @Override
@ -138,6 +150,12 @@ public class CacheDataSource implements DataSource {
public synchronized void close() { public synchronized void close() {
source.close(); source.close();
cachedAuths.invalidateAll(); cachedAuths.invalidateAll();
executorService.shutdown();
try {
executorService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
ConsoleLogger.writeStackTrace(e);
}
} }
@Override @Override