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

View File

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