mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2025-12-27 11:09:06 +00:00
f
This commit is contained in:
82
src/main/java/com/volmit/iris/util/data/KCache.java
Normal file
82
src/main/java/com/volmit/iris/util/data/KCache.java
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.data;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.CacheLoader;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.github.benmanes.caffeine.cache.LoadingCache;
|
||||
import com.volmit.iris.engine.framework.MeteredCache;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class KCache<K,V> implements MeteredCache {
|
||||
private long max;
|
||||
private CacheLoader<? super K, ? extends V> loader;
|
||||
private LoadingCache<K, V> cache;
|
||||
|
||||
public KCache(CacheLoader<K, V> loader, long max)
|
||||
{
|
||||
this.max = max;
|
||||
this.loader = loader;
|
||||
this.cache = Caffeine
|
||||
.newBuilder()
|
||||
.maximumSize(max)
|
||||
.build((k) -> loader == null ? null : loader.load(k));
|
||||
}
|
||||
|
||||
public void setLoader(CacheLoader<? super K, ? extends V> loader)
|
||||
{
|
||||
this.loader = loader;
|
||||
}
|
||||
|
||||
public void invalidate(K k)
|
||||
{
|
||||
cache.invalidate(k);
|
||||
}
|
||||
|
||||
public void invalidate()
|
||||
{
|
||||
cache.invalidateAll();
|
||||
cache.cleanUp();
|
||||
}
|
||||
|
||||
public V get(K k)
|
||||
{
|
||||
return cache.get(k);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSize() {
|
||||
return cache.estimatedSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaxSize() {
|
||||
return max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClosed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(K next) {
|
||||
return cache.getIfPresent(next) != null;
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ package com.volmit.iris.util.stream;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.loader.IrisData;
|
||||
import com.volmit.iris.engine.framework.Engine;
|
||||
import com.volmit.iris.engine.object.IRare;
|
||||
import com.volmit.iris.engine.object.IrisStyledRange;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
@@ -287,8 +288,12 @@ public interface ProceduralStream<T> extends ProceduralLayer, Interpolated<T> {
|
||||
return new To3DStream<T>(this);
|
||||
}
|
||||
|
||||
default ProceduralStream<T> cache2D(int maxSize) {
|
||||
return new CachedStream2D<T>(this, maxSize);
|
||||
default ProceduralStream<T> cache2D(Engine engine, int maxSize) {
|
||||
return cache2D(engine, maxSize, false);
|
||||
}
|
||||
|
||||
default ProceduralStream<T> cache2D(Engine engine, int maxSize, boolean weak) {
|
||||
return new CachedStream2D<T>(engine, this, maxSize, weak);
|
||||
}
|
||||
|
||||
default ProceduralStream<T> cache3D(int maxSize) {
|
||||
|
||||
@@ -18,23 +18,30 @@
|
||||
|
||||
package com.volmit.iris.util.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.core.IrisSettings;
|
||||
import com.volmit.iris.core.service.PreservationSVC;
|
||||
import com.volmit.iris.engine.data.cache.Cache;
|
||||
import com.volmit.iris.engine.framework.Engine;
|
||||
import com.volmit.iris.engine.framework.MeteredCache;
|
||||
import com.volmit.iris.util.data.KCache;
|
||||
import com.volmit.iris.util.stream.BasicStream;
|
||||
import com.volmit.iris.util.stream.ProceduralStream;
|
||||
|
||||
public class CachedStream2D<T> extends BasicStream<T> implements ProceduralStream<T> {
|
||||
public class CachedStream2D<T> extends BasicStream<T> implements ProceduralStream<T>, MeteredCache {
|
||||
private final ProceduralStream<T> stream;
|
||||
private final ConcurrentLinkedHashMap<Long, T> cache;
|
||||
private final KCache<Long, T> cache;
|
||||
private final Engine engine;
|
||||
|
||||
public CachedStream2D(ProceduralStream<T> stream, int size) {
|
||||
public CachedStream2D(Engine engine, ProceduralStream<T> stream, int size, boolean weak) {
|
||||
super();
|
||||
this.stream = stream;
|
||||
cache = new ConcurrentLinkedHashMap.Builder<Long, T>()
|
||||
.initialCapacity(size)
|
||||
.maximumWeightedCapacity(size)
|
||||
.concurrencyLevel(32)
|
||||
.build();
|
||||
this.engine = engine;
|
||||
cache = new KCache<>(k -> stream.get(Cache.keyX(k), Cache.keyZ(k)), size);
|
||||
Iris.service(PreservationSVC.class).registerCache(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,11 +56,26 @@ public class CachedStream2D<T> extends BasicStream<T> implements ProceduralStrea
|
||||
|
||||
@Override
|
||||
public T get(double x, double z) {
|
||||
return cache.compute(Cache.key((int) x, (int) z), (k, v) -> v != null ? v : stream.get((int) x, (int) z));
|
||||
return cache.get(Cache.key((int)x, (int)z));
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(double x, double y, double z) {
|
||||
return stream.get(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSize() {
|
||||
return cache.getSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaxSize() {
|
||||
return cache.getMaxSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClosed() {
|
||||
return engine.isClosed();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user