Skip to content

Commit e02de70

Browse files
authored
refactor(cache): add size() method to TTLCache and expose active cache metrics (JhaSourav07#594)
## Description Fixes JhaSourav07#301 Adds a `size(): number` method to the TTLCache implementation in `lib/cache.ts` to allow observing the number of active (non-expired) cache entries. This improves debugging and enables cache metrics without exposing internal state. ## Pillar - [ ] Pillar 1 — New Theme Design - [ ] Pillar 2 — Geometric SVG Improvement - [x] Pillar 3 — Timezone Logic Optimization - [x] Other (Bug fix, refactoring, docs) ## Visual Preview (Not applicable — no UI/SVG changes) ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [ ] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have started the repo. - [x] I have made sure that I have only one commit to merge in this PR. - [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [ ] (Recommended) I joined the CommitPulse Discord server for faster collaboration, mentorship, and PR support.
2 parents 59b2264 + 77585c0 commit e02de70

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

lib/cache.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,44 @@ describe('TTLCache', () => {
9696
});
9797
});
9898

99+
describe('size()', () => {
100+
it('returns 0 for an empty cache', () => {
101+
const cache = new TTLCache<number>();
102+
expect(cache.size()).toBe(0);
103+
cache.destroy();
104+
});
105+
106+
it('counts only entries before expiry', () => {
107+
vi.useFakeTimers();
108+
const cache = new TTLCache<number>();
109+
cache.set('a', 1, 10_000);
110+
cache.set('b', 2, 20_000);
111+
expect(cache.size()).toBe(2);
112+
113+
vi.advanceTimersByTime(15_000);
114+
expect(cache.size()).toBe(1);
115+
cache.destroy();
116+
});
117+
118+
it('returns 0 when all entries have expired (after TTL expiry)', () => {
119+
vi.useFakeTimers();
120+
const cache = new TTLCache<number>();
121+
cache.set('a', 1, 10_000);
122+
vi.advanceTimersByTime(15_000);
123+
expect(cache.size()).toBe(0);
124+
cache.destroy();
125+
});
126+
127+
it('returns 0 after clear() is called', () => {
128+
const cache = new TTLCache<number>();
129+
cache.set('a', 1, 10_000);
130+
expect(cache.size()).toBe(1);
131+
cache.clear();
132+
expect(cache.size()).toBe(0);
133+
cache.destroy();
134+
});
135+
});
136+
99137
describe('destroy()', () => {
100138
it('clears the store and stops the interval', () => {
101139
vi.useFakeTimers();

lib/cache.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ export class TTLCache<T> {
120120
this.store.clear();
121121
}
122122

123-
/**
124-
* Stops the cleanup interval and clears the cache.
125-
*
126-
* @returns void
127-
*/
123+
size(): number {
124+
this.sweep();
125+
return this.store.size;
126+
}
127+
128128
destroy(): void {
129129
if (this.cleanupInterval) {
130130
clearInterval(this.cleanupInterval);

0 commit comments

Comments
 (0)