Tweak caching for private home list viewing, fix #222

This commit is contained in:
William committed 2022-09-19 11:14:01 +01:00
1 parent 39931dba04
commit c56936d9d5
3 files changed
+11 -11

No files matched your search

@@ -43,9 +43,9 @@ public class Cache {
public final List<String> players; public final List<String> players;
/** /**
* Cached lists of private homes for pagination * Cached lists of private homes for pagination, mapped to the username of the home owner
*/ */
public final HashMap<UUID, PaginatedList> privateHomeLists; public final HashMap<String, PaginatedList> privateHomeLists;
/** /**
* Cached lists of public homes for pagination * Cached lists of public homes for pagination
@@ -140,7 +140,7 @@ public class Cache {
} }
@NotNull @NotNull
public Optional<MineDown> getHomeList(@NotNull OnlineUser onlineUser, @NotNull User listOwner, @NotNull Locales locales, public Optional<MineDown> getHomeList(@NotNull OnlineUser onlineUser, @NotNull User homeOwner, @NotNull Locales locales,
@NotNull List<Home> homes, final int itemsPerPage, final int page) { @NotNull List<Home> homes, final int itemsPerPage, final int page) {
if (eventDispatcher.dispatchViewHomeListEvent(homes, onlineUser, false).join().isCancelled()) { if (eventDispatcher.dispatchViewHomeListEvent(homes, onlineUser, false).join().isCancelled()) {
return Optional.empty(); return Optional.empty();
@@ -152,10 +152,10 @@ public class Cache {
Locales.escapeMineDown(locales.formatDescription(home.meta.description))) Locales.escapeMineDown(locales.formatDescription(home.meta.description)))
.orElse(home.meta.name)).sorted().collect(Collectors.toList()), getBaseList(locales, itemsPerPage) .orElse(home.meta.name)).sorted().collect(Collectors.toList()), getBaseList(locales, itemsPerPage)
.setHeaderFormat(locales.getRawLocale("home_list_page_title", .setHeaderFormat(locales.getRawLocale("home_list_page_title",
listOwner.username, "%first_item_on_page_index%", homeOwner.username, "%first_item_on_page_index%",
"%last_item_on_page_index%", "%total_items%").orElse("")) "%last_item_on_page_index%", "%total_items%").orElse(""))
.setCommand("/huskhomes:homelist").build()); .setCommand("/huskhomes:homelist").build());
this.privateHomeLists.put(onlineUser.uuid, homeList); this.privateHomeLists.put(homeOwner.username, homeList);
return Optional.of(homeList.getNearestValidPage(page)); return Optional.of(homeList.getNearestValidPage(page));
} }
@@ -53,8 +53,8 @@ public class HomeListCommand extends CommandBase implements ConsoleExecutable {
* @param pageNumber page number to display * @param pageNumber page number to display
*/ */
private void showHomeList(@NotNull OnlineUser onlineUser, @NotNull String homeOwner, int pageNumber) { private void showHomeList(@NotNull OnlineUser onlineUser, @NotNull String homeOwner, int pageNumber) {
if (plugin.getCache().privateHomeLists.containsKey(onlineUser.uuid)) { if (plugin.getCache().privateHomeLists.containsKey(homeOwner)) {
onlineUser.sendMessage(plugin.getCache().privateHomeLists.get(onlineUser.uuid).getNearestValidPage(pageNumber)); onlineUser.sendMessage(plugin.getCache().privateHomeLists.get(homeOwner).getNearestValidPage(pageNumber));
return; return;
} }
plugin.getDatabase().getUserDataByName(homeOwner).thenAccept(optionalUser -> optionalUser.ifPresentOrElse(userData -> { plugin.getDatabase().getUserDataByName(homeOwner).thenAccept(optionalUser -> optionalUser.ifPresentOrElse(userData -> {
@@ -88,7 +88,7 @@ public class SavedPositionManager {
} }
cache.homes.putIfAbsent(home.owner.uuid, new ArrayList<>()); cache.homes.putIfAbsent(home.owner.uuid, new ArrayList<>());
cache.homes.get(home.owner.uuid).add(home.meta.name); cache.homes.get(home.owner.uuid).add(home.meta.name);
cache.privateHomeLists.remove(home.owner.uuid); cache.privateHomeLists.remove(home.owner.username);
return database.saveHome(home).thenApply(value -> return database.saveHome(home).thenApply(value ->
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join(); new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join();
}).join(); }).join();
@@ -118,7 +118,7 @@ public class SavedPositionManager {
homeNames.remove(home.meta.name); homeNames.remove(home.meta.name);
return homeNames; return homeNames;
}); });
cache.privateHomeLists.remove(home.owner.uuid); cache.privateHomeLists.remove(home.owner.username);
cache.publicHomeLists.clear(); cache.publicHomeLists.clear();
if (mapHook != null) { if (mapHook != null) {
mapHook.removeHome(home); mapHook.removeHome(home);
@@ -155,7 +155,7 @@ public class SavedPositionManager {
cache.homes.get(home.owner.uuid).remove(existingHomeName); cache.homes.get(home.owner.uuid).remove(existingHomeName);
cache.homes.get(home.owner.uuid).add(newHomeMeta.name); cache.homes.get(home.owner.uuid).add(newHomeMeta.name);
} }
cache.privateHomeLists.remove(home.owner.uuid); cache.privateHomeLists.remove(home.owner.username);
cache.publicHomeLists.clear(); cache.publicHomeLists.clear();
if (home.isPublic && mapHook != null) { if (home.isPublic && mapHook != null) {
mapHook.updateHome(home); mapHook.updateHome(home);
@@ -217,7 +217,7 @@ public class SavedPositionManager {
mapHook.removeHome(home); mapHook.removeHome(home);
} }
} }
cache.privateHomeLists.remove(home.owner.uuid); cache.privateHomeLists.remove(home.owner.username);
cache.publicHomeLists.clear(); cache.publicHomeLists.clear();
return true; return true;
}).join(); }).join();