diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java new file mode 100644 index 0000000..5fc6f5a --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java @@ -0,0 +1,50 @@ +package shcm.shsupercm.fabric.citresewn.cit; + +import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + +public abstract class CITCache<T extends CITType> { + public long lastCachedStamp = 0; + + public static class Single<T extends CITType> extends CITCache<T> { + public WeakReference<CIT<T>> cit = null; + public final Function<CITContext, CIT<T>> realtime; + + public Single(Function<CITContext, CIT<T>> realtime) { + this.realtime = realtime; + } + + public WeakReference<CIT<T>> get(CITContext context) { + if (this.cit == null || System.currentTimeMillis() - this.lastCachedStamp >= CITResewnConfig.INSTANCE.cache_ms) { + this.cit = new WeakReference<>(this.realtime.apply(context)); + this.lastCachedStamp = System.currentTimeMillis(); + } + + return this.cit; + } + } + + public static class MultiList<T extends CITType> extends CITCache<T> { + public List<WeakReference<CIT<T>>> cit = null; + public final Function<CITContext, List<CIT<T>>> realtime; + + public MultiList(Function<CITContext, List<CIT<T>>> realtime) { + this.realtime = realtime; + } + + public List<WeakReference<CIT<T>>> get(CITContext context) { + if (this.cit == null || System.currentTimeMillis() - this.lastCachedStamp >= CITResewnConfig.INSTANCE.cache_ms) { + this.cit = new ArrayList<>(); + for (CIT<T> realtimeCIT : this.realtime.apply(context)) + this.cit.add(new WeakReference<>(realtimeCIT)); + this.lastCachedStamp = System.currentTimeMillis(); + } + + return cit; + } + } +} |