Skip to content

Add cleanup timer for SimpleCache fallback#52

Open
numbpill3d wants to merge 1 commit intomainfrom
codex/modify-simplecache-for-periodic-cleanup
Open

Add cleanup timer for SimpleCache fallback#52
numbpill3d wants to merge 1 commit intomainfrom
codex/modify-simplecache-for-periodic-cleanup

Conversation

@numbpill3d
Copy link
Collaborator

@numbpill3d numbpill3d commented Jun 8, 2025

Summary

  • add periodic cleanup logic to fallback SimpleCache
  • clear timer when process exits

Testing

  • npm test (fails: Missing required Supabase environment variables)

https://chatgpt.com/codex/tasks/task_e_68450e463050832fbdefec9efb0d3b83

Summary by Sourcery

Implement periodic cleanup and TTL configuration in the fallback SimpleCache, and ensure cleanup timers are cleared on process termination.

New Features:

  • Introduce periodic cleanup of expired cache entries in the fallback SimpleCache.
  • Add support for specifying default TTL and cleanup interval through SimpleCache constructor options.

Enhancements:

  • Ensure the cleanup timer is cleared on process exit, SIGINT, and SIGTERM signals.
  • Update the set method to apply the configured default TTL when no TTL is provided.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jun 8, 2025

Reviewer's Guide

Enhance the fallback SimpleCache by introducing configurable default TTL and periodic cleanup with proper resource teardown on process exit, and update set() to use the new default TTL logic.

Sequence Diagram: SimpleCache Initialization with Cleanup

sequenceDiagram
  participant Caller
  participant SC as SimpleCache
  participant NodeProcess as process
  participant TimerModule as "Global Timer"

  Caller->>SC: new SimpleCache(options)
  SC->>SC: Initialize stdTTL, checkperiod from options
  alt checkperiod > 0
    SC->>TimerModule: setInterval(this.cleanup, checkperiod * 1000)
    TimerModule-->>SC: intervalId (_cleanupInterval)
    opt intervalId.unref exists
      SC->>SC: intervalId.unref()
    end
    SC->>NodeProcess: process.once('exit', clearTimerCallback)
    SC->>NodeProcess: process.once('SIGINT', clearTimerCallback)
    SC->>NodeProcess: process.once('SIGTERM', clearTimerCallback)
  end
Loading

Sequence Diagram: SimpleCache Periodic Cleanup Process

sequenceDiagram
  participant TimerModule as "Global Timer"
  participant SC as SimpleCache
  participant CacheMap as "cache (Map)"

  TimerModule-->>SC: Periodically invokes cleanup()
  SC->>SC: Get current time (now)
  loop For each [key, item] in cache
    SC->>CacheMap: Access item
    alt item has expired (item.expires && now > item.expires)
      SC->>CacheMap: delete(key)
    end
  end
Loading

Sequence Diagram: Clearing Cleanup Timer on Process Exit

sequenceDiagram
  participant NodeProcess as process
  participant SC_Instance as "SimpleCache Instance"
  participant TimerModule as "Global Timer"

  NodeProcess-->>SC_Instance: Event: 'exit' / 'SIGINT' / 'SIGTERM'
  SC_Instance->>TimerModule: clearInterval(SC_Instance._cleanupInterval)
Loading

File-Level Changes

Change Details Files
Implement periodic cleanup and resource management in fallback SimpleCache
  • Introduce stdTTL and checkperiod options in constructor
  • Schedule and unref a cleanup interval based on checkperiod
  • Register process exit/SIGINT/SIGTERM handlers to clear the interval
  • Add cleanup() method to purge expired entries
server/utils/performance.js
Refine TTL handling in set()
  • Compute actualTtl using explicit ttl or stdTTL
  • Set expires only when actualTtl is non-zero
server/utils/performance.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Comment on lines +41 to +47
cleanup() {
const now = Date.now();
for (const [key, item] of this.cache.entries()) {
if (item.expires && now > item.expires) {
this.cache.delete(key);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cleanup method in the SimpleCache class 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.

Comment on lines 50 to 56
set(key, value, ttl) {
const actualTtl = typeof ttl === 'number' ? ttl : this.stdTTL;
this.cache.set(key, {
value,
expires: ttl ? Date.now() + (ttl * 1000) : null
expires: actualTtl ? Date.now() + (actualTtl * 1000) : null
});
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The set method in the SimpleCache class always returns true, which does not provide any feedback about the success of the cache insertion operation. This could mask errors or issues during the cache update process. It would be beneficial to implement error handling or at least return a boolean indicating the success of the operation. Additionally, consider throwing exceptions or logging errors when the cache operation fails, to aid in debugging and maintaining the cache logic.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @numbpill3d - I've reviewed your changes - here's some feedback:

  • Registering process listeners per SimpleCache instance can lead to listener leaks when multiple instances are created; consider centralizing or cleaning up those listeners after the interval is cleared.
  • Expose a close() or stopCleanup() method on SimpleCache so users can manually clear the interval and attached listeners when the cache is no longer needed, instead of relying solely on process exit.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

}

set(key, value, ttl) {
const actualTtl = typeof ttl === 'number' ? ttl : this.stdTTL;
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant