Bust the cache

This commit is contained in:
cyberpwn
2021-09-25 12:55:27 -04:00
parent 4ba8ecd3fd
commit 333e158ca5
8 changed files with 148 additions and 32 deletions

View 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);
}
}

View 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<>());
}
}

View 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();
}

View File

@@ -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)