Refactoring.

This commit is contained in:
Harrison Deng 2020-04-29 23:12:27 -05:00
parent 64a3e3db27
commit f637e79ea5
2 changed files with 10 additions and 3 deletions

View File

@ -8,13 +8,13 @@ import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
public class Cache<K, V> {
private final int maxSize;
private final ConcurrentHashMap<K, CacheValue<K, V>> data;
private final UsageStack<K, V> usage = new UsageStack<>();
private final CacheUsageStack<K, V> usage = new CacheUsageStack<>();
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private final WriteLock writeLock = lock.writeLock();
private final ReadLock readLock = lock.readLock();
public Cache(int maxSize) {
data = new ConcurrentHashMap<>(maxSize + 1, 0.75f, 6);
data = new ConcurrentHashMap<>(maxSize + 1, 0.75f, 16);
this.maxSize = maxSize;
}

View File

@ -2,7 +2,8 @@ package ca.recrown.islandsurvivalcraft.caching;
import java.util.concurrent.locks.ReentrantLock;
class UsageStack<K, V> {
class CacheUsageStack<K, V> {
private volatile int size;
private volatile CacheValue<K, V> first, last;
private final ReentrantLock lock = new ReentrantLock();
@ -15,6 +16,7 @@ class UsageStack<K, V> {
}
value.front = null;
first = value;
size++;
}
private void removeValue(CacheValue<K, V> value) {
@ -30,6 +32,7 @@ class UsageStack<K, V> {
}
value.front = null;
value.back = null;
size--;
}
public void moveToTop(CacheValue<K, V> value) {
@ -86,4 +89,8 @@ class UsageStack<K, V> {
lock.unlock();
}
}
public int size() {
return size;
}
}