Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Keep negative caching when you replace a lookup layer
A lookup cache that only stores hits re-queries the backend on every single request for anything that does not exist, forever. Caching the explicit not-found result is what bounds the cost of misses, and it is easy to drop silently when swapping the provider out.
Bad example
| 1 | async getLogo(id) { |
| 2 | const hit = await cache.get(key(id)); |
| 3 | if (hit) return hit; |
| 4 | const found = await search.byId(id); // miss -> uncached query every request |
| 5 | if (found) await cache.set(key(id), found, 3600); |
| 6 | return found ?? null; |
| 7 | } |
Explanation (EN)
Ids absent from the index cost a backend query on every request for the rest of time, because only successes are ever written.
Objašnjenje (HR)
Identifikatori kojih nema u indeksu kostaju upit prema pozadini na svakom zahtjevu zauvijek, jer se zapisuju samo uspjesi.
Good example
| 1 | async getLogo(id) { |
| 2 | const hit = await cache.get(key(id)); |
| 3 | if (hit) return hit; |
| 4 | const found = await search.byId(id); |
| 5 | const value = found ?? { id, logo: null }; // cache the miss too |
| 6 | await cache.set(key(id), value, 3600); |
| 7 | return value.logo === null ? null : value; |
| 8 | } |
Explanation (EN)
A missing id costs one lookup per TTL window instead of one per request.
Objašnjenje (HR)
Identifikator koji ne postoji kosta jedno trazenje po TTL prozoru umjesto jednog po zahtjevu.
Notes (EN)
When you delete a provider, read what its cache actually stored before deciding the replacement is equivalent. Negative caching rarely appears in the interface, only in the body.
Bilješke (HR)
Kad brises pruzatelja podataka, procitaj sto je njegov cache stvarno spremao prije nego zakljucis da je zamjena istovrijedna. Negativno kesiranje rijetko se vidi u sucelju, samo u tijelu funkcije.