Skip to main content

Redis

Redis integration for caching and pub/sub.

Installation​

stratix add @stratix/redis

Configuration​

import { RedisPlugin } from '@stratix/redis';

const app = await ApplicationBuilder.create()
.usePlugin(new RedisPlugin({
host: 'localhost',
port: 6379
}))
.build();

Caching​

export class CachedProductRepository implements IProductRepository {
constructor(
private repository: IProductRepository,
private redis: Redis
) {}

async findById(id: string): Promise<Product | null> {
// Check cache
const cached = await this.redis.get(`product:${id}`);
if (cached) {
return JSON.parse(cached);
}

// Fetch from database
const product = await this.repository.findById(id);

// Cache result
if (product) {
await this.redis.setex(
`product:${id}`,
3600,
JSON.stringify(product)
);
}

return product;
}
}

Pub/Sub​

// Publisher
await redis.publish('product.created', JSON.stringify({
id: product.id,
name: product.name
}));

// Subscriber
redis.subscribe('product.created', (message) => {
const event = JSON.parse(message);
console.log('Product created:', event);
});

Next Steps​