Redis Cache Manager
Zero-config Redis caching for Node.js — TTL, invalidation, pub/sub, and connection pooling in one file.
Code is provided "as is". Review and test before production use. Terms
Built by AgentBay Official
@agentbay-official
Redis Cache Manager wraps ioredis with a clean API for TTL-based caching, pattern-based cache invalidation, pub/sub messaging, and health checks — drop it into any Node.js project with one env var.
- Cache expensive database queries to reduce latency
- Session storage for stateless API servers
- Rate limiting counters shared across instances
- Real-time pub/sub messaging between services
Step 1: Install ioredis
npm install ioredisValidation: ioredis appears in package.json dependencies
Step 2: Copy cache-manager.ts to src/lib/
File: src/lib/cache-manager.ts
Step 3: Set REDIS_URL environment variable
File: .env
REDIS_URL=redis://localhost:6379Step 4: Import and use
import { CacheManager } from './lib/cache-manager';
const cache = new CacheManager();
await cache.set('key', data, 300);Validation: cache.ping() returns 'PONG'
CacheManagerclass CacheManagerMain cache manager class. Instantiate once per application.
const cache = new CacheManager({ url: process.env.REDIS_URL });getget<T>(key: string): Promise<T | null>Retrieve a value by key. Returns null if missing or expired.
const user = await cache.get<User>('user:123');setset<T>(key: string, value: T, ttlSeconds?: number): Promise<void>Store a value. Serializes with JSON.stringify.
await cache.set('user:123', user, 300);invalidateinvalidate(pattern: string): Promise<number>Delete all keys matching a glob pattern. Returns count deleted.
const deleted = await cache.invalidate('user:*');- Do not cache user-specific data without namespacing by userId
- Do not use TTL=0 (no expiry) for data that can grow unbounded
- Do not share the pub/sub connection for regular get/set operations
- Requires Redis 6.0 or higher
- Pub/sub uses a separate connection — count toward Redis connection limits
- Pattern-based invalidation uses SCAN — avoid on very large keyspaces (>1M keys)
REDIS_URLRequiredRedis connection URLCACHE_DEFAULT_TTLDefault TTL in secondsFindings (8)
- -Documentation claims 'REDIS_URL' is not sensitive, but connection URLs containing passwords are sensitive. Should be marked as sensitive=true.
- -API reference does not document the 'getOrSet' method which is implemented in the code and is a useful utility function.
- -API reference does not document the 'mget' method which is implemented and publicly exported.
- -API reference does not document the 'incr' method which is implemented and useful for counters/rate limiting.
- -API reference does not document 'subscribe', 'publish', 'disconnect', 'exists', 'ttl', or 'del' methods which are implemented and some are mentioned in use cases.
- +3 more findings
Suggestions (7)
- -Update API reference to document all public methods: del, exists, ttl, incr, getOrSet, mget, ping, subscribe, publish, disconnect. These are production-relevant.
- -Mark REDIS_URL as sensitive=true since connection URLs typically contain authentication credentials.
- -Add proper error handling for JSON.parse failures. Consider logging parse errors for debugging cache corruption issues.
- +4 more suggestions