Better caching

This commit is contained in:
DanLT 2021-06-22 09:35:28 -08:00
parent b59278601c
commit a45d311339
2 changed files with 19 additions and 13 deletions

View File

@ -52,22 +52,18 @@ shadowJar
{
minimize()
dependencies {
include(dependency('com.github.ben-manes.caffeine:caffeine:2.8.5'))
include(dependency('org.zeroturnaround:zt-zip:1.14'))
include(dependency('io.papermc:paperlib:1.0.5'))
include(dependency('com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2'))
}
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
// Shade
implementation 'com.github.ben-manes.caffeine:caffeine:3.0.2'
implementation 'org.zeroturnaround:zt-zip:1.14'
implementation 'io.papermc:paperlib:1.0.5'
// Provided
implementation 'com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2'
implementation 'org.spigotmc:spigot-api:1.17-R0.1-SNAPSHOT'
implementation 'org.bukkit.craftbukkit:1.17:1.17'
implementation 'com.bergerkiller.bukkit:BKCommonLib:1.16.4-v2'

View File

@ -1,23 +1,28 @@
package com.volmit.iris.scaffold.stream.utility;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import com.volmit.iris.Iris;
import com.volmit.iris.scaffold.cache.Cache;
import com.volmit.iris.scaffold.stream.BasicStream;
import com.volmit.iris.scaffold.stream.ProceduralStream;
import com.volmit.iris.util.ChronoLatch;
import com.volmit.iris.util.Form;
public class CachedStream2D<T> extends BasicStream<T> implements ProceduralStream<T>
{
private final ProceduralStream<T> stream;
private final LoadingCache<Long, T> cache;
private final ConcurrentLinkedHashMap<Long, T> cache;
private ChronoLatch cl = new ChronoLatch(1000);
public CachedStream2D(ProceduralStream<T> stream, int size)
{
super();
this.stream = stream;
cache = Caffeine.newBuilder()
.maximumSize(size)
.build((b) -> stream.get(Cache.keyX(b), Cache.keyZ(b)));
cache = new ConcurrentLinkedHashMap.Builder<Long, T>()
.initialCapacity(size)
.maximumWeightedCapacity(size)
.concurrencyLevel(32)
.build();
}
@Override
@ -35,7 +40,12 @@ public class CachedStream2D<T> extends BasicStream<T> implements ProceduralStrea
@Override
public T get(double x, double z)
{
return cache.get(Cache.key((int) x, (int) z));
if(cl.flip())
{
Iris.info("Cache: " + Form.f(cache.size()) + " / " + Form.f(cache.weightedSize()));
}
long ck = Cache.key((int) x, (int) z);
return cache.compute(ck, (k, v) -> v != null ? v : stream.get(Cache.keyX(ck), Cache.keyZ(ck)));
}
@Override