-
Notifications
You must be signed in to change notification settings - Fork 0
Add cleanup timer for SimpleCache fallback #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,14 +20,38 @@ try { | |
| console.warn('NodeCache module not found, using fallback implementation'); | ||
| // Simple fallback implementation | ||
| NodeCache = class SimpleCache { | ||
| constructor() { | ||
| constructor(options = {}) { | ||
| this.cache = new Map(); | ||
| this.stdTTL = options.stdTTL || 0; | ||
| this.checkperiod = options.checkperiod || 0; | ||
|
|
||
| if (this.checkperiod > 0) { | ||
| this._cleanupInterval = setInterval(() => this.cleanup(), this.checkperiod * 1000); | ||
| if (this._cleanupInterval.unref) { | ||
| this._cleanupInterval.unref(); | ||
| } | ||
|
|
||
| const clear = () => clearInterval(this._cleanupInterval); | ||
| process.once('exit', clear); | ||
| process.once('SIGINT', clear); | ||
| process.once('SIGTERM', clear); | ||
| } | ||
| } | ||
|
|
||
| cleanup() { | ||
| const now = Date.now(); | ||
| for (const [key, item] of this.cache.entries()) { | ||
| if (item.expires && now > item.expires) { | ||
| this.cache.delete(key); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| set(key, value, ttl) { | ||
| const actualTtl = typeof ttl === 'number' ? ttl : this.stdTTL; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): Normalize TTL to a non-negative number Validate or clamp TTL to ensure it is non-negative before using it. |
||
| this.cache.set(key, { | ||
| value, | ||
| expires: ttl ? Date.now() + (ttl * 1000) : null | ||
| expires: actualTtl ? Date.now() + (actualTtl * 1000) : null | ||
| }); | ||
| return true; | ||
|
Comment on lines
50
to
56
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
cleanupmethod in theSimpleCacheclass iterates over all cache entries to remove expired ones. This approach can lead to performance issues, especially with a large number of cache entries, as it requires a full scan of the cache. Consider optimizing this by maintaining a priority queue or another data structure that can more efficiently track and remove expired entries. This would reduce the time complexity from O(n) to a more manageable level, improving the performance of the cache cleanup operation.