diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java index ebfab1eb3..435aaa993 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportRepository.java @@ -11,14 +11,18 @@ import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import com.mysql.jdbc.Statement; import mineplex.core.database.MinecraftRepository; import mineplex.serverdata.database.DBPool; @@ -83,7 +87,10 @@ public class ReportRepository extends MinecraftRepository private static final String GET_ACCOUNT_UUID = "SELECT id, uuid FROM accounts" + " WHERE id IN (%s);"; - private final Map _cachedReports = new HashMap<>(); + private final Cache _cachedReports = CacheBuilder.newBuilder() + .maximumSize(1000) + .expireAfterAccess(15, TimeUnit.MINUTES) + .build(); public ReportRepository(JavaPlugin plugin) { @@ -140,9 +147,11 @@ public class ReportRepository extends MinecraftRepository { if (reportId != -1) { - if (_cachedReports.containsKey(reportId)) + Report cachedReport = _cachedReports.getIfPresent(reportId); + + if (cachedReport != null) { - return CompletableFuture.completedFuture(_cachedReports.get(reportId)); + return CompletableFuture.completedFuture(cachedReport); } else { @@ -393,10 +402,11 @@ public class ReportRepository extends MinecraftRepository */ protected void clearCacheFor(int accountId) { - for (Map.Entry entry : new HashSet<>(_cachedReports.entrySet())) + Iterator iterator = _cachedReports.asMap().values().iterator(); + + while (iterator.hasNext()) { - int reportId = entry.getKey(); - Report report = entry.getValue(); + Report report = iterator.next(); Optional handlerIdOptional = report.getHandlerId(); CompletableFuture disposeCacheFuture = CompletableFuture.completedFuture(false); @@ -422,7 +432,7 @@ public class ReportRepository extends MinecraftRepository { if (dispose) { - _cachedReports.remove(reportId); + iterator.remove(); } }); }