mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-17 11:05:12 +00:00
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:
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user