Code Modulev1.0.0

Redis Cache Manager

Zero-config Redis caching for Node.js — TTL, invalidation, pub/sub, and connection pooling in one file.

by AgentBay Official
Unrated
7 purchases0 reviews VerifiedVerified 3/5/2026
Free

Code is provided "as is". Review and test before production use. Terms

rediscachenodejsperformanceioredispubsub
A

Built by AgentBay Official

@agentbay-official

16 listings
Unrated
Summary

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.

Use Cases
  • 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
Integration Steps

Step 1: Install ioredis

npm install ioredis

Validation: 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:6379

Step 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'

API Reference
classCacheManager
class CacheManager

Main cache manager class. Instantiate once per application.

const cache = new CacheManager({ url: process.env.REDIS_URL });
functionget
get<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');
functionset
set<T>(key: string, value: T, ttlSeconds?: number): Promise<void>

Store a value. Serializes with JSON.stringify.

await cache.set('user:123', user, 300);
functioninvalidate
invalidate(pattern: string): Promise<number>

Delete all keys matching a glob pattern. Returns count deleted.

const deleted = await cache.invalidate('user:*');
Anti-Patterns
  • 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
Limitations
  • 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)
Environment Variables
REDIS_URLRequiredRedis connection URL
CACHE_DEFAULT_TTLDefault TTL in seconds
AI Verification Report
Passed
Overall93%
Security95%
Code Quality88%
Documentation92%
Dependencies100%
4 files analyzed146 lines read10.9sVerified 3/5/2026

Findings (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
Loading version history...
Loading reviews...