mirror of
https://github.com/BentoBoxWorld/Level.git
synced 2025-01-24 08:21:21 +01:00
Implement a cache for top tens (#309)
This commit is contained in:
parent
a4f8d12138
commit
4d16e9c227
@ -2,6 +2,7 @@ package world.bentobox.level;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.text.DecimalFormat;
|
||||
import java.time.Instant;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@ -31,6 +32,7 @@ import world.bentobox.level.events.IslandPreLevelEvent;
|
||||
import world.bentobox.level.objects.IslandLevels;
|
||||
import world.bentobox.level.objects.LevelsData;
|
||||
import world.bentobox.level.objects.TopTenData;
|
||||
import world.bentobox.level.util.CachedData;
|
||||
|
||||
public class LevelsManager {
|
||||
private static final String INTOPTEN = "intopten";
|
||||
@ -52,6 +54,8 @@ public class LevelsManager {
|
||||
private final Map<String, IslandLevels> levelsCache;
|
||||
// Top ten lists
|
||||
private final Map<World, TopTenData> topTenLists;
|
||||
// Cache for top tens
|
||||
private Map<World, CachedData> cache = new HashMap<>();
|
||||
|
||||
public LevelsManager(Level addon) {
|
||||
this.addon = addon;
|
||||
@ -339,7 +343,19 @@ public class LevelsManager {
|
||||
@NonNull
|
||||
public Map<String, Long> getTopTen(@NonNull World world, int size) {
|
||||
createAndCleanRankings(world);
|
||||
// Return the sorted map
|
||||
CachedData cachedData = cache.get(world);
|
||||
Instant now = Instant.now();
|
||||
|
||||
if (cachedData != null && cachedData.getLastUpdated().plusSeconds(1).isAfter(now)) {
|
||||
return cachedData.getCachedMap();
|
||||
} else {
|
||||
Map<String, Long> newTopTen = calculateTopTen(world, size);
|
||||
cache.put(world, new CachedData(newTopTen, now));
|
||||
return newTopTen;
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Long> calculateTopTen(@NonNull World world, int size) {
|
||||
return Collections.unmodifiableMap(topTenLists.get(world).getTopTen().entrySet().stream()
|
||||
.filter(l -> l.getValue() > 0).sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
|
||||
.limit(size)
|
||||
@ -416,6 +432,8 @@ public class LevelsManager {
|
||||
public void removeEntry(World world, String uuid) {
|
||||
if (topTenLists.containsKey(world)) {
|
||||
topTenLists.get(world).getTopTen().remove(uuid);
|
||||
// Invalidate the cache because of this deletion
|
||||
cache.remove(world);
|
||||
}
|
||||
|
||||
}
|
||||
|
30
src/main/java/world/bentobox/level/util/CachedData.java
Normal file
30
src/main/java/world/bentobox/level/util/CachedData.java
Normal file
@ -0,0 +1,30 @@
|
||||
package world.bentobox.level.util;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Cache for top tens
|
||||
*/
|
||||
public class CachedData {
|
||||
private Map<String, Long> cachedMap;
|
||||
private Instant lastUpdated;
|
||||
|
||||
public CachedData(Map<String, Long> cachedMap, Instant lastUpdated) {
|
||||
this.cachedMap = cachedMap;
|
||||
this.lastUpdated = lastUpdated;
|
||||
}
|
||||
|
||||
public Map<String, Long> getCachedMap() {
|
||||
return cachedMap;
|
||||
}
|
||||
|
||||
public Instant getLastUpdated() {
|
||||
return lastUpdated;
|
||||
}
|
||||
|
||||
public void updateCache(Map<String, Long> newMap, Instant newUpdateTime) {
|
||||
this.cachedMap = newMap;
|
||||
this.lastUpdated = newUpdateTime;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user