Keeps heads for 3 minutes in cache

It appears that heads are being "forgotten" by the server after a few
minutes (not sure how long) so caching forever doesn't help. This causes
blocking calls when the head is set.

https://github.com/BentoBoxWorld/Level/issues/159
This commit is contained in:
tastybento 2020-06-26 07:47:19 -07:00
parent 0e3535d0cc
commit 4e56ff8c92
4 changed files with 37 additions and 5 deletions

View File

@ -2,7 +2,6 @@ package world.bentobox.bentobox.api.commands.island;
import java.util.*;
import org.bukkit.Bukkit;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.panels.LanguagePanel;

View File

@ -5,7 +5,6 @@ import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.event.inventory.ClickType;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.BentoBox;

View File

@ -0,0 +1,31 @@
package world.bentobox.bentobox.util.heads;
import org.bukkit.inventory.ItemStack;
public class HeadCache {
private final ItemStack head;
private final long timestamp;
/**
* @param head
* @param timestamp
*/
public HeadCache(ItemStack head, long timestamp) {
super();
this.head = head;
this.timestamp = timestamp;
}
/**
* @return the head
*/
public ItemStack getHead() {
return head;
}
/**
* @return the timestamp
*/
public long getTimestamp() {
return timestamp;
}
}

View File

@ -16,8 +16,9 @@ import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.panels.PanelItem;
public class HeadGetter {
private static Map<String,ItemStack> cachedHeads = new HashMap<>();
private static Map<String,HeadCache> cachedHeads = new HashMap<>();
private static final Map<String, PanelItem> names = new HashMap<>();
private static final long TOO_LONG = 360000; // 3 minutes
private BentoBox plugin;
private static Map<String,Set<HeadRequester>> headRequesters = new HashMap<>();
@ -42,7 +43,7 @@ public class HeadGetter {
meta.setOwner(en.getKey());
playerSkull.setItemMeta(meta);
// Save in cache
cachedHeads.put(en.getKey(), playerSkull);
cachedHeads.put(en.getKey(), new HeadCache(playerSkull, System.currentTimeMillis()));
// Tell requesters the head came in
if (headRequesters.containsKey(en.getKey())) {
for (HeadRequester req : headRequesters.get(en.getKey())) {
@ -61,9 +62,11 @@ public class HeadGetter {
* @param requester - callback class
*/
public static void getHead(PanelItem panelItem, HeadRequester requester) {
// Freshen cache
cachedHeads.values().removeIf(c -> System.currentTimeMillis() - c.getTimestamp() > TOO_LONG);
// Check if in cache
if (cachedHeads.containsKey(panelItem.getPlayerHeadName())) {
panelItem.setHead(cachedHeads.get(panelItem.getPlayerHeadName()).clone());
panelItem.setHead(cachedHeads.get(panelItem.getPlayerHeadName()).getHead().clone());
requester.setHead(panelItem);
} else {
// Get the name