Further caching improvements

This commit is contained in:
William committed 2023-03-21 20:20:08 +00:00
1 parent a959aa9519
commit 8923b44c92
12 files changed
+212 -46

No files matched your search

@@ -2,6 +2,8 @@ package net.william278.huskhomes.manager;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.network.Message;
import net.william278.huskhomes.network.Payload;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.PositionMeta;
@@ -12,17 +14,19 @@ import net.william278.huskhomes.util.ValidationException;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
public class HomesManager {
private final HuskHomes plugin;
private final List<Home> publicHomes;
private final Map<String, List<Home>> userHomes;
private final ConcurrentLinkedQueue<Home> publicHomes;
private final ConcurrentHashMap<String, ConcurrentLinkedQueue<Home>> userHomes;
protected HomesManager(@NotNull HuskHomes plugin) {
this.plugin = plugin;
this.publicHomes = plugin.getDatabase().getPublicHomes();
this.userHomes = new HashMap<>();
this.publicHomes = new ConcurrentLinkedQueue<>(plugin.getDatabase().getPublicHomes());
this.userHomes = new ConcurrentHashMap<>();
plugin.runAsync(() -> plugin.getOnlineUsers()
.forEach(this::cacheUserHomes));
}
@@ -38,7 +42,56 @@ public class HomesManager {
}
public void cacheUserHomes(@NotNull User user) {
userHomes.put(user.getUuid().toString(), plugin.getDatabase().getHomes(user));
userHomes.put(user.getUsername(), new ConcurrentLinkedQueue<>(plugin.getDatabase().getHomes(user)));
}
public void cacheHome(@NotNull Home home, boolean propagate) {
userHomes.computeIfPresent(home.getOwner().getUsername(), (k, v) -> {
v.remove(home);
v.add(home);
return v;
});
if (publicHomes.remove(home) && !home.isPublic()) {
plugin.getMapHook().ifPresent(hook -> hook.removeHome(home));
}
if (home.isPublic()) {
publicHomes.add(home);
plugin.getMapHook().ifPresent(hook -> hook.updateHome(home));
}
if (propagate) {
propagateCacheUpdate(home.getUuid());
}
}
public void unCacheHome(@NotNull UUID homeId, boolean propagate) {
userHomes.values().forEach(homes -> homes.removeIf(home -> home.getUuid().equals(homeId)));
publicHomes.removeIf(home -> {
if (home.getUuid().equals(homeId)) {
plugin.getMapHook().ifPresent(hook -> hook.removeHome(home));
return true;
}
return false;
});
if (propagate) {
this.propagateCacheUpdate(homeId);
}
}
private void propagateCacheUpdate(@NotNull UUID homeId) {
if (plugin.getSettings().isCrossServer()) {
plugin.getOnlineUsers().stream().findAny().ifPresent(user -> Message.builder()
.type(Message.Type.UPDATE_HOME)
.scope(Message.Scope.SERVER)
.target(Message.TARGET_ALL)
.payload(Payload.withString(homeId.toString()))
.build().send(plugin.getMessenger(), user));
}
}
public void updatePublicHomeCache() {
plugin.getDatabase().getPublicHomes().forEach(home -> cacheHome(home, false));
}
public void removeUserHomes(@NotNull User user) {
@@ -99,6 +152,7 @@ public class HomesManager {
})
.orElse(new Home(position, new PositionMeta(name, ""), owner));
plugin.getDatabase().saveHome(home);
this.cacheHome(home, true);
}
public void createHome(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) throws ValidationException {
@@ -116,10 +170,19 @@ public class HomesManager {
public void deleteHome(@NotNull Home home) {
plugin.getDatabase().deleteHome(home.getUuid());
this.unCacheHome(home.getUuid(), true);
}
public int deleteAllHomes(@NotNull User owner) {
return plugin.getDatabase().deleteAllHomes(owner);
final int deleted = plugin.getDatabase().deleteAllHomes(owner);
userHomes.computeIfPresent(owner.getUsername(), (k, v) -> {
v.clear();
return v;
});
publicHomes.removeIf(h -> h.getOwner().getUuid().equals(owner.getUuid()));
plugin.getMapHook().ifPresent(hook -> hook.clearHomes(owner));
plugin.getManager().propagateCacheUpdate();
return deleted;
}
public void setHomePosition(@NotNull User owner, @NotNull String name, @NotNull Position position) throws ValidationException {
@@ -134,6 +197,7 @@ public class HomesManager {
public void setHomePosition(@NotNull Home home, @NotNull Position position) throws ValidationException {
home.update(position);
plugin.getDatabase().saveHome(home);
this.cacheHome(home, true);
}
public void setHomeName(@NotNull User owner, @NotNull String name, @NotNull String newName) throws ValidationException {
@@ -152,6 +216,7 @@ public class HomesManager {
home.getMeta().setName(newName);
plugin.getDatabase().saveHome(home);
this.cacheHome(home, true);
}
public void setHomeDescription(@NotNull User owner, @NotNull String name, @NotNull String description) throws ValidationException {
@@ -170,6 +235,7 @@ public class HomesManager {
home.getMeta().setDescription(description);
plugin.getDatabase().saveHome(home);
this.cacheHome(home, true);
}
public void setHomePrivacy(@NotNull User owner, @NotNull String name, boolean isPublic) throws ValidationException {
@@ -199,6 +265,7 @@ public class HomesManager {
home.setPublic(isPublic);
plugin.getDatabase().saveHome(home);
this.cacheHome(home, true);
}
}
@@ -1,15 +1,18 @@
package net.william278.huskhomes.manager;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.network.Message;
import org.jetbrains.annotations.NotNull;
public class Manager {
private final HuskHomes plugin;
private final HomesManager homes;
private final WarpsManager warps;
private final RequestsManager requests;
public Manager(@NotNull HuskHomes plugin) {
this.plugin = plugin;
this.homes = new HomesManager(plugin);
this.warps = new WarpsManager(plugin);
this.requests = new RequestsManager(plugin);
@@ -29,4 +32,13 @@ public class Manager {
public RequestsManager requests() {
return requests;
}
// Update caches on all servers
protected void propagateCacheUpdate() {
plugin.getOnlineUsers().stream().findAny().ifPresent(user -> Message.builder()
.type(Message.Type.UPDATE_CACHES)
.scope(Message.Scope.SERVER)
.target(Message.TARGET_ALL)
.build().send(plugin.getMessenger(), user));
}
}
@@ -1,6 +1,9 @@
package net.william278.huskhomes.manager;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.hook.MapHook;
import net.william278.huskhomes.network.Message;
import net.william278.huskhomes.network.Payload;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.PositionMeta;
import net.william278.huskhomes.position.Warp;
@@ -9,14 +12,54 @@ import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
public class WarpsManager {
private final HuskHomes plugin;
private final List<Warp> warps;
private final ConcurrentLinkedQueue<Warp> warps;
protected WarpsManager(@NotNull HuskHomes plugin) {
this.plugin = plugin;
this.warps = plugin.getDatabase().getWarps();
this.warps = new ConcurrentLinkedQueue<>(plugin.getDatabase().getWarps());
}
public void cacheWarp(@NotNull Warp warp, boolean propagate) {
warps.remove(warp);
warps.add(warp);
plugin.getMapHook().ifPresent(hook -> hook.updateWarp(warp));
if (propagate) {
this.propagateCacheUpdate(warp.getUuid());
}
}
public void unCacheWarp(@NotNull UUID warpId, boolean propagate) {
warps.removeIf(warp -> {
if (warp.getUuid().equals(warpId)) {
plugin.getMapHook().ifPresent(hook -> hook.removeWarp(warp));
return true;
}
return false;
});
if (propagate) {
this.propagateCacheUpdate(warpId);
}
}
private void propagateCacheUpdate(@NotNull UUID warpId) {
if (plugin.getSettings().isCrossServer()) {
plugin.getOnlineUsers().stream().findAny().ifPresent(user -> Message.builder()
.type(Message.Type.UPDATE_WARP)
.scope(Message.Scope.SERVER)
.target(Message.TARGET_ALL)
.payload(Payload.withString(warpId.toString()))
.build().send(plugin.getMessenger(), user));
}
}
public void updateWarpCache() {
plugin.getDatabase().getWarps().forEach(warp -> cacheWarp(warp, false));
}
/**
@@ -50,6 +93,7 @@ public class WarpsManager {
})
.orElse(new Warp(position, new PositionMeta(name, "")));
plugin.getDatabase().saveWarp(warp);
this.cacheWarp(warp, true);
}
public void createWarp(@NotNull String name, @NotNull Position position) throws ValidationException {
@@ -67,10 +111,15 @@ public class WarpsManager {
public void deleteWarp(@NotNull Warp warp) {
plugin.getDatabase().deleteWarp(warp.getUuid());
this.unCacheWarp(warp.getUuid(), true);
}
public int deleteAllWarps() {
return plugin.getDatabase().deleteAllWarps();
final int deleted = plugin.getDatabase().deleteAllWarps();
warps.clear();
plugin.getMapHook().ifPresent(MapHook::clearWarps);
plugin.getManager().propagateCacheUpdate();
return deleted;
}
public void relocateWarp(@NotNull String name, @NotNull Position position) throws ValidationException {
@@ -85,6 +134,7 @@ public class WarpsManager {
public void relocateWarp(@NotNull Warp warp, @NotNull Position position) {
warp.update(position);
plugin.getDatabase().saveWarp(warp);
this.cacheWarp(warp, true);
}
public void renameWarp(@NotNull String name, @NotNull String newName) throws ValidationException {
@@ -103,6 +153,7 @@ public class WarpsManager {
warp.getMeta().setName(newName);
plugin.getDatabase().saveWarp(warp);
this.cacheWarp(warp, true);
}
public void setWarpDescription(@NotNull String name, @NotNull String description) throws ValidationException {
@@ -121,6 +172,7 @@ public class WarpsManager {
warp.getMeta().setDescription(description);
plugin.getDatabase().saveWarp(warp);
this.cacheWarp(warp, true);
}
}