aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorSHsuperCM <shsupercm@gmail.com>2022-02-13 12:49:48 +0200
committerSHsuperCM <shsupercm@gmail.com>2022-02-13 12:49:48 +0200
commitcf735c9f3205208801a9675e6b6217dc84b1ea3c (patch)
treedc2ac518c145668343593952d6256327a35c7e87 /src/main/java
parentfe02178b220ebf57225bd092bfdb0d8b88067c33 (diff)
downloadCITResewn-cf735c9f3205208801a9675e6b6217dc84b1ea3c.tar.gz
CITResewn-cf735c9f3205208801a9675e6b6217dc84b1ea3c.tar.bz2
CITResewn-cf735c9f3205208801a9675e6b6217dc84b1ea3c.zip
Implemented caching
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITCache.java50
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;
+ }
+ }
+}