mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-16 17:44:46 +00:00
Multithread Market Saving + Record History for Markets
This commit is contained in:
1 parent
9e7c2d7e6a
commit
3507cb85c5
13 files changed
+172
-74
No files matched your search
@@ -6,11 +6,17 @@ import java.util.Map;
|
||||
|
||||
public class CacheHashMap<K, V extends CacheHashMap.CachedObject<?>> extends HashMap<K, V> {
|
||||
private final long maxCacheTime;
|
||||
private long saveInterval;
|
||||
|
||||
public CacheHashMap(long maxCacheTime) {
|
||||
this.maxCacheTime = maxCacheTime;
|
||||
}
|
||||
|
||||
public CacheHashMap(long maxCacheTime, long saveInterval) {
|
||||
this.maxCacheTime = maxCacheTime;
|
||||
this.saveInterval = saveInterval;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(Object key) {
|
||||
V object = super.get(key);
|
||||
@@ -19,9 +25,16 @@ public class CacheHashMap<K, V extends CacheHashMap.CachedObject<?>> extends Has
|
||||
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) {
|
||||
long lifeTimeDiff = entry.getValue().timeSinceAccessed();
|
||||
if (lifeTimeDiff > this.maxCacheTime) {
|
||||
entry.getValue().save();
|
||||
iterator.remove();
|
||||
} else if (this.saveInterval > 0) {
|
||||
long saveTimeDiff = entry.getValue().timeSinceSaved();
|
||||
if (saveTimeDiff > this.saveInterval) {
|
||||
entry.getValue().save();
|
||||
entry.getValue().updateSaveTime();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,22 +42,34 @@ public class CacheHashMap<K, V extends CacheHashMap.CachedObject<?>> extends Has
|
||||
}
|
||||
|
||||
public static class CachedObject<T> {
|
||||
private long timeSaved;
|
||||
private long lastTimeAccessed;
|
||||
private final T object;
|
||||
|
||||
public CachedObject(T object) {
|
||||
this.object = object;
|
||||
this.lastTimeAccessed = System.currentTimeMillis();
|
||||
this.timeSaved = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public long timeSinceAccessed() {
|
||||
return System.currentTimeMillis() - this.lastTimeAccessed;
|
||||
}
|
||||
|
||||
public long timeSinceSaved() {
|
||||
return System.currentTimeMillis() - this.timeSaved;
|
||||
}
|
||||
|
||||
public void updateTimeAccessed() {
|
||||
this.lastTimeAccessed = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public void updateSaveTime() {
|
||||
this.timeSaved = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public void save(){}
|
||||
|
||||
public T getObject() {
|
||||
return this.object;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user