From cf735c9f3205208801a9675e6b6217dc84b1ea3c Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sun, 13 Feb 2022 12:49:48 +0200 Subject: Implemented caching --- .../shsupercm/fabric/citresewn/cit/CITCache.java | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java (limited to 'src/main/java') 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 { + public long lastCachedStamp = 0; + + public static class Single extends CITCache { + public WeakReference> cit = null; + public final Function> realtime; + + public Single(Function> realtime) { + this.realtime = realtime; + } + + public WeakReference> 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 extends CITCache { + public List>> cit = null; + public final Function>> realtime; + + public MultiList(Function>> realtime) { + this.realtime = realtime; + } + + public List>> get(CITContext context) { + if (this.cit == null || System.currentTimeMillis() - this.lastCachedStamp >= CITResewnConfig.INSTANCE.cache_ms) { + this.cit = new ArrayList<>(); + for (CIT realtimeCIT : this.realtime.apply(context)) + this.cit.add(new WeakReference<>(realtimeCIT)); + this.lastCachedStamp = System.currentTimeMillis(); + } + + return cit; + } + } +} -- cgit