mirror of
https://github.com/MineLittlePony/MineLittlePony.git
synced 2025-02-16 17:44:23 +01:00
Occassionally cull the ponies cache of unused values to prevent a gradual increase in memory usage
This commit is contained in:
parent
ab881956a7
commit
3adfef141c
2 changed files with 17 additions and 2 deletions
|
@ -42,11 +42,22 @@ public class Pony implements IPony {
|
||||||
private final ResourceLocation texture;
|
private final ResourceLocation texture;
|
||||||
private final IPonyData metadata;
|
private final IPonyData metadata;
|
||||||
|
|
||||||
|
private long expirationPeriod;
|
||||||
|
|
||||||
public Pony(ResourceLocation resource) {
|
public Pony(ResourceLocation resource) {
|
||||||
texture = resource;
|
texture = resource;
|
||||||
metadata = checkSkin(texture);
|
metadata = checkSkin(texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean hasExpired() {
|
||||||
|
return expirationPeriod <= System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
Pony touch() {
|
||||||
|
expirationPeriod = System.currentTimeMillis() + 30000;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
private IPonyData checkSkin(ResourceLocation resource) {
|
private IPonyData checkSkin(ResourceLocation resource) {
|
||||||
IPonyData data = checkPonyMeta(resource);
|
IPonyData data = checkPonyMeta(resource);
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
|
|
|
@ -49,7 +49,7 @@ public class PonyManager implements IResourceManagerReloadListener, ISkinCacheCl
|
||||||
|
|
||||||
private PonyConfig config;
|
private PonyConfig config;
|
||||||
|
|
||||||
private Map<ResourceLocation, IPony> poniesCache = Maps.newHashMap();
|
private final Map<ResourceLocation, Pony> poniesCache = Maps.newHashMap();
|
||||||
|
|
||||||
public PonyManager(PonyConfig config) {
|
public PonyManager(PonyConfig config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
@ -61,7 +61,11 @@ public class PonyManager implements IResourceManagerReloadListener, ISkinCacheCl
|
||||||
* @param resource A texture resource
|
* @param resource A texture resource
|
||||||
*/
|
*/
|
||||||
public IPony getPony(ResourceLocation 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue