-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try an exponential decay top 10 table for a smoother top N experience.
- Loading branch information
1 parent
07fdc48
commit 6af32ae
Showing
4 changed files
with
36 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 35 additions & 19 deletions
54
src/rust/lqosd/src/node_manager/js_build/src/lq_js_common/helpers/timed_cache.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,49 @@ | ||
// Keeps items for up to 10 seconds. Items are tagged by a key, which will | ||
// be used to avoid duplicates. | ||
const alpha = 0.3; | ||
|
||
export class TimedCache { | ||
constructor(maxAge = 10) { | ||
this.cache = new Map(); | ||
this.maxAge = maxAge; | ||
constructor(maxAgeSeconds) { | ||
this.entries = new Map(); | ||
this.maxAge = maxAgeSeconds; | ||
} | ||
|
||
addOrUpdate(key, value) { | ||
this.cache.set(key, { value: value, age: 0 }); | ||
addOrUpdate(key, value, score) { | ||
if (!this.entries.has(key)) { | ||
// New entry | ||
this.entries.set(key, { value: value, score: score, lastSeen: Date.now() }); | ||
} else { | ||
// Update existing entry | ||
let entry = this.entries.get(key); | ||
entry.value = value; | ||
entry.score = alpha * score + (1 - alpha) * entry.score; | ||
entry.lastSeen = Date.now(); | ||
} | ||
} | ||
|
||
tick() { | ||
let toRemove = []; | ||
this.cache.forEach((val, key) => { | ||
val.age += 1; | ||
if (val.age > this.maxAge) { | ||
toRemove.push(key); | ||
// Remove older than maxAge seconds | ||
let now = Date.now(); | ||
this.entries.forEach((v, k) => { | ||
if (now - v.lastSeen > this.maxAge * 1000) { | ||
this.entries.delete(k); | ||
} | ||
}); | ||
toRemove.forEach((key) => { | ||
this.cache.delete(key) | ||
}); | ||
} | ||
|
||
get() { | ||
let result = []; | ||
this.cache.forEach((val) => { | ||
result.push(val.value); | ||
}) | ||
return result; | ||
// Sort by score, descending | ||
let entries = Array.from(this.entries.values()); | ||
entries.sort((a, b) => { | ||
return b.score - a.score; | ||
}); | ||
|
||
// Map to only have the value | ||
entries = entries.map((v) => { | ||
return v.value; | ||
}); | ||
|
||
// Return the top 10 | ||
return entries.slice(0, 10); | ||
} | ||
} | ||
} |