Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Never write an unserializable value into a cache, and treat it as a miss on read
Write only values supported by the cache codec, handle absent serialization results explicitly and recover from invalid stored entries.
Bad example
| 1 | set(key, value, ttl) { |
| 2 | // JSON.stringify(undefined) === undefined, not a string |
| 3 | return client.setEx(key, ttl, JSON.stringify(value)); |
| 4 | } |
| 5 |
|
| 6 | async get(key) { |
| 7 | const raw = await client.get(key); |
| 8 | return raw !== null ? JSON.parse(raw) : null; // JSON.parse(undefined) throws forever |
| 9 | } |
Explanation (EN)
Passing a non-string serialization result to a string store can fail or create an unreadable value.
Objašnjenje (HR)
Rezultat serijalizacije koji nije string može izazvati kvar ili nečitljiv zapis u spremištu stringova.
Good example
| 1 | set(key, value, ttl) { |
| 2 | if (value === undefined) return; // not cacheable |
| 3 | return client.setEx(key, ttl, JSON.stringify(value)); |
| 4 | } |
| 5 |
|
| 6 | async get(key) { |
| 7 | const raw = await client.get(key); |
| 8 | if (raw === null || raw === undefined) return null; // both are a miss |
| 9 | return JSON.parse(raw); |
| 10 | } |
Explanation (EN)
Non-cacheable values are returned to the caller but never stored, and both absent forms read as a miss.
Objašnjenje (HR)
Vrijednosti koje se ne mogu kesirati vracaju se pozivatelju, ali se nikad ne spremaju, a oba oblika odsutnosti citaju se kao promasaj.
Notes (EN)
Validate the cache serialization contract before writing: JSON.stringify can return undefined, throw for cycles/BigInt or alter unsupported values. Treat an absent entry separately from valid null/false/zero.
Bilješke (HR)
Prije upisa provjeri ugovor serijalizacije: JSON.stringify može vratiti undefined, baciti pogrešku za ciklus ili BigInt te promijeniti nepodržane vrijednosti. Nedostajući zapis razlikuj od valjanih null, false i nula.
Exceptions / Tradeoffs (EN)
A tagged serialization codec may intentionally support values plain JSON cannot. Corrupt stored data needs eviction/recovery according to the cache contract, not a claim it lasts until process restart.