-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support redis cluster, redis sentinel
- Loading branch information
miaowing
committed
Jun 11, 2019
1 parent
db301d7
commit 6958380
Showing
3 changed files
with
33 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |