Skip to content

Commit

Permalink
Merge pull request thx#275 from galaxybing/feature-redis-cluster
Browse files Browse the repository at this point in the history
feature redis cluster, development env
  • Loading branch information
Bosn authored Aug 7, 2018
2 parents dc2e3d0 + fa97f85 commit d160dd0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
"cluster": "^0.7.7",
"cross-env": "^5.1.1",
"graceful": "^1.0.1",
"ioredis": "^3.2.2",
"is-md5": "^0.0.2",
"js-beautify": "^1.6.9",
"kcors": "^2.2.1",
"koa": "^2.2.0",
"koa-body": "^4.0.1",
"koa-generic-session": "^2.0.1",
"koa-logger": "^3.2.0",
"koa-redis": "^3.1.2",
"koa-redis": "git://github.com/galaxybing/koa-redis.git#feature-add-redis-cluster",
"koa-router": "^7.1.1",
"koa-send": "^4.0.0",
"koa-static": "^4.0.2",
Expand Down
20 changes: 18 additions & 2 deletions src/config/config.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,27 @@ let config: IConfigOptions = {
pool: {
max: 5,
min: 0,
idle: 10000,
idle: 10000
},
logging: false,
logging: true
},
redis: {
// isRedisCluster: true,
// nodes: [{
// port: 6379,
// host: '*.*.*',
// },
// {
// port: 6380,
// host: '*.*.*',
// },
// {
// port: 6381,
// host: '*.*.*',
// }],
// redisOptions: {
// password: '*',
// }
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/service/redis.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as redis from 'redis'
import * as ioredis from 'ioredis'
import config from '../config'

export enum CACHE_KEY {
REPOSITORY_GET = 'REPOSITORY_GET',
}

export default class RedisService {
private static client: redis.RedisClient = redis.createClient(config.redis)
// 判断 是否使用cluster集群
private static client: redis.RedisClient = config.redis && config.redis.isRedisCluster ? new ioredis.Cluster(config.redis.nodes, {redisOptions: config.redis.redisOptions}) : redis.createClient(config.redis)

private static getCacheKey(key: CACHE_KEY, entityId?: number): string {
return `${key}:${entityId || ''}`
Expand Down
1 change: 1 addition & 0 deletions src/types/custom-typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'ioredis';
14 changes: 11 additions & 3 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/// <reference path="custom-typings.d.ts" />

import { PoolOptions } from "sequelize";
import { ISequelizeConfig } from "sequelize-typescript";
import { RedisOptions } from "koa-redis";

declare interface RedisAndClusterOptions extends RedisOptions {
isRedisCluster?: boolean;
nodes?: object[];
redisOptions?: any;
}

declare interface IConfigOptions {
version: string,
serve: {
Expand All @@ -12,6 +20,6 @@ declare interface IConfigOptions {
key: string
},
keycenter?: string | boolean,
db: ISequelizeConfig
redis: RedisOptions
}
db: ISequelizeConfig,
redis: RedisAndClusterOptions
}

0 comments on commit d160dd0

Please sign in to comment.