Skip to content

Conversation

@mbuckingham74
Copy link
Owner

Summary

Two cache service improvements:

1. Stable cache key generation

The old code used raw JSON.stringify(params) which produces different strings for objects with the same properties but different key order:

JSON.stringify({a:1, b:2}) // '{"a":1,"b":2}'
JSON.stringify({b:2, a:1}) // '{"b":2,"a":1}'  ← different!

This could cause duplicate cache entries for logically identical requests, wasting storage and causing cache misses.

The new stableStringify() function sorts object keys before stringifying, ensuring consistent cache keys.

2. Cleanup interval graceful shutdown

Added .unref() call to the hourly cleanup interval. Without this, the interval keeps the Node.js event loop alive and prevents graceful process shutdown.

Test plan

  • Verify cache still works (hit/miss behavior unchanged)
  • Verify server can shut down cleanly (SIGTERM exits promptly)

🤖 Generated with Claude Code

- Use JSON.stringify with replacer for stable key ordering
  This preserves JSON.stringify semantics (Date.toJSON, undefined omission,
  etc.) while ensuring {a:1, b:2} and {b:2, a:1} produce the same cache key

- Call unref() on cleanup interval so it doesn't prevent graceful shutdown
  Without this, the hourly interval keeps the Node.js event loop alive
  even when the server is trying to shut down

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@mbuckingham74 mbuckingham74 force-pushed the perf/cache-key-stability branch from c7a068e to 25025a3 Compare December 13, 2025 01:59
@mbuckingham74 mbuckingham74 merged commit f9e9f6e into main Dec 13, 2025
18 of 19 checks passed
@mbuckingham74 mbuckingham74 deleted the perf/cache-key-stability branch December 13, 2025 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants