Skip to content

Commit

Permalink
support redis cluster, redis sentinel
Browse files Browse the repository at this point in the history
  • Loading branch information
miaowing committed Jun 11, 2019
1 parent db301d7 commit 6958380
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
9 changes: 7 additions & 2 deletions packages/redis/interfaces/redis-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { RedisOptions } from 'ioredis';
import { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis';

export interface RedisModuleOptions extends RedisOptions {
export interface RedisModuleOptions {
name?: string;
dependencies?: string[];
cluster: {
nodes: ClusterNode[];
options?: ClusterOptions;
};
redisOptions: RedisOptions;
}
7 changes: 3 additions & 4 deletions packages/redis/redis.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { DynamicModule, Global, Module } from '@nestjs/common';
import { createClient, createOptions } from "./redis.provider";
import { RedisModuleOptions } from "./interfaces/redis-options.interface";
import { create, createOptions } from './redis.provider';
import { RedisModuleOptions } from './interfaces/redis-options.interface';
import { NEST_BOOT_PROVIDER, NEST_BOOT, NEST_CONSUL_CONFIG_PROVIDER, NEST_CONSUL_CONFIG } from '@nestcloud/common';


@Global()
@Module({})
export class RedisModule {
Expand All @@ -20,7 +19,7 @@ export class RedisModule {
}
}

const redisProvider = createClient(options.name, inject);
const redisProvider = create(options.name, inject);
const optionsProvider = createOptions(options.name, inject, options);

return {
Expand Down
30 changes: 23 additions & 7 deletions packages/redis/redis.provider.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
import * as Redis from 'ioredis';
import { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis';
import { IBoot, IConsulConfig } from '@nestcloud/common';

import { REDIS_CLIENT, REDIS_MODULE_OPTIONS } from "./constants";
import { RedisModuleOptions } from "./interfaces/redis-options.interface";
import { REDIS_CLIENT, REDIS_MODULE_OPTIONS } from './constants';
import { RedisModuleOptions } from './interfaces/redis-options.interface';

export const createClient = (name: string = 'default', inject?: string[]) => ({
export const create = (name: string = 'default', inject?: string[]) => ({
provide: REDIS_CLIENT + name,
useFactory: (options: RedisModuleOptions, config: IBoot | IConsulConfig): Redis.Redis => {
useFactory: (options: RedisModuleOptions, config: IBoot | IConsulConfig): Redis.Redis | Redis.Cluster => {
if (config) {
options = (config as IBoot).get<RedisModuleOptions>(`redis.${ name }`, options);
options = (config as IBoot).get<RedisModuleOptions>(`redis.${name}`, options);
}
if (options.cluster) {
const nodes: ClusterNode[] = options.cluster.nodes;
const clusterOptions: ClusterOptions = options.cluster.options;
return createCluster(nodes, clusterOptions);
} else {
const redisOptions = options.redisOptions;
return createClient(redisOptions);
}
return new Redis(options);
},
inject: [REDIS_MODULE_OPTIONS + name, ...inject],
});

export const createOptions = (name: string = 'default', inject?: string[], options?: RedisModuleOptions) => ({
provide: REDIS_MODULE_OPTIONS + name,
useValue: options,
inject
inject,
});

const createClient = (options: RedisOptions): Redis.Redis => {
return new Redis(options);
};

const createCluster = (nodes: ClusterNode[], options: ClusterOptions): Redis.Cluster => {
return new Redis.Cluster(nodes, options);
};

0 comments on commit 6958380

Please sign in to comment.