Occassionally cull the ponies cache of unused values to prevent a gradual increase in memory usage

This commit is contained in:
Sollace 2018-12-09 23:29:14 +02:00
parent ab881956a7
commit 3adfef141c
2 changed files with 17 additions and 2 deletions

View file

@ -42,11 +42,22 @@ public class Pony implements IPony {
private final ResourceLocation texture;
private final IPonyData metadata;
private long expirationPeriod;
public Pony(ResourceLocation resource) {
texture = resource;
metadata = checkSkin(texture);
}
boolean hasExpired() {
return expirationPeriod <= System.currentTimeMillis();
}
Pony touch() {
expirationPeriod = System.currentTimeMillis() + 30000;
return this;
}
private IPonyData checkSkin(ResourceLocation resource) {
IPonyData data = checkPonyMeta(resource);
if (data != null) {

View file

@ -49,7 +49,7 @@ public class PonyManager implements IResourceManagerReloadListener, ISkinCacheCl
private PonyConfig config;
private Map<ResourceLocation, IPony> poniesCache = Maps.newHashMap();
private final Map<ResourceLocation, Pony> poniesCache = Maps.newHashMap();
public PonyManager(PonyConfig config) {
this.config = config;
@ -61,7 +61,11 @@ public class PonyManager implements IResourceManagerReloadListener, ISkinCacheCl
* @param resource A texture resource
*/
public IPony getPony(ResourceLocation resource) {
return poniesCache.computeIfAbsent(resource, Pony::new);
IPony result = poniesCache.computeIfAbsent(resource, Pony::new).touch();
poniesCache.entrySet().removeIf(entry -> entry.getValue().hasExpired());
return result;
}
/**