Cache value now has convenience constructor.

Implemented the changes.

Also placed putting data in hashmap priority so for faster access.
This commit is contained in:
Harrison Deng 2020-04-30 15:50:14 -05:00
parent 3e80d1e67d
commit 9b111cc977
2 changed files with 7 additions and 4 deletions

View File

@ -32,11 +32,9 @@ public class Cache<K, V> {
lock.lock();
try {
if (data.containsKey(key)) return;
CacheValue<K, V> val = new CacheValue<>();
val.key = key;
val.value = value;
usage.add(val);
CacheValue<K, V> val = new CacheValue<>(key, value);
data.put(key, val);
usage.add(val);
if (data.size() > maxSize) {
data.remove(usage.pop().key);
}

View File

@ -21,4 +21,9 @@ class CacheValue<KeyType, ValueType> {
*/
public volatile boolean detached = true;
public CacheValue(KeyType key, ValueType value) {
this.key = key;
this.value = value;
}
}