Fixed clear cache method.

Alternator currently uses threads as identifier (TEMPORARY).
This commit is contained in:
Harrison Deng 2020-04-25 17:58:29 -05:00
parent 1e0d63e562
commit 4b6cfacc47
3 changed files with 15 additions and 14 deletions

View File

@ -7,11 +7,11 @@ public class Cache<Key, Value> {
private final int maxSize; private final int maxSize;
private final HashMap<Key, Value> data; private final HashMap<Key, Value> data;
private final HashMap<Key, Integer> occurrences; private final HashMap<Key, Integer> occurrences;
private final LinkedList<Key> usageTrackers; private final LinkedList<Key> occurrenceOrder;
public Cache(int maxSize) { public Cache(int maxSize) {
data = new HashMap<>(maxSize); data = new HashMap<>(maxSize);
usageTrackers = new LinkedList<>(); occurrenceOrder = new LinkedList<>();
this.maxSize = maxSize; this.maxSize = maxSize;
this.occurrences = new HashMap<>(); this.occurrences = new HashMap<>();
} }
@ -23,10 +23,10 @@ public class Cache<Key, Value> {
public synchronized void setValue(Key key, Value value) { public synchronized void setValue(Key key, Value value) {
if (!data.containsKey(key)) { if (!data.containsKey(key)) {
if (data.size() >= maxSize) { if (data.size() >= maxSize) {
while (occurrences.get(usageTrackers.peek()) > 1) { while (occurrences.get(occurrenceOrder.peek()) > 1) {
occurencesTrackedPoll(); occurencesTrackedPoll();
} }
data.remove(usageTrackers.poll()); data.remove(occurrenceOrder.poll());
} }
occurrencesTrackedAdd(key); occurrencesTrackedAdd(key);
} }
@ -41,11 +41,11 @@ public class Cache<Key, Value> {
} }
occ++; occ++;
occurrences.put(key, occ); occurrences.put(key, occ);
usageTrackers.add(key); occurrenceOrder.add(key);
} }
private Key occurencesTrackedPoll() { private Key occurencesTrackedPoll() {
Key key = usageTrackers.poll(); Key key = occurrenceOrder.poll();
int occ = 0; int occ = 0;
if (occurrences.get(key) != null) { if (occurrences.get(key) != null) {
occ = occurrences.get(key); occ = occurrences.get(key);
@ -70,6 +70,7 @@ public class Cache<Key, Value> {
public void clearCache() { public void clearCache() {
data.clear(); data.clear();
usageTrackers.clear(); occurrenceOrder.clear();
occurrences.clear();
} }
} }

View File

@ -17,7 +17,7 @@ public class IslandSurvivalCraftChunkGenerator extends ChunkGenerator {
@Override @Override
public ChunkData generateChunkData(World world, Random random, int chunkX, int chunkZ, BiomeGrid biome) { public ChunkData generateChunkData(World world, Random random, int chunkX, int chunkZ, BiomeGrid biome) {
IslandWorldGenerator worldGenerator = alternator.getIslandChunkGeneratorSystemForWorld(world, random); IslandWorldGenerator worldGenerator = alternator.getIslandChunkGeneratorSystem(world, random);
ChunkData chunk = createChunkData(world); ChunkData chunk = createChunkData(world);
for (int localX = 0; localX < 16; localX++) { for (int localX = 0; localX < 16; localX++) {

View File

@ -2,7 +2,6 @@ package ca.recrown.islandsurvivalcraft.world;
import java.util.HashMap; import java.util.HashMap;
import java.util.Random; import java.util.Random;
import java.util.UUID;
import org.bukkit.World; import org.bukkit.World;
@ -14,7 +13,7 @@ import ca.recrown.islandsurvivalcraft.world.generation.IslandWorldGenerator;
* Uses IslandWorldGenerator as the container for each world. * Uses IslandWorldGenerator as the container for each world.
*/ */
public class IslandWorldGeneratorAlternator { public class IslandWorldGeneratorAlternator {
private HashMap<UUID, IslandWorldGenerator> chunkGenerator; private HashMap<Long, IslandWorldGenerator> chunkGenerator;
private IslandBiomeGenerator islandBiomeGenerator; private IslandBiomeGenerator islandBiomeGenerator;
public IslandWorldGeneratorAlternator(IslandBiomeGenerator biomeGenerator) { public IslandWorldGeneratorAlternator(IslandBiomeGenerator biomeGenerator) {
@ -22,10 +21,11 @@ public class IslandWorldGeneratorAlternator {
this.islandBiomeGenerator = biomeGenerator; this.islandBiomeGenerator = biomeGenerator;
} }
public synchronized IslandWorldGenerator getIslandChunkGeneratorSystemForWorld(World world, Random random) { public synchronized IslandWorldGenerator getIslandChunkGeneratorSystem(World world, Random random) {
if (!chunkGenerator.containsKey(world.getUID())) { long tid = Thread.currentThread().getId();
chunkGenerator.put(world.getUID(), new IslandWorldGenerator(world, islandBiomeGenerator.getInstance(), random)); if (!chunkGenerator.containsKey(tid)) {
chunkGenerator.put(tid, new IslandWorldGenerator(world, islandBiomeGenerator.getInstance(), random));
} }
return chunkGenerator.get(world.getUID()); return chunkGenerator.get(tid);
} }
} }