Use more robust Guava Cache class for caching

This commit is contained in:
Keir Nellyer 2016-06-10 12:33:46 +01:00
parent 4503f12738
commit 2cacdbae34

View File

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