Skip to content

Commit 9fb5521

Browse files
authored
feat(@142vip/nest-redis): 新增RedisModule全局模块,支持注入即使用,补充单元测试、文档 (#719)
1 parent 39d0471 commit 9fb5521

File tree

14 files changed

+679
-43
lines changed

14 files changed

+679
-43
lines changed

packages/nest-redis/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,63 @@ npm install @142vip/nest-redis
1111
pnpm i @142vip/nest-redis
1212
```
1313

14+
## 配置
15+
16+
```typescript
17+
import { RedisConfig } from '@142vip/redis'
18+
19+
// 简单配置
20+
const config: RedisConfig = {
21+
url: 'redis://localhost:6379',
22+
}
23+
24+
// 集群配置
25+
const clusterConfig: RedisConfig = {
26+
clusterNodes: [
27+
{
28+
url: 'redis://localhost:6379',
29+
},
30+
{
31+
url: 'redis://localhost:6380',
32+
},
33+
],
34+
}
35+
// ...
36+
```
37+
38+
## 使用
39+
40+
### 模块注入
41+
```typescript
42+
import { RedisModule } from '@142vip/nest-redis'
43+
44+
@Module({
45+
imports: [RedisModule.register({
46+
url: 'redis://localhost:6379',
47+
})],
48+
})
49+
export class AppModule {}
50+
```
51+
52+
### 使用服务
53+
```typescript
54+
import { RedisService } from '@142vip/nest-redis'
55+
56+
@Injectable()
57+
export class AppService {
58+
constructor(
59+
private readonly redisService: RedisService
60+
) {}
61+
}
62+
```
63+
64+
`RedisService`类实例化后,可以直接使用类对应的方法,代码的最佳实践,可以参考模块:[redis-example](https://github.com/142vip/core-x/apps/nest-demo/src/core/redis-example)
65+
66+
## 参考
67+
68+
- [NPM @142vip/redis](https://www.npmjs.com/package/@142vip/redis)
69+
- [Redis 官网](https://redis.io/)
70+
1471
## 证书
1572

1673
[MIT](https://opensource.org/license/MIT)

packages/nest-redis/build.config.ts

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

packages/nest-redis/jest.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createDefaultPreset } from 'ts-jest'
2+
3+
const tsJestTransformCfg = createDefaultPreset().transform
4+
5+
/** @type {import("jest").Config} **/
6+
export default {
7+
testEnvironment: 'node',
8+
transform: {
9+
...tsJestTransformCfg,
10+
},
11+
testMatch: [
12+
// "**/__tests__/**/*.[jt]s?(x)",
13+
'**/test/**/?(*.)+(spec|test).[tj]s?(x)',
14+
],
15+
}

packages/nest-redis/package.json

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"exports": {
2424
".": {
2525
"types": "./dist/index.d.ts",
26-
"import": "./dist/index.mjs",
27-
"require": "./dist/index.cjs"
26+
"import": "./dist/index.js",
27+
"require": "./dist/index.js"
2828
}
2929
},
3030
"authorInfo": {
@@ -33,21 +33,29 @@
3333
"url": "https://github.com/142vip",
3434
"homePage": "https://142vip.cn"
3535
},
36-
"main": "./dist/index.mjs",
37-
"module": "./dist/index.mjs",
36+
"main": "./dist/index.js",
37+
"module": "./dist/index.js",
3838
"types": "./dist/index.d.ts",
3939
"files": [
4040
"dist"
4141
],
4242
"scripts": {
43-
"build": "unbuild",
44-
"typecheck": "tsc --noEmit"
43+
"build": "npx nest build",
44+
"typecheck": "tsc --noEmit",
45+
"test": "npx jest",
46+
"test:coverage": "npx jest --coverage"
4547
},
4648
"dependencies": {
47-
"@142vip/utils": "workspace:*"
49+
"@142vip/redis": "workspace:*",
50+
"@nestjs/common": "11.1.7",
51+
"@nestjs/core": "11.1.7"
4852
},
4953
"publishConfig": {
5054
"access": "public",
5155
"registry": "https://registry.npmjs.org"
56+
},
57+
"devDependencies": {
58+
"@nestjs/cli": "11.0.10",
59+
"@nestjs/testing": "11.1.7"
5260
}
5361
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* redis client Token 唯一标记
3+
*/
4+
export const REDIS_CLIENT_TOKEN = Symbol('@142vip/nest-redis#client-token')
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Inject } from '@nestjs/common'
2+
import { REDIS_CLIENT_TOKEN } from './redis.constants'
3+
4+
/**
5+
* redis client 装饰器
6+
*/
7+
export function InjectRedisClient(): PropertyDecorator & ParameterDecorator {
8+
return Inject(REDIS_CLIENT_TOKEN)
9+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { RedisConfig } from '@142vip/redis'
2+
import { DynamicModule, Module } from '@nestjs/common'
3+
import { REDIS_CLIENT_TOKEN } from './redis.constants'
4+
import { RedisService } from './redis.service'
5+
6+
@Module({})
7+
export class RedisModule {
8+
public static register(config: RedisConfig): DynamicModule {
9+
const redisService = new RedisService(config)
10+
const providers = [
11+
{
12+
provide: RedisService,
13+
useValue: redisService,
14+
},
15+
{
16+
provide: REDIS_CLIENT_TOKEN,
17+
useValue: redisService.getClient(),
18+
},
19+
]
20+
return {
21+
module: RedisModule,
22+
providers,
23+
exports: providers,
24+
global: true,
25+
}
26+
}
27+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type { RedisClient, RedisConfig } from '@142vip/redis'
2+
import { RedisFactory } from '@142vip/redis'
3+
import { Injectable } from '@nestjs/common'
4+
5+
@Injectable()
6+
export class RedisService {
7+
private readonly config: RedisConfig
8+
private readonly client: RedisClient
9+
10+
constructor(config: RedisConfig) {
11+
this.config = config
12+
this.client = this.getClient()
13+
}
14+
15+
/**
16+
* 获取客户端
17+
*/
18+
public getClient(): RedisClient {
19+
return new RedisFactory().getClient(this.config)
20+
}
21+
22+
/**
23+
* 存储
24+
* - 单位:分钟
25+
* @param key
26+
* @param data
27+
* @param expiredTime
28+
*/
29+
public async setEx<T>(key: string, data: T, expiredTime: number): Promise<void> {
30+
const jsonData = JSON.stringify(data)
31+
await this.client.set(key, jsonData, 'EX', expiredTime)
32+
}
33+
34+
/**
35+
* 获取
36+
*/
37+
public async getEx<T>(key: string): Promise<T | null> {
38+
const jsonData = await this.client.get(key)
39+
40+
try {
41+
if (jsonData == null) {
42+
return null
43+
}
44+
return JSON.parse(jsonData) as T
45+
}
46+
catch {
47+
return null
48+
}
49+
}
50+
51+
/**
52+
* 删除
53+
* - 支持延迟双删
54+
*/
55+
public async del(key: string): Promise<void> {
56+
await this.client.del(key)
57+
setTimeout(async (): Promise<void> => {
58+
await this.client.del(key)
59+
}, 1000)
60+
}
61+
}

packages/nest-redis/src/core/redis.ts

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

packages/nest-redis/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
export const test = 1
1+
export * from './core/redis.constants'
2+
export * from './core/redis.decorator'
3+
export * from './core/redis.module'
4+
export * from './core/redis.service'

0 commit comments

Comments
 (0)