← ClaudeAtlas

redis-cachelisted

Redis with ioredis in a Node.js backend — CacheManager and SessionManager helpers, cache-aside reads, key naming, mandatory TTLs, invalidation on write, Redis as non-fatal, SCAN instead of KEYS, Redis-backed rate limiting and pub/sub. Use when adding caching, sessions, rate limits, counters or realtime fan-out with Redis, or reviewing Redis code.
manishdashsharma/express-backend-skills · ★ 0 · API & Backend · score 72
Install: claude install-skill manishdashsharma/express-backend-skills
# Redis ## Access Never `new Redis()` in a module. Use the helpers from `src/config/redis.js` (template in the `create-backend` skill): ```js import { getRedisClient, CacheManager, SessionManager } from '../../../config/redis.js'; const cache = new CacheManager(); ``` Instantiating `CacheManager` at module scope is fine — it holds no connection of its own. ## Non-negotiables 1. **Every key has a TTL.** `cache.set(key, value, ttlSeconds)`; counters use `incr(key, ttl)` which sets expiry on first hit. No key lives forever, especially anything sensitive. 2. **Redis is non-fatal.** Every helper catches errors and returns a safe default (`null`, `false`, `0`, `[]`). Services must still work when Redis is down. Never `throw` from a cache path. 3. **Invalidate on write.** The service function that mutates data deletes every cache key that covers it, in the same function, after the write succeeds. Cross-entity writes bust every affected key. 4. **Never `KEYS` in production code.** Use `SCAN` (`delByPattern` in the template). Prefer deterministic keys you can delete directly over pattern deletes. ## Key naming `<resource>:<scopeId>:<id>` or `<resource>:<scopeId>:<detail>` — always include the tenant scope so one tenant can never read another's cache. ``` vendor:store_1:abc123 categories:store_1 unread:store_1:user_4 stats:store_1:daily ``` Define key builders and TTL constants once per module or in `shared/cache/`: ```js const VENDOR_TTL_SECONDS = 300; const vendorKey = (s