Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Keep concurrent-miss coalescing when you reimplement a cache wrapper
A plain get-miss-compute-set wrapper runs the expensive function once per concurrent caller, so at every TTL boundary on a hot key the load on the backing store is multiplied by request concurrency. Deduplicate in-flight misses on the same key with a promise map.
Bad example
| 1 | async wrap(key, fn, { ttl }) { |
| 2 | const hit = await this.get(key); |
| 3 | if (hit !== null) return hit; |
| 4 | const value = await fn(); // N concurrent misses -> N executions |
| 5 | await this.set(key, value, ttl); |
| 6 | return value; |
| 7 | } |
Explanation (EN)
Concurrent misses without coalescing multiply backing-store load.
Objašnjenje (HR)
Istodobni cache promašaji bez objedinjavanja umnožavaju opterećenje izvora.
Good example
| 1 | private inFlight = new Map<string, Promise<unknown>>(); |
| 2 |
|
| 3 | async wrap<T>(key: string, fn: () => Promise<T>, { ttl }): Promise<T> { |
| 4 | const hit = await this.get<T>(key); |
| 5 | if (hit !== null) return hit; |
| 6 |
|
| 7 | const existing = this.inFlight.get(key); |
| 8 | if (existing) return existing as Promise<T>; |
| 9 |
|
| 10 | const pending = fn() |
| 11 | .then(async (value) => { |
| 12 | await this.set(key, value, ttl); |
| 13 | return value; |
| 14 | }) |
| 15 | .finally(() => this.inFlight.delete(key)); |
| 16 |
|
| 17 | this.inFlight.set(key, pending); |
| 18 | return pending; |
| 19 | } |
Explanation (EN)
Overlapping misses within this wrapper instance share one promise; distributed coordination and cache-failure behavior require separate policies.
Objašnjenje (HR)
Preklapajući promašaji unutar ove instance dijele jedan promise. Distribuirana koordinacija i ponašanje pri kvaru cachea zahtijevaju zasebnu politiku.
Notes (EN)
Preserve in-flight deduplication for callers with the same complete cache identity, and remove settled/rejected promises so later attempts can run.
Bilješke (HR)
Zadrži objedinjavanje istodobnih poziva s istim potpunim cache identitetom. Ukloni završene ili odbijene promiseove kako bi kasniji pokušaji mogli ponovno izvršiti posao.
Exceptions / Tradeoffs (EN)
A process-local map only coalesces within that instance and does not guarantee one source fetch across a cluster or TTL window. Separate authorization scopes and avoid one caller cancelling work shared by others.