Redis component for Midway.js framework providing comprehensive Redis database integration with support for single instances, clusters, and sentinel configurations
npx @tessl/cli install tessl/npm-midwayjs--redis@3.20.0@midwayjs/redis is a Redis component for the Midway.js Node.js framework that provides comprehensive Redis database integration capabilities. It serves as a service factory wrapper around the ioredis library, offering support for single Redis instances, Redis Sentinel high-availability setups, and Redis Cluster configurations with automatic client lifecycle management.
npm install @midwayjs/redisimport { RedisService, RedisServiceFactory, Configuration } from "@midwayjs/redis";For accessing the original ioredis class:
import { Redis } from "@midwayjs/redis";import { RedisService } from "@midwayjs/redis";
import { Inject } from "@midwayjs/core";
// In a Midway.js service or controller
export class UserService {
@Inject()
redisService: RedisService;
async cacheUser(id: string, userData: any) {
// Use like standard ioredis - all Redis methods available
await this.redisService.set(`user:${id}`, JSON.stringify(userData));
await this.redisService.expire(`user:${id}`, 3600);
}
async getUser(id: string) {
const cached = await this.redisService.get(`user:${id}`);
return cached ? JSON.parse(cached) : null;
}
}@midwayjs/redis is built around several key components:
RedisServiceFactory manages multiple Redis client instances with configuration-driven setupRedisService provides the default Redis client with full ioredis API delegationRedisConfiguration handles application startup, shutdown, and health checksCore service factory for creating and managing Redis client instances with support for single, cluster, and sentinel configurations.
class RedisServiceFactory extends ServiceFactory<Redis> {
protected redisConfig: ServiceFactoryConfigOption<RedisConfigOptions>;
protected logger: ILogger;
protected async createClient(config: RedisConfigOptions): Promise<Redis>;
getName(): string;
protected async destroyClient(redisInstance: Redis): Promise<void>;
}Main service wrapper that provides access to Redis operations through dependency injection, delegating all ioredis methods.
class RedisService implements Redis {
private serviceFactory: RedisServiceFactory;
private instance: Redis;
defineCommand(
name: string,
definition: {
lua: string;
numberOfKeys?: number;
readOnly?: boolean;
}
): void;
}
interface RedisService extends Redis {}Lifecycle and health check management for Redis components within the Midway.js framework.
class RedisConfiguration implements ILifeCycle {
async onReady(container: IMidwayContainer): Promise<void>;
async onStop(container: IMidwayContainer): Promise<void>;
async onHealthCheck(container: IMidwayContainer): Promise<HealthResult>;
}import { ServiceFactoryConfigOption } from '@midwayjs/core';
import { ClusterNode, ClusterOptions } from 'ioredis';
type RedisConfigOptions =
| Redis.RedisOptions
| ({
cluster?: boolean;
nodes?: ClusterNode[];
} & ClusterOptions);
interface HealthResult {
status: boolean;
reason: string;
}
interface MidwayCommonError extends Error {
name: 'MidwayCommonError';
}