提交 b71806e0 authored 作者: zzy's avatar zzy

feat(redis-client.provider.ts): add multi client

service can can use multi client
上级 0626744f
import * as Redis from "ioredis";
import { RedisModuleOptions, RedisModuleAsyncOptions } from "./redis.interface";
export declare class RedisClientError extends Error {
}
export interface RedisClient {
defaultKey: string;
clients: Map<string, Redis.Redis>;
size: number;
}
export declare const createClient: () => {
provide: symbol;
useFactory: (options: RedisModuleOptions) => Redis.Redis;
useFactory: (options: RedisModuleOptions | RedisModuleOptions[]) => {
defaultKey: string;
clients: Map<string, Redis.Redis>;
size: number;
};
inject: symbol[];
};
export declare const createAsyncClientOptions: (options: RedisModuleAsyncOptions) => {
provide: symbol;
useFactory: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions>;
useFactory: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions> | RedisModuleOptions[] | Promise<RedisModuleOptions[]>;
inject: any[];
};
......@@ -2,10 +2,37 @@
Object.defineProperty(exports, "__esModule", { value: true });
const Redis = require("ioredis");
const redis_constants_1 = require("./redis.constants");
const uuid = require("uuid");
class RedisClientError extends Error {
}
exports.RedisClientError = RedisClientError;
exports.createClient = () => ({
provide: redis_constants_1.REDIS_CLIENT,
useFactory: (options) => {
return new Redis(options);
const clients = new Map();
const defaultKey = uuid();
if (Array.isArray(options)) {
for (let o of options) {
if (o.name) {
if (clients.has(o.name)) {
throw new RedisClientError(`client ${o.name} is exists`);
}
clients.set(o.name, new Redis(o));
}
else {
if (clients.has(defaultKey)) {
throw new RedisClientError('default client is exists');
}
clients.set(defaultKey, new Redis(o));
}
}
}
else {
clients.set(defaultKey, new Redis(options));
}
return {
defaultKey, clients, size: clients.size
};
},
inject: [redis_constants_1.REDIS_MODULE_OPTIONS]
});
......
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { RedisOptions } from 'ioredis';
export interface RedisModuleOptions extends RedisOptions {
name?: string;
}
export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions>;
useFactory?: (...args: any[]) => RedisModuleOptions | RedisModuleOptions[] | Promise<RedisModuleOptions> | Promise<RedisModuleOptions[]>;
inject?: any[];
}
import { DynamicModule } from '@nestjs/common';
import { RedisModuleOptions, RedisModuleAsyncOptions } from './redis.interface';
export declare class RedisModule {
static register(options: RedisModuleOptions): DynamicModule;
static register(options: RedisModuleOptions | RedisModuleOptions[]): DynamicModule;
static forRootAsync(options: RedisModuleAsyncOptions): DynamicModule;
}
import * as Redis from "ioredis";
import { RedisClient } from "./redis-client.provider";
export declare class RedisService {
private readonly client;
constructor(client: Redis.Redis);
getClient(): Redis.Redis;
private readonly redisClient;
constructor(redisClient: RedisClient);
getClient(name?: string): Redis.Redis;
}
......@@ -14,13 +14,19 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@nestjs/common");
const redis_constants_1 = require("./redis.constants");
const Redis = require("ioredis");
const redis_client_provider_1 = require("./redis-client.provider");
let RedisService = class RedisService {
constructor(client) {
this.client = client;
constructor(redisClient) {
this.redisClient = redisClient;
}
getClient() {
return this.client;
getClient(name) {
if (!name) {
name = this.redisClient.defaultKey;
}
if (!this.redisClient.clients.has(name)) {
throw new redis_client_provider_1.RedisClientError(`client ${name} is not exists`);
}
return this.redisClient.clients.get(name);
}
};
RedisService = __decorate([
......
import * as Redis from "ioredis"
import { REDIS_CLIENT, REDIS_MODULE_OPTIONS } from './redis.constants';
import { RedisModuleOptions, RedisModuleAsyncOptions } from "./redis.interface";
import * as uuid from 'uuid'
export class RedisClientError extends Error {}
export interface RedisClient {
defaultKey:string,
clients: Map<string, Redis.Redis>,
size:number
}
export const createClient = () => ({
provide: REDIS_CLIENT,
useFactory:(options: RedisModuleOptions) => {
return new Redis(options)
useFactory:(options: RedisModuleOptions | RedisModuleOptions[]) => {
const clients = new Map<string,Redis.Redis>()
const defaultKey = uuid()
if(Array.isArray(options)) {
for(let o of options) {
if(o.name) {
if(clients.has(o.name)) {
throw new RedisClientError(`client ${o.name} is exists`)
}
clients.set(o.name, new Redis(o))
} else {
if(clients.has(defaultKey)) {
throw new RedisClientError('default client is exists')
}
clients.set(defaultKey, new Redis(o))
}
}
} else {
clients.set(defaultKey,new Redis(options))
}
return {
defaultKey,clients,size:clients.size
}
},
inject:[REDIS_MODULE_OPTIONS]
})
......
import { ModuleMetadata } from '@nestjs/common/interfaces';
import { RedisOptions } from 'ioredis';
export interface RedisModuleOptions extends RedisOptions {}
export interface RedisModuleOptions extends RedisOptions {
name?: string
}
export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory?: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions>,
useFactory?: (...args: any[]) => RedisModuleOptions | RedisModuleOptions[] | Promise<RedisModuleOptions> | Promise<RedisModuleOptions[]>,
inject?: any[];
}
\ No newline at end of file
......@@ -10,7 +10,7 @@ import { createClient, createAsyncClientOptions } from './redis-client.provider'
exports:[RedisService]
})
export class RedisModule {
static register(options:RedisModuleOptions): DynamicModule {
static register(options:RedisModuleOptions|RedisModuleOptions[]): DynamicModule {
return {
module: RedisModule,
providers: [
......
import { Injectable, Inject } from "@nestjs/common";
import { REDIS_CLIENT } from './redis.constants';
import * as Redis from "ioredis";
import { RedisClient, RedisClientError } from "./redis-client.provider";
@Injectable()
export class RedisService {
constructor(
@Inject(REDIS_CLIENT) private readonly client: Redis.Redis
) {}
@Inject(REDIS_CLIENT) private readonly redisClient: RedisClient
) { }
getClient() : Redis.Redis {
return this.client
getClient(name?: string): Redis.Redis {
if (!name) {
name = this.redisClient.defaultKey
}
if (!this.redisClient.clients.has(name)) {
throw new RedisClientError(`client ${name} is not exists`)
}
return this.redisClient.clients.get(name)
}
}
\ No newline at end of file
{
"name": "nestjs-redis",
"version": "0.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@types/ioredis": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/@types/ioredis/-/ioredis-4.0.4.tgz",
"integrity": "sha512-QdJTMFrmKkphjoGIxItTMhP++8/6INLbgSIxB8kd9N+3OuiuiaZ2knt+OR4gFQrloac/ctwaQA1PCzISmD9afQ==",
"dev": true,
"requires": {
"@types/node": "10.12.12"
}
},
"@types/node": {
"version": "10.12.12",
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.12.tgz",
"integrity": "sha512-Pr+6JRiKkfsFvmU/LK68oBRCQeEg36TyAbPhc2xpez24OOZZCuoIhWGTd39VZy6nGafSbxzGouFPTFD/rR1A0A==",
"dev": true
}
}
}
......@@ -12,16 +12,26 @@
"build": "rm -rf dist && tsc -p tsconfig.json",
"precommit": "lint-staged",
"prepublish:npm": "yarn run build",
"publish:npm": "yarn publish --access public"
"publish:npm": "yarn publish --access public",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^5.1.0",
"ioredis": "^4.2.0",
"rxjs": "^6.2.2"
"rxjs": "^6.2.2",
"uuid": "^3.3.2"
},
"devDependencies": {
"@nestjs/testing": "^5.5.0",
"@types/ioredis": "^4.0.4",
"@types/node": "^10.7.1",
"@types/uuid": "^3.4.4",
"cz-conventional-changelog": "^2.1.0",
"jest": "^23.6.0",
"ts-jest": "^23.10.5",
"typescript": "^2.4.2"
},
"keywords": [
......@@ -29,5 +39,10 @@
"nest",
"redis",
"nestjs-redis"
]
],
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论