Redis component for Midway.js framework providing comprehensive Redis database integration with support for single instances, clusters, and sentinel configurations
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
The RedisConfiguration class manages the lifecycle of Redis components within the Midway.js framework, handling initialization, cleanup, and health monitoring for Redis services.
Lifecycle management class implementing Midway's ILifeCycle interface.
/**
* Configuration class for Redis component lifecycle management
* Handles initialization, shutdown, and health checks
*/
class RedisConfiguration implements ILifeCycle {
async onReady(container: IMidwayContainer): Promise<void>;
async onStop(container: IMidwayContainer): Promise<void>;
async onHealthCheck(container: IMidwayContainer): Promise<HealthResult>;
}Initializes the Redis service factory when the application is ready.
/**
* Called when the Midway application is ready
* Initializes the RedisServiceFactory
* @param container - Midway IoC container
*/
async onReady(container: IMidwayContainer): Promise<void>;Gracefully shuts down all Redis connections when the application stops.
/**
* Called when the Midway application is stopping
* Gracefully shuts down all Redis clients
* @param container - Midway IoC container
*/
async onStop(container: IMidwayContainer): Promise<void>;Monitors the health status of all Redis client connections.
/**
* Performs health check on all Redis clients
* Excludes low-priority clients from health check evaluation
* @param container - Midway IoC container
* @returns Health check result with status and reason
*/
async onHealthCheck(container: IMidwayContainer): Promise<HealthResult>;
interface HealthResult {
status: boolean;
reason: string;
}The configuration is automatically loaded through Midway's configuration system:
// config/config.default.ts
import { MidwayConfig } from '@midwayjs/core';
export default {
redis: {
// Single client configuration
client: {
host: 'localhost',
port: 6379,
password: 'your-password',
db: 0,
},
// Client priority configuration for health checks
priority: {
default: 'High' // Include in health check evaluation
}
}
} as MidwayConfig;The RedisConfiguration includes default configuration imports:
@Configuration({
namespace: 'redis',
importConfigs: [
{
default: {
redis: {},
},
},
],
})
export class RedisConfiguration {
// Configuration implementation
}The health check evaluates Redis client status:
isLowPriority() methodPriority Configuration:
'Low' priority are excluded from causing health check failures'High' priority (or undefined) can fail health checksUsage Examples:
import { RedisConfiguration } from "@midwayjs/redis";
import { IMidwayContainer } from "@midwayjs/core";
// Manual health check (typically called by framework)
async function checkRedisHealth(container: IMidwayContainer) {
const config = new RedisConfiguration();
const health = await config.onHealthCheck(container);
console.log('Redis Health:', health.status);
if (!health.status) {
console.log('Failure Reason:', health.reason);
}
}// Healthy response
{
status: true,
reason: ''
}
// Unhealthy response
{
status: false,
reason: 'redis client "cache" is not ready'
}The configuration integrates with Midway's application lifecycle:
// Automatic integration - no manual setup required
import { Configuration } from '@midwayjs/core';
import { RedisConfiguration } from '@midwayjs/redis';
@Configuration({
imports: [RedisConfiguration],
})
export class MainConfiguration {
// RedisConfiguration lifecycle hooks are automatically called
}onReady() during application startuponStop() during application shutdown// Configuration validation errors are logged and propagated
try {
await container.getAsync(RedisServiceFactory);
} catch (error) {
// Handle Redis initialization failure
console.error('Redis initialization failed:', error.message);
}// Health check failures provide specific client information
const health = await config.onHealthCheck(container);
if (!health.status) {
// Log specific client failure
console.error(`Redis health check failed: ${health.reason}`);
}// Shutdown errors are logged but don't prevent application termination
await config.onStop(container);
// Application continues shutdown process even if Redis cleanup failsThe configuration uses the 'redis' namespace for Midway's configuration system:
// Automatic configuration binding
declare module '@midwayjs/core/dist/interface' {
interface MidwayConfig {
redis?: ServiceFactoryConfigOption<RedisConfigOptions>;
}
}This allows type-safe configuration access throughout the application:
import { Config } from '@midwayjs/core';
export class SomeService {
@Config('redis')
redisConfig: ServiceFactoryConfigOption<RedisConfigOptions>;
}