PC-966 Implement report user cache
This commit is contained in:
parent
a39d894d85
commit
ee6e8a9656
@ -7,12 +7,17 @@ import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.RemovalListener;
|
||||
import com.google.common.cache.RemovalNotification;
|
||||
import mineplex.core.common.util.UtilFuture;
|
||||
import mineplex.core.report.ReportCategory;
|
||||
import mineplex.core.report.ReportResultType;
|
||||
@ -38,6 +43,11 @@ public class ReportUserRepository
|
||||
|
||||
private final JavaPlugin _plugin;
|
||||
|
||||
private final Cache<Integer, ReportUser> _cachedUsers = CacheBuilder.newBuilder()
|
||||
.maximumSize(1000)
|
||||
.weakValues()
|
||||
.build();
|
||||
|
||||
public ReportUserRepository(JavaPlugin plugin)
|
||||
{
|
||||
_plugin = plugin;
|
||||
@ -57,26 +67,35 @@ public class ReportUserRepository
|
||||
|
||||
public CompletableFuture<ReportUser> getUser(int accountId)
|
||||
{
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
{
|
||||
ReportUser user = new ReportUser(accountId);
|
||||
ReportUser cachedUser = _cachedUsers.getIfPresent(accountId);
|
||||
|
||||
try (Connection connection = DBPool.getAccount().getConnection())
|
||||
{
|
||||
loadTeams(connection, user);
|
||||
loadStatistics(connection, user);
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return user;
|
||||
}).exceptionally(throwable ->
|
||||
if (cachedUser != null)
|
||||
{
|
||||
_plugin.getLogger().log(Level.SEVERE, "Error fetching ReportUser (id: " + accountId + ").", throwable);
|
||||
return null;
|
||||
});
|
||||
return CompletableFuture.completedFuture(cachedUser);
|
||||
}
|
||||
else
|
||||
{
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
{
|
||||
ReportUser user = new ReportUser(accountId);
|
||||
|
||||
try (Connection connection = DBPool.getAccount().getConnection())
|
||||
{
|
||||
loadTeams(connection, user);
|
||||
loadStatistics(connection, user);
|
||||
_cachedUsers.put(accountId, user);
|
||||
return user;
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}).exceptionally(throwable ->
|
||||
{
|
||||
_plugin.getLogger().log(Level.SEVERE, "Error fetching ReportUser (id: " + accountId + ").", throwable);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void loadStatistics(Connection connection, ReportUser user) throws SQLException
|
||||
|
Loading…
Reference in New Issue
Block a user