Use more robust Guava Cache class for caching
This commit is contained in:
parent
4503f12738
commit
2cacdbae34
@ -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<Integer, Report> _cachedReports = new HashMap<>();
|
||||
private final Cache<Integer, Report> _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<Integer, Report> entry : new HashSet<>(_cachedReports.entrySet()))
|
||||
Iterator<Report> iterator = _cachedReports.asMap().values().iterator();
|
||||
|
||||
while (iterator.hasNext())
|
||||
{
|
||||
int reportId = entry.getKey();
|
||||
Report report = entry.getValue();
|
||||
Report report = iterator.next();
|
||||
Optional<Integer> handlerIdOptional = report.getHandlerId();
|
||||
|
||||
CompletableFuture<Boolean> disposeCacheFuture = CompletableFuture.completedFuture(false);
|
||||
@ -422,7 +432,7 @@ public class ReportRepository extends MinecraftRepository
|
||||
{
|
||||
if (dispose)
|
||||
{
|
||||
_cachedReports.remove(reportId);
|
||||
iterator.remove();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user