mirror of
https://github.com/LibreQoE/LibreQoS.git
synced 2025-02-25 18:55:32 -06:00
Add prototype TimedCache
This commit is contained in:
33
helpers/timed_cache.js
Normal file
33
helpers/timed_cache.js
Normal file
@@ -0,0 +1,33 @@
|
||||
// Keeps items for up to 10 seconds. Items are tagged by a key, which will
|
||||
// be used to avoid duplicates.
|
||||
export class TimedCache {
|
||||
constructor(maxAge = 10) {
|
||||
this.cache = new Map();
|
||||
this.maxAge = maxAge;
|
||||
}
|
||||
|
||||
addOrUpdate(key, value) {
|
||||
this.cache.set(key, { value: value, age: 0 });
|
||||
}
|
||||
|
||||
tick() {
|
||||
let toRemove = [];
|
||||
this.cache.forEach((val, key) => {
|
||||
val.age += 1;
|
||||
if (val.age > this.maxAge) {
|
||||
toRemove.push(key);
|
||||
}
|
||||
});
|
||||
toRemove.forEach((key) => {
|
||||
this.cache.delete(key)
|
||||
});
|
||||
}
|
||||
|
||||
get() {
|
||||
let result = [];
|
||||
this.cache.forEach((val) => {
|
||||
result.push(val.value);
|
||||
})
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user