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).
Derive cache keys from all inputs that affect the result
Build cache keys from every parameter that changes the response, not a fixed string, to avoid cache collisions across filters.
Bad example
| 1 | async getOrders(params?: OrderQuery) { |
| 2 | const cacheKey = 'orders'; |
| 3 | const cached = await this.cache.get(cacheKey); |
| 4 | if (cached) return cached; |
| 5 | const result = await this.fetchOrders(params); |
| 6 | await this.cache.set(cacheKey, result, TTL); |
| 7 | return result; |
| 8 | } |
Explanation (EN)
A fixed key means a request filtered by `country=US` and an unfiltered request share the same cache entry, so callers receive data for the wrong filter.
Objašnjenje (HR)
Fiksni ključ znači da zahtjev filtriran s `country=US` i nefiltrirani zahtjev dijele isti zapis u cacheu, pa pozivatelji dobiju podatke za pogrešan filter.
Good example
| 1 | async getOrders(params?: OrderQuery) { |
| 2 | const cacheKey = `orders-${params?.country ?? 'all'}`; |
| 3 | const cached = await this.cache.get(cacheKey); |
| 4 | if (cached) return cached; |
| 5 | const result = await this.fetchOrders(params); |
| 6 | await this.cache.set(cacheKey, result, TTL); |
| 7 | return result; |
| 8 | } |
Explanation (EN)
Encoding the filtering params into the key isolates each variant, so different filters never read each other's cached results.
Objašnjenje (HR)
Ugrađivanje parametara filtriranja u ključ izolira svaku varijantu, pa različiti filteri nikad ne čitaju tuđe cachirane rezultate.
Notes (EN)
Keep keys deterministic: sort/normalize multi-value params so logically equal requests map to the same key.
Bilješke (HR)
Drži ključeve determinističkima: sortiraj/normaliziraj parametre s više vrijednosti kako bi logički jednaki zahtjevi mapirali na isti ključ.