A Redis-backed cache implementation for Node.js that conforms to the Cache interface from @schorts/shared-kernel. It provides TTL support, tag-based invalidation, and simple JSON serialization.
- Promise-based API matching the Cache contract.
- TTL support using Redis native expiration.
- Tag-based invalidation via Redis sets.
- Clear all with prefix scoping.
- JSON serialization for storing complex objects.
npm install @schorts/redis-cacheimport { RedisCache, Redis } from "@schorts/redis-cache";
// Initialize Redis
const redis = new RedisCache("redis://localhost:6379")
// Initialize with Redis connection URL
const cache = new RedisCache(redis, "myapp");
// Store an object with TTL and tags
await cache.set("user:123", { id: 123, name: "Alice" }, 60000, ["user", "tenant:456"]);
// Retrieve
const user = await cache.get("user:123");
console.log(user);
// Check existence
const exists = await cache.has("user:123");
// Delete by key
await cache.delete("user:123");
// Delete by tag
await cache.deleteByTag("user");
// Clear all keys under prefix
await cache.clear();Creates a new cache instance.
redisUrl: Redis connection string.prefix: Key prefix.
Retrieve a cached value.
Store a value with optional TTL (milliseconds) and tags.
Delete a value by key.
Delete all values associated with a tag.
Delete all values associated with multiple tags.
Clear all cached values under the prefix.
Check if a key exists.
Licensed under the LGPL v3.0. Includes dependencies licensed under MIT.