CreateMod/src/main/java/com/simibubi/create/foundation/render/SuperByteBufferCache.java
PepperCode1 246543c76b Fix memory leaks
- Fix CopycatPanelModel using wrong state during trapdoor special case
- Update Flywheel
2023-07-03 13:53:51 -07:00

57 lines
1.6 KiB
Java

package com.simibubi.create.foundation.render;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
public class SuperByteBufferCache {
protected final Map<Compartment<?>, Cache<Object, SuperByteBuffer>> caches = new HashMap<>();
public synchronized void registerCompartment(Compartment<?> compartment) {
caches.put(compartment, CacheBuilder.newBuilder()
.<Object, SuperByteBuffer>removalListener(n -> n.getValue().delete())
.build());
}
public synchronized void registerCompartment(Compartment<?> compartment, long ticksUntilExpired) {
caches.put(compartment, CacheBuilder.newBuilder()
.expireAfterAccess(ticksUntilExpired * 50, TimeUnit.MILLISECONDS)
.<Object, SuperByteBuffer>removalListener(n -> n.getValue().delete())
.build());
}
public <T> SuperByteBuffer get(Compartment<T> compartment, T key, Callable<SuperByteBuffer> callable) {
Cache<Object, SuperByteBuffer> cache = caches.get(compartment);
if (cache != null) {
try {
return cache.get(key, callable);
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return null;
}
public <T> void invalidate(Compartment<T> compartment, T key) {
caches.get(compartment).invalidate(key);
}
public <T> void invalidate(Compartment<?> compartment) {
caches.get(compartment).invalidateAll();
}
public void invalidate() {
caches.forEach((compartment, cache) -> cache.invalidateAll());
}
public static class Compartment<T> {
}
}