mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-04-05 23:36:12 +00:00
Bust the cache
This commit is contained in:
53
src/main/java/com/volmit/iris/util/data/ChunkCache.java
Normal file
53
src/main/java/com/volmit/iris/util/data/ChunkCache.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.volmit.iris.util.function.Function2;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
|
||||
public class ChunkCache<T> {
|
||||
private final AtomicReferenceArray<T> cache;
|
||||
|
||||
public ChunkCache() {
|
||||
cache = new AtomicReferenceArray<>(256);
|
||||
}
|
||||
|
||||
public T compute(int x, int z, Function2<Integer, Integer, T> function)
|
||||
{
|
||||
T t = get(x&15, z&15);
|
||||
|
||||
if(t == null)
|
||||
{
|
||||
t = function.apply(x, z);
|
||||
set(x&15, z&15, t);
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
private void set(int x, int z, T t) {
|
||||
cache.set(x * 16 + z, t);
|
||||
}
|
||||
|
||||
private T get(int x, int z) {
|
||||
return cache.get(x * 16 + z);
|
||||
}
|
||||
}
|
||||
46
src/main/java/com/volmit/iris/util/data/ComplexCache.java
Normal file
46
src/main/java/com/volmit/iris/util/data/ComplexCache.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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.volmit.iris.engine.data.cache.Cache;
|
||||
import com.volmit.iris.util.collection.KMap;
|
||||
|
||||
public class ComplexCache<T> {
|
||||
private KMap<Long, ChunkCache<T>> chunks;
|
||||
|
||||
public ComplexCache()
|
||||
{
|
||||
chunks = new KMap<>();
|
||||
}
|
||||
|
||||
public boolean has(int x, int z)
|
||||
{
|
||||
return chunks.containsKey(Cache.key(x, z));
|
||||
}
|
||||
|
||||
public void invalidate(int x, int z)
|
||||
{
|
||||
chunks.remove(Cache.key(x, z));
|
||||
}
|
||||
|
||||
public ChunkCache<T> chunk(int x, int z)
|
||||
{
|
||||
return chunks.computeIfAbsent(Cache.key(x, z), (f) -> new ChunkCache<>());
|
||||
}
|
||||
}
|
||||
24
src/main/java/com/volmit/iris/util/data/Heafty.java
Normal file
24
src/main/java/com/volmit/iris/util/data/Heafty.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Heafty {
|
||||
int getHeaft();
|
||||
}
|
||||
@@ -21,18 +21,29 @@ 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.Iris;
|
||||
import com.volmit.iris.engine.framework.MeteredCache;
|
||||
import com.volmit.iris.util.format.Form;
|
||||
import com.volmit.iris.util.scheduling.J;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class KCache<K,V> implements MeteredCache {
|
||||
private long max;
|
||||
private final long max;
|
||||
private CacheLoader<K, V> loader;
|
||||
private LoadingCache<K, V> cache;
|
||||
private final boolean fastDump;
|
||||
|
||||
public KCache(CacheLoader<K, V> loader, long max)
|
||||
{
|
||||
this(loader, max, false);
|
||||
}
|
||||
|
||||
public KCache(CacheLoader<K, V> loader, long max, boolean fastDump)
|
||||
{
|
||||
this.max = max;
|
||||
this.fastDump = fastDump;
|
||||
this.loader = loader;
|
||||
this.cache = create(loader);
|
||||
}
|
||||
@@ -41,6 +52,9 @@ public class KCache<K,V> implements MeteredCache {
|
||||
return Caffeine
|
||||
.newBuilder()
|
||||
.maximumSize(max)
|
||||
.initialCapacity((int) (max))
|
||||
.softValues()
|
||||
.expireAfterAccess(5, TimeUnit.MINUTES)
|
||||
.build((k) -> loader == null ? null : loader.load(k));
|
||||
}
|
||||
|
||||
@@ -57,9 +71,7 @@ public class KCache<K,V> implements MeteredCache {
|
||||
|
||||
public void invalidate()
|
||||
{
|
||||
LoadingCache<?,?> c = cache;
|
||||
cache = create(loader);
|
||||
c.invalidateAll();
|
||||
cache.invalidateAll();
|
||||
}
|
||||
|
||||
public V get(K k)
|
||||
|
||||
Reference in New Issue
Block a user