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).
Namespace cache keys with a consistent, type-describing prefix
Prefix cache keys with a consistent namespace (e.g. session:, user:) to avoid collisions, make keys self-describing, and enable targeted invalidation.
Bad example
| 1 | // Bare composite key with no namespace |
| 2 | const cacheKey = `${siteSlug}:${token}`; |
| 3 | await cache.setJson(cacheKey, session, ttl); |
| 4 |
|
| 5 | // Elsewhere, a different value type reuses the same id shape |
| 6 | const otherKey = `${siteSlug}:${token}`; // collision risk, unclear what it holds |
Explanation (EN)
Without a type prefix the key is ambiguous: you cannot tell what kind of value it stores, two unrelated features can collide on the same identifier shape, and you cannot scan or bulk-invalidate one category of keys.
Objašnjenje (HR)
Bez prefiksa tipa kljuc je dvosmislen: ne moze se znati kakvu vrijednost cuva, dvije nepovezane znacajke mogu se sudariti na istom obliku identifikatora, i ne mozes skenirati ni masovno ponistiti jednu kategoriju kljuceva.
Good example
| 1 | const SESSION_KEY_PREFIX = 'zephr-session:'; |
| 2 | const sessionCacheKey = `${SESSION_KEY_PREFIX}${siteSlug}:${token}`; |
| 3 | await cache.setJson(sessionCacheKey, session, ttl); |
| 4 |
|
| 5 | // Self-describing, collision-free, and scannable: cache.scan('zephr-session:*') |
Explanation (EN)
A consistent prefix makes every key self-describing, eliminates cross-type collisions, and lets you target a whole namespace for scanning or invalidation. Reusing the project's existing prefix convention keeps keys uniform across the codebase.
Objašnjenje (HR)
Dosljedan prefiks cini svaki kljuc samoopisujucim, uklanja sudare medju tipovima i omogucuje ciljanje citavog imenskog prostora za skeniranje ili ponistavanje. Ponovno koristenje postojece konvencije prefiksa u projektu cini kljuceve ujednacenima kroz citavu bazu koda.
Notes (EN)
Centralize key builders (e.g. a userCacheKey(id) helper) so the prefix is defined once and reused, instead of templating the string inline at every call site.
Bilješke (HR)
Centraliziraj graditelje kljuceva (npr. helper userCacheKey(id)) tako da je prefiks definiran jednom i ponovno koristen, umjesto da string sastavljas inline na svakom pozivnom mjestu.