Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Skip the write for a non-positive TTL instead of passing it to the cache backend
A negative or zero TTL is a common in-house convention for disable caching here, and some backends silently treat it as already-expired. Others reject it outright, so every such call burns a failed round trip and an error log. Short-circuit the write instead of forwarding the sentinel.
Bad example
| 1 | // caller: const ttl = -1; // disable caching in preview mode |
| 2 | set(key, value, ttl) { |
| 3 | if (ttl) { // -1 is truthy |
| 4 | client.setEx(key, ttl, value); // ERR invalid expire time |
| 5 | } |
| 6 | } |
Explanation (EN)
Forwarding a disable-cache sentinel into a positive-expiry API causes avoidable failed writes.
Objašnjenje (HR)
Prosljeđivanje vrijednosti koja isključuje cache API-ju za pozitivan rok uzrokuje nepotrebne neuspješne upise.
Good example
| 1 | set(key, value, ttl) { |
| 2 | if (ttl <= 0) return; // caching disabled for this call |
| 3 | client.setEx(key, ttl, value); |
| 4 | } |
Explanation (EN)
The sentinel is handled where it means something, and the backend only ever sees a valid expiry.
Objašnjenje (HR)
Sentinel se obraduje ondje gdje nesto znaci, a pozadinski servis vidi samo valjano vrijeme isteka.
Notes (EN)
At the cache adapter boundary, define TTL units, allowed range and whether zero/negative values mean disabled or persistent; handle that convention before calling the backend.
Bilješke (HR)
Na granici cache adaptera odredi jedinicu, raspon i značenje TTL-a, uključujući znači li nula ili negativna vrijednost isključeni ili trajni cache. Tu konvenciju obradi prije poziva backenda.
Exceptions / Tradeoffs (EN)
Some libraries use zero to mean no expiry. Do not silently change that existing contract; reject NaN/infinity and enforce backend-specific precision/range.