refactor(utils): expose Stats per-key data as public trackedKeys map#1660
Conversation
Rename the private `_keyCounts` map to a public `trackedKeys` field so consumers can read per-key counters (and `trackedKeys.size`) directly, and drop the now-redundant `trackedKeyCount` getter. Export the `KeyCounters` value type and update the tests and README accordingly. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SjsBvVfhc7vKeqPE4nWCUr
There was a problem hiding this comment.
Code Review
This pull request refactors the Stats class by exporting the KeyCounters type and replacing the private _keyCounts map and its public getter trackedKeyCount with a public trackedKeys map. The review feedback correctly points out that exposing a mutable Map directly as a public property breaks encapsulation, as external consumers could mutate the map and bypass internal logic such as pruning and size tracking. The reviewer recommends keeping the map private and exposing it publicly via a getter returning a ReadonlyMap instead.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1660 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 29 29
Lines 3379 3379
Branches 763 777 +14
=========================================
Hits 3379 3379 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b1ce9ad3ea
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Address review feedback: keep the per-key map private (`_trackedKeys`) and expose it through a public getter returning `ReadonlyMap<string, Readonly<KeyCounters>>`. Consumers keep direct `trackedKeys.size` / `.get()` access while no longer being able to mutate internal state and bypass pruning and `maxTrackedKeys` invariants. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SjsBvVfhc7vKeqPE4nWCUr
Summary
Simplifies the
Statsper-key tracking surface in@cacheable/utils:_keyCountsmap to a publictrackedKeysfield. Consumers can now read per-key counters (and their.size) directly viastats.trackedKeys, instead of going through a dedicated accessor.trackedKeyCountgetter —stats.trackedKeys.sizegives the same value.KeyCounterstype (the map's value type: rawhits/misses/gets/sets/deletesper key), since it's now part of the public API.Notes
StatsSnapshot.trackedKeys(fromtoJSON()) is unchanged — it remains the count (anumber) of unique tracked keys, which is the serializable equivalent of the live map's size.recordKey,keyStats,mostUsedKeys,leastUsedKeys,clearKeys) are unchanged.Testing
pnpm --filter @cacheable/utils test— Biome clean, 230 tests pass, 100% coverage onstats.ts.pnpm --filter @cacheable/utils build— dual CJS/ESM + type declarations emit correctly (trackedKeys: Map<string, KeyCounters>,KeyCountersexported).🤖 Generated with Claude Code
https://claude.ai/code/session_01SjsBvVfhc7vKeqPE4nWCUr
Generated by Claude Code