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:
parent
3e80d1e67d
commit
9b111cc977
@ -32,11 +32,9 @@ public class Cache<K, V> {
|
|||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
if (data.containsKey(key)) return;
|
if (data.containsKey(key)) return;
|
||||||
CacheValue<K, V> val = new CacheValue<>();
|
CacheValue<K, V> val = new CacheValue<>(key, value);
|
||||||
val.key = key;
|
|
||||||
val.value = value;
|
|
||||||
usage.add(val);
|
|
||||||
data.put(key, val);
|
data.put(key, val);
|
||||||
|
usage.add(val);
|
||||||
if (data.size() > maxSize) {
|
if (data.size() > maxSize) {
|
||||||
data.remove(usage.pop().key);
|
data.remove(usage.pop().key);
|
||||||
}
|
}
|
||||||
|
@ -21,4 +21,9 @@ class CacheValue<KeyType, ValueType> {
|
|||||||
*/
|
*/
|
||||||
public volatile boolean detached = true;
|
public volatile boolean detached = true;
|
||||||
|
|
||||||
|
public CacheValue(KeyType key, ValueType value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user