From 775e2f703952f9ace98e14a81d9fe944b3827681 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Fri, 26 Feb 2016 11:37:47 +0700 Subject: [PATCH] Async refresh. --- .../authme/datasource/CacheDataSource.java | 56 ++++++++++++------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java index 45b25b9a3..0d8fc542f 100644 --- a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java @@ -4,14 +4,19 @@ import com.google.common.base.Optional; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; -import com.google.common.cache.RemovalListener; -import com.google.common.cache.RemovalNotification; +import com.google.common.util.concurrent.ListenableFuture; +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.PlayerCache; import fr.xephi.authme.security.crypts.HashedPassword; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** @@ -20,6 +25,7 @@ public class CacheDataSource implements DataSource { private final DataSource source; private final LoadingCache> cachedAuths; + private final ListeningExecutorService executorService; /** * Constructor for CacheDataSource. @@ -27,25 +33,31 @@ public class CacheDataSource implements DataSource { * @param src DataSource */ public CacheDataSource(DataSource src) { - this.source = src; - this.cachedAuths = CacheBuilder.newBuilder() - .expireAfterWrite(8, TimeUnit.MINUTES) - .removalListener(new RemovalListener>() { + source = src; + executorService = MoreExecutors.listeningDecorator( + Executors.newCachedThreadPool(new ThreadFactoryBuilder() + .setDaemon(true) + .setNameFormat("AuthMe-CacheLoader") + .build()) + ); + cachedAuths = CacheBuilder.newBuilder() + .refreshAfterWrite(8, TimeUnit.MINUTES) + .build(new CacheLoader>() { @Override - public void onRemoval(RemovalNotification> removalNotification) { - String name = removalNotification.getKey(); - if (PlayerCache.getInstance().isAuthenticated(name)) { - cachedAuths.getUnchecked(name); - } + public Optional load(String key) { + return Optional.fromNullable(source.getAuth(key)); } - }) - .build( - new CacheLoader>() { - @Override - public Optional load(String key) { - return Optional.fromNullable(source.getAuth(key)); - } - }); + + @Override + public ListenableFuture> reload(final String key, Optional oldValue) { + return executorService.submit(new Callable>() { + @Override + public Optional call() { + return load(key); + } + }); + } + }); } @Override @@ -138,6 +150,12 @@ public class CacheDataSource implements DataSource { public synchronized void close() { source.close(); cachedAuths.invalidateAll(); + executorService.shutdown(); + try { + executorService.awaitTermination(5, TimeUnit.SECONDS); + } catch (InterruptedException e) { + ConsoleLogger.writeStackTrace(e); + } } @Override