Skip to content

Commit 39d0471

Browse files
authored
feat(@142vip/redis): 新增RedisFactory工厂类,优化Redis实例创建的核心逻辑 (#718)
1 parent 3df6a07 commit 39d0471

File tree

6 files changed

+149
-51
lines changed

6 files changed

+149
-51
lines changed

packages/redis/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,80 @@ npm install @142vip/redis
99
pnpm i @142vip/redis
1010
```
1111

12+
## 使用
13+
14+
### 创建工厂类
15+
```typescript
16+
import { RedisFactory } from '@142vip/redis'
17+
18+
// 初始化工厂类实例
19+
const redisFactory = new RedisFactory()
20+
```
21+
22+
- `redisFactory.createClient()` :创建客户端
23+
- `redisFactory.createCluster()` :创建集群客户端
24+
- `redisFactory.getClient()` :获取客户端
25+
26+
## 配置
27+
28+
### 简单&哨兵模式
29+
```typescript
30+
// 默认连接 127.0.0.1:6379
31+
redisFactory.getClient()
32+
// 连接 127.0.0.1:6380, db 4,使用密码 123456
33+
redisFactory.getClient({
34+
url: 'redis://:123456@127.0.0.1:6380/4',
35+
})
36+
// 用户名、密码都可以通过 URI 传递。
37+
redisFactory.getClient({
38+
url: 'redis://username:password@127.0.0.1:6380/4',
39+
})
40+
redisFactory.getClient({
41+
port: 6379, // 端口
42+
host: '127.0.0.1', // host主机地址
43+
username: 'default', // 需要redis版本大于6
44+
password: 'my-top-secret',
45+
db: 0, // 默认0
46+
})
47+
```
48+
49+
### 集群模式
50+
51+
```typescript
52+
redisFactory.getClient({
53+
clusterNodes: [
54+
{
55+
host: '127.0.0.1',
56+
port: 6379,
57+
},
58+
{
59+
host: '127.0.0.1',
60+
port: 6380,
61+
},
62+
],
63+
// 可选
64+
clusterOptions: {
65+
// 集群模式下,每个节点的连接配置
66+
redisOptions: {
67+
username: 'default', // 需要redis版本大于6
68+
password: 'my-top-secret',
69+
db: 0, // 默认0
70+
},
71+
},
72+
})
73+
```
74+
75+
## 最佳实践
76+
77+
- [egg-redis](https://github.com/eggjs/egg-redis)
78+
- [@142vip/nest-redis](https://github.com/142vip/nest-redis)
79+
80+
## 参考
81+
82+
- [IORedis Npm](https://www.npmjs.com/package/redis)
83+
- [IORedis Github](https://github.com/luin/ioredis)
84+
- [IORedis 官网](https://redis.io/)
85+
1286
## 证书
1387

1488
[MIT](https://opensource.org/license/MIT)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type Cluster from 'ioredis/built/cluster'
2+
import Redis from 'ioredis'
3+
import { RedisClient, RedisClientConfig, RedisClusterConfig, RedisConfig } from './redis.interface'
4+
5+
/**
6+
* Redis工厂类
7+
*/
8+
export class RedisFactory {
9+
/**
10+
* 简单&哨兵模式
11+
*/
12+
public createClient(config: RedisClientConfig): Redis {
13+
return config.url != null ? new Redis(config.url, config) : new Redis(config)
14+
}
15+
16+
/**
17+
* 集群模式
18+
*/
19+
public createCluster(config: RedisClusterConfig): Cluster {
20+
return new Redis.Cluster(config.clusterNodes, config.clusterOptions)
21+
}
22+
23+
/**
24+
* 获取Redis客户端
25+
*/
26+
public getClient(config: RedisConfig): RedisClient {
27+
// 集群模式
28+
if (config.clusterNodes != null) {
29+
return this.createCluster(config as RedisClusterConfig)
30+
}
31+
else {
32+
return this.createClient(config)
33+
}
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Redis from 'ioredis'
2+
import Cluster, { ClusterNode } from 'ioredis/built/cluster'
3+
import { ClusterOptions } from 'ioredis/built/cluster/ClusterOptions'
4+
import { RedisOptions } from 'ioredis/built/redis/RedisOptions'
5+
6+
/**
7+
* io-redis支持的连接模式
8+
*/
9+
export enum RedisMode {
10+
STANDARD = 'standard',
11+
CLUSTER = 'cluster',
12+
SENTINEL = 'sentinel',
13+
}
14+
15+
/**
16+
* 单机、哨兵配置
17+
*/
18+
export interface RedisClientConfig extends RedisOptions {
19+
url?: string
20+
}
21+
22+
/**
23+
* 集群配置
24+
*/
25+
export interface RedisClusterConfig {
26+
clusterNodes: ClusterNode[]
27+
clusterOptions?: ClusterOptions
28+
}
29+
30+
/**
31+
* Redis 建立连接配置
32+
*/
33+
export interface RedisConfig extends RedisClientConfig, Partial<RedisClusterConfig> {}
34+
35+
/**
36+
* Redis 客户端
37+
*/
38+
export type RedisClient = Redis | Cluster

packages/redis/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './io-redis'
2-
export * from './redis'
1+
export * from './core/redis.factory'
2+
export * from './core/redis.interface'

packages/redis/src/io-redis.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

packages/redis/src/redis.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)