-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a buffer for the top/bottom 10 downloaders for smoother transitions.
- Loading branch information
1 parent
1a83a75
commit 61d3c42
Showing
3 changed files
with
68 additions
and
2 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
48 changes: 48 additions & 0 deletions
48
src/rust/lqosd/src/node_manager/js_build/src/helpers/timed_lru_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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export class TimedLRUCache { | ||
constructor(maxCapacity = 10, timeToLive = 300) { // default TTL is 5 minutes (300 seconds) | ||
this.cache = new Map(); | ||
this.maxCapacity = maxCapacity; | ||
this.timeToLive = timeToLive; // in seconds | ||
} | ||
|
||
// Add or update an object in the cache | ||
set(key, value) { | ||
if (this.cache.has(key)) { | ||
this.cache.delete(key); // Reset order | ||
} | ||
this.cache.set(key, { value: value, lastAccessed: Date.now() }); | ||
|
||
// If capacity exceeds max, remove the oldest item | ||
if (this.cache.size > this.maxCapacity) { | ||
const oldestKey = this.cache.keys().next().value; | ||
this.cache.delete(oldestKey); | ||
} | ||
} | ||
|
||
// Get an object from the cache (resets time) | ||
get(key) { | ||
if (!this.cache.has(key)) { | ||
return null; | ||
} | ||
const entry = this.cache.get(key); | ||
entry.lastAccessed = Date.now(); // Update access time | ||
this.cache.delete(key); | ||
this.cache.set(key, entry); // Refresh order in the Map | ||
return entry.value; | ||
} | ||
|
||
// Tick function to be called externally every second | ||
tick() { | ||
const now = Date.now(); | ||
for (const [key, entry] of this.cache) { | ||
const elapsedTime = (now - entry.lastAccessed) / 1000; // convert ms to seconds | ||
if (elapsedTime > this.timeToLive) { | ||
this.cache.delete(key); // Remove expired items | ||
} | ||
} | ||
} | ||
|
||
toArray() { | ||
return Array.from(this.cache.values()); | ||
} | ||
} |