Skip to content

Commit 6f9b794

Browse files
authored
docs(cache): add JSDoc comments for TTLCache (JhaSourav07#621)
## Description Added JSDoc documentation for the TTLCache class and all public methods in lib/cache.ts. - Changes made - Added class-level documentation - Added constructor documentation - Added @param, @returns, and @example tags for public methods - Documented in-process cache limitation - No functional changes Fixes JhaSourav07#302 ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview <img width="1297" height="936" alt="image" src="https://github.com/user-attachments/assets/dc88c136-d035-4fef-ae67-eb4dbe10243e" /> <img width="1127" height="927" alt="image" src="https://github.com/user-attachments/assets/d548be43-ee27-4ae3-b654-509113964ff7" /> ## 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): ...`). - [x] 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 community for contributor discussions, mentorship, and faster PR support.
2 parents 18f5bd3 + dd0338e commit 6f9b794

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

lib/cache.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1+
/**
2+
* Represents a cached item with its expiration timestamp.
3+
*/
14
type CacheItem<T> = {
25
value: T;
36
expiresAt: number;
47
};
58

9+
/**
10+
* A Simple in-memory TTL(Time To Live) cache.
11+
*
12+
* Stores values in-process only and automatically removes expired entries.
13+
* This cache is not shared accross multiple server instances or severless invocations.
14+
*
15+
* @typeParam T - Type of values stored in the cache.
16+
*/
617
export class TTLCache<T> {
718
private store = new Map<string, CacheItem<T>>();
819
private cleanupInterval: ReturnType<typeof setInterval> | null = null;
920
private readonly maxSize?: number;
1021

22+
/**
23+
* Creates a new TTL cache instance.
24+
*
25+
* @param maxSize - Maximum number of items allowed in the cache.
26+
* @param cleanupIntervalMs - Interval in milliseconds for cleaning expired entries.
27+
*/
1128
constructor(maxSize?: number, cleanupIntervalMs: number = 60000) {
1229
this.maxSize = maxSize === undefined ? undefined : Math.max(1, maxSize);
1330
const interval = Math.max(1000, cleanupIntervalMs);
@@ -35,6 +52,17 @@ export class TTLCache<T> {
3552
}
3653
}
3754

55+
/**
56+
* Retrieves a value from the cache.
57+
*
58+
* Returns 'null' if the key does not exist or if the entry has expired.
59+
*
60+
* @param key - Cache key.
61+
* @returns The cached value or 'null'.
62+
*
63+
* @example
64+
* const user = cache.get("user:1");
65+
*/
3866
get(key: string): T | null {
3967
const hit = this.store.get(key);
4068
if (!hit) return null;
@@ -47,6 +75,20 @@ export class TTLCache<T> {
4775
return hit.value;
4876
}
4977

78+
/**
79+
* Stores a value in the cache with a TTL.
80+
*
81+
* If the cache reaches its maximum capacity, the oldest item
82+
* may be removed to make room for new entries.
83+
*
84+
* @param key - Cache key.
85+
* @param value - Value to cache.
86+
* @param ttlMs - Time to live in milliseconds.
87+
* @returns void
88+
*
89+
* @example
90+
* cache.set("user:1",userData,5000);
91+
*/
5092
set(key: string, value: T, ttlMs: number): void {
5193
// Capacity eviction (FIFO / LRU-lite)
5294
const maxSize = this.maxSize;
@@ -64,10 +106,23 @@ export class TTLCache<T> {
64106
this.store.set(key, { value, expiresAt: Date.now() + ttlMs });
65107
}
66108

109+
/**
110+
* Removes all entries from the cache.
111+
*
112+
* @returns void
113+
*
114+
* @example
115+
* cache.clear();
116+
*/
67117
clear(): void {
68118
this.store.clear();
69119
}
70120

121+
/**
122+
* Stops the cleanup interval and clears the cache.
123+
*
124+
* @returns void
125+
*/
71126
destroy(): void {
72127
if (this.cleanupInterval) {
73128
clearInterval(this.cleanupInterval);

0 commit comments

Comments
 (0)