Replace global connected plots cache with local cache

This commit is contained in:
SirYwell 2022-12-21 15:20:15 +01:00
parent 0661d0d5a1
commit 25defaac07
No known key found for this signature in database
2 changed files with 15 additions and 22 deletions

View File

@ -125,9 +125,6 @@ public class Plot {
private static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build(); private static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build();
private static final Cleaner CLEANER = Cleaner.create(); private static final Cleaner CLEANER = Cleaner.create();
static Set<Plot> connected_cache;
static Set<CuboidRegion> regions_cache;
static { static {
FLAG_DECIMAL_FORMAT.setMaximumFractionDigits(340); FLAG_DECIMAL_FORMAT.setMaximumFractionDigits(340);
} }
@ -210,6 +207,8 @@ public class Plot {
*/ */
private Plot origin; private Plot origin;
private Set<Plot> connectedCache;
/** /**
* Constructor for a new plot. * Constructor for a new plot.
* (Only changes after plot.create() will be properly set in the database) * (Only changes after plot.create() will be properly set in the database)
@ -2131,17 +2130,16 @@ public class Plot {
this.origin.origin = base; this.origin.origin = base;
other.origin = base; other.origin = base;
this.origin = base; this.origin = base;
connected_cache = null; this.connectedCache = null;
} }
} else { } else {
if (this.origin != null) { if (this.origin != null) {
this.origin.origin = null; this.origin.origin = null;
this.origin = null; this.origin = null;
} }
connected_cache = null; this.connectedCache = null;
} }
DBFunc.setMerged(this, this.getSettings().getMerged()); DBFunc.setMerged(this, this.getSettings().getMerged());
regions_cache = null;
} }
} }
@ -2176,8 +2174,7 @@ public class Plot {
} }
public void clearCache() { public void clearCache() {
connected_cache = null; this.connectedCache = null;
regions_cache = null;
if (this.origin != null) { if (this.origin != null) {
this.origin.origin = null; this.origin.origin = null;
this.origin = null; this.origin = null;
@ -2327,12 +2324,11 @@ public class Plot {
event.commit(); event.commit();
return Collections.singleton(this); return Collections.singleton(this);
} }
if (connected_cache != null && connected_cache.contains(this)) { if (this.connectedCache != null && this.connectedCache.contains(this)) {
event.mergedSize = connected_cache.size(); event.mergedSize = this.connectedCache.size();
event.commit(); event.commit();
return connected_cache; return this.connectedCache;
} }
regions_cache = null;
HashSet<Plot> tmpSet = new HashSet<>(); HashSet<Plot> tmpSet = new HashSet<>();
tmpSet.add(this); tmpSet.add(this);
@ -2437,7 +2433,7 @@ public class Plot {
} }
} }
} }
connected_cache = tmpSet; this.connectedCache = tmpSet;
event.mergedSize = tmpSet.size(); event.mergedSize = tmpSet.size();
event.cacheMiss = true; event.cacheMiss = true;
event.commit(); event.commit();
@ -2452,19 +2448,15 @@ public class Plot {
* @return all regions within the plot * @return all regions within the plot
*/ */
public @NonNull Set<CuboidRegion> getRegions() { public @NonNull Set<CuboidRegion> getRegions() {
if (regions_cache != null && connected_cache != null && connected_cache.contains(this)) {
return regions_cache;
}
if (!this.isMerged()) { if (!this.isMerged()) {
Location pos1 = this.getBottomAbs().withY(getArea().getMinBuildHeight()); Location pos1 = this.getBottomAbs().withY(getArea().getMinBuildHeight());
Location pos2 = this.getTopAbs().withY(getArea().getMaxBuildHeight()); Location pos2 = this.getTopAbs().withY(getArea().getMaxBuildHeight());
connected_cache = Sets.newHashSet(this); this.connectedCache = Sets.newHashSet(this);
CuboidRegion rg = new CuboidRegion(pos1.getBlockVector3(), pos2.getBlockVector3()); CuboidRegion rg = new CuboidRegion(pos1.getBlockVector3(), pos2.getBlockVector3());
regions_cache = Collections.singleton(rg); return Collections.singleton(rg);
return regions_cache;
} }
Set<Plot> plots = this.getConnectedPlots(); Set<Plot> plots = this.getConnectedPlots();
Set<CuboidRegion> regions = regions_cache = new HashSet<>(); Set<CuboidRegion> regions = new HashSet<>();
Set<PlotId> visited = new HashSet<>(); Set<PlotId> visited = new HashSet<>();
for (Plot current : plots) { for (Plot current : plots) {
if (visited.contains(current.getId())) { if (visited.contains(current.getId())) {

View File

@ -327,6 +327,7 @@ public final class PlotModificationManager {
ArrayList<PlotId> ids = new ArrayList<>(plots.size()); ArrayList<PlotId> ids = new ArrayList<>(plots.size());
for (Plot current : plots) { for (Plot current : plots) {
current.setHome(null); current.setHome(null);
current.clearCache();
ids.add(current.getId()); ids.add(current.getId());
} }
this.plot.clearRatings(); this.plot.clearRatings();
@ -478,8 +479,7 @@ public final class PlotModificationManager {
this.plot.updateWorldBorder(); this.plot.updateWorldBorder();
} }
} }
Plot.connected_cache = null; this.plot.clearCache();
Plot.regions_cache = null;
this.plot.getTrusted().clear(); this.plot.getTrusted().clear();
this.plot.getMembers().clear(); this.plot.getMembers().clear();
this.plot.getDenied().clear(); this.plot.getDenied().clear();
@ -630,6 +630,7 @@ public final class PlotModificationManager {
if (queue.size() > 0) { if (queue.size() > 0) {
queue.enqueue(); queue.enqueue();
} }
visited.forEach(Plot::clearCache);
return toReturn; return toReturn;
} }