@flightdev/cache-redis
v0.0.7
Published
Redis cache adapter for Flight Framework using ioredis
Maintainers
Readme
@flightdev/cache-redis
Redis cache adapter for Flight Framework. Production-ready caching with Redis.
Table of Contents
Installation
npm install @flightdev/cache-redis ioredisQuick Start
import { createCache } from '@flightdev/core/cache';
import { redis } from '@flightdev/cache-redis';
const cache = createCache(redis({
url: process.env.REDIS_URL,
}));
// Set a value
await cache.set('user:123', { name: 'John', email: '[email protected]' });
// Get a value
const user = await cache.get('user:123');
// Delete a value
await cache.delete('user:123');Configuration
redis({
// Redis URL
url: 'redis://localhost:6379',
// Or individual options
host: 'localhost',
port: 6379,
password: 'secret',
db: 0,
// Key prefix
prefix: 'myapp:cache:',
// Default TTL in seconds
ttl: 3600,
// Enable TLS
tls: true,
// Connection options
maxRetriesPerRequest: 3,
connectTimeout: 10000,
});Usage
Basic Operations
// Set with TTL
await cache.set('key', 'value', { ttl: 60 });
// Get
const value = await cache.get('key');
// Check existence
const exists = await cache.has('key');
// Delete
await cache.delete('key');
// Clear all
await cache.clear();With Fetch
Fetch from cache or compute if missing:
const user = await cache.fetch('user:123', async () => {
return await db.users.findById(123);
}, { ttl: 300 });Tags
Invalidate groups of keys:
await cache.set('products:1', data, { tags: ['products'] });
await cache.set('products:2', data, { tags: ['products'] });
// Invalidate all products
await cache.invalidateTag('products');Cluster Mode
import { redisCluster } from '@flightdev/cache-redis';
const cache = createCache(redisCluster({
nodes: [
{ host: 'node1.example.com', port: 6379 },
{ host: 'node2.example.com', port: 6379 },
{ host: 'node3.example.com', port: 6379 },
],
password: 'secret',
}));API Reference
Adapter Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| url | string | - | Redis URL |
| host | string | 'localhost' | Redis host |
| port | number | 6379 | Redis port |
| password | string | - | Password |
| db | number | 0 | Database number |
| prefix | string | '' | Key prefix |
| ttl | number | 3600 | Default TTL |
| tls | boolean | false | Enable TLS |
License
MIT
