@gatwech/redis
v1.0.0-beta.0
Published
Payvo Node.js Redis Client for Microservices
Downloads
211
Maintainers
Readme
@gatwech/redis
A robust, type-safe Redis client wrapper for Payvo microservices, built on top of ioredis. This package provides a set of specialized services for common caching patterns, distributed locking, and pub/sub messaging.
Installation
pnpm add @gatwech/redisConfiguration
The package exports a createRedisClient factory to initialize the main Redis connection.
import { createRedisClient } from "@gatwech/redis";
const redis = createRedisClient({
host: "localhost",
port: 6379,
// ... other ioredis options
});Services
ServiceCacheService
A dedicated cache service for microservices, providing namespace isolation.
import { ServiceCacheService } from "@gatwech/redis";
const cache = new ServiceCacheService(redis, "my-service");
await cache.set("user:123", { name: "John" }, 300); // TTL 300s
const user = await cache.get("user:123");HashCacheService
Manage Redis Hashes with ease.
import { HashCacheService } from "@gatwech/redis";
const hashCache = new HashCacheService("redis://localhost:6379");
await hashCache.setField("users", "123", { name: "John" });
const user = await hashCache.getField("users", "123");DistributedLockService
Implement distributed locking to prevent race conditions across services.
import { DistributedLockService } from "@gatwech/redis";
const lockService = new DistributedLockService(redis);
const lock = await lockService.acquireLock("resource:123", 5000); // 5s TTL
if (lock) {
try {
// Critical section
} finally {
await lockService.releaseLock("resource:123", lock);
}
}BlacklistService
Efficiently manage blacklisted tokens or identifiers.
import { getBlacklistService } from "@gatwech/redis";
const blacklist = getBlacklistService();
await blacklist.add("token-123", 3600); // Blacklist for 1 hour
const isBlacklisted = await blacklist.has("token-123");Pub/Sub
Publish and subscribe to events using CachePublisher and CacheSubscriber.
import { CachePublisher, CacheSubscriber } from "@gatwech/redis";
const publisher = new CachePublisher(redis);
await publisher.publish("channel", { message: "hello" });
const subscriber = new CacheSubscriber(redis);
await subscriber.subscribe("channel", (message) => {
console.log(message);
});License
ISC
