Added synchronization blocks to functions retrieving objects from CacheHashMaps and made the API use one for caching item stacks as well.

This commit is contained in:
Luis C.M committed 2023-05-31 00:35:57 -05:00
1 parent 3bc02a708e
commit 118d79f3ad
4 files changed
+34 -53

No files matched your search

@@ -11,25 +11,18 @@ public class CacheHashMap<K, V extends CacheHashMap.CachedObject<?>> extends Has
@Override
public V get(Object key) {
synchronized (this) {
List<K> keysToRemove = new ArrayList<>();
Set<Map.Entry<K,V>> entrySet = this.entrySet();
for (Map.Entry<K,V> entry : entrySet) {
if (!Objects.equals(key,entry.getKey())) {
long timeDiff = entry.getValue().timeSinceAccessed();
if (timeDiff > this.maxCacheTime) {
keysToRemove.add(entry.getKey());
}
}
}
for (K k : keysToRemove) {
this.remove(k);
}
}
V object = super.get(key);
object.updateTimeAccessed();
Iterator<Map.Entry<K,V>> iterator = this.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<K,V> entry = iterator.next();
long timeDiff = entry.getValue().timeSinceAccessed();
if (timeDiff > this.maxCacheTime) {
iterator.remove();
}
}
return object;
}