@edirect/redis 11.0.0 → 11.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/package.json +4 -4
- package/dist/src/constants.js +2 -0
- package/dist/src/index.js +5 -0
- package/dist/src/redis.client.module.js +22 -0
- package/dist/src/redis.interface.js +1 -0
- package/dist/src/redis.module.js +14 -0
- package/dist/src/redis.providers.js +57 -0
- package/dist/src/redis.service.js +127 -0
- package/package.json +4 -4
package/dist/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edirect/redis",
|
|
3
|
-
"version": "11.0.
|
|
3
|
+
"version": "11.0.6",
|
|
4
4
|
"packageScope": "@edirect",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.esm.js",
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
"!**/*.tsbuildinfo"
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@edirect/config": "
|
|
24
|
-
"@edirect/logger": "
|
|
23
|
+
"@edirect/config": "workspace:*",
|
|
24
|
+
"@edirect/logger": "workspace:*",
|
|
25
25
|
"@nestjs/common": "^11.1.6",
|
|
26
|
-
"
|
|
26
|
+
"redis": "^5.8.2"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@nx/rollup": "21.3.11"
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { REDIS_CLIENT_KEY, REDIS_SERVICE_KEY } from './constants';
|
|
2
|
+
export { RedisClientModule } from './redis.client.module';
|
|
3
|
+
export { RedisModule } from './redis.module';
|
|
4
|
+
export { createRedisClientProvider, RedisClientProvider, RedisProviders, } from './redis.providers';
|
|
5
|
+
export { RedisService } from './redis.service';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
var RedisClientModule_1;
|
|
2
|
+
import { __decorate } from "tslib";
|
|
3
|
+
import { Global, Module } from '@nestjs/common';
|
|
4
|
+
import { createRedisClientProvider, RedisProviders } from './redis.providers';
|
|
5
|
+
let RedisClientModule = RedisClientModule_1 = class RedisClientModule {
|
|
6
|
+
static forRoot(options, provide) {
|
|
7
|
+
const redisProviders = RedisProviders(provide);
|
|
8
|
+
return {
|
|
9
|
+
module: RedisClientModule_1,
|
|
10
|
+
providers: [
|
|
11
|
+
createRedisClientProvider(options),
|
|
12
|
+
...redisProviders
|
|
13
|
+
],
|
|
14
|
+
exports: [...redisProviders],
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
RedisClientModule = RedisClientModule_1 = __decorate([
|
|
19
|
+
Global(),
|
|
20
|
+
Module({})
|
|
21
|
+
], RedisClientModule);
|
|
22
|
+
export { RedisClientModule };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { Global, Module } from '@nestjs/common';
|
|
3
|
+
import { RedisClientProvider, RedisProviders } from './redis.providers';
|
|
4
|
+
const providers = RedisProviders();
|
|
5
|
+
let RedisModule = class RedisModule {
|
|
6
|
+
};
|
|
7
|
+
RedisModule = __decorate([
|
|
8
|
+
Global(),
|
|
9
|
+
Module({
|
|
10
|
+
providers: [RedisClientProvider, ...providers],
|
|
11
|
+
exports: [...providers],
|
|
12
|
+
})
|
|
13
|
+
], RedisModule);
|
|
14
|
+
export { RedisModule };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { ConfigService } from '@edirect/config';
|
|
2
|
+
import { createClient } from 'redis';
|
|
3
|
+
import { REDIS_CLIENT_KEY, REDIS_SERVICE_KEY } from './constants';
|
|
4
|
+
import { RedisService } from './redis.service';
|
|
5
|
+
import { LoggerService } from '@edirect/logger';
|
|
6
|
+
const defaultOptions = (configService, loggerService) => ({
|
|
7
|
+
url: `redis://${configService.get('REDIS_HOST')}:${configService.get('REDIS_PORT')}`,
|
|
8
|
+
...((configService.get('REDIS_PASS') ||
|
|
9
|
+
configService.get('REDIS_PASSWORD')) && {
|
|
10
|
+
password: configService.get('REDIS_PASS') ||
|
|
11
|
+
configService.get('REDIS_PASSWORD'),
|
|
12
|
+
}),
|
|
13
|
+
pingInterval: 1000,
|
|
14
|
+
socket: {
|
|
15
|
+
connectTimeout: 10000,
|
|
16
|
+
keepAlive: true,
|
|
17
|
+
noDelay: true,
|
|
18
|
+
reconnectStrategy(retries, cause) {
|
|
19
|
+
if (cause) {
|
|
20
|
+
loggerService.error('Error connecting to redis', JSON.stringify(cause));
|
|
21
|
+
}
|
|
22
|
+
return Math.min(retries * 50, 500);
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
export const createRedisClientProvider = (options) => ({
|
|
27
|
+
provide: REDIS_CLIENT_KEY,
|
|
28
|
+
useFactory: async (configService, loggerService) => {
|
|
29
|
+
const redisClient = createClient({
|
|
30
|
+
...defaultOptions(configService, loggerService),
|
|
31
|
+
...options,
|
|
32
|
+
});
|
|
33
|
+
await redisClient.connect();
|
|
34
|
+
return redisClient;
|
|
35
|
+
},
|
|
36
|
+
inject: [ConfigService, LoggerService],
|
|
37
|
+
});
|
|
38
|
+
export const RedisClientProvider = {
|
|
39
|
+
provide: REDIS_CLIENT_KEY,
|
|
40
|
+
useFactory: async (configService, loggerService) => {
|
|
41
|
+
const redisClient = createClient(defaultOptions(configService, loggerService));
|
|
42
|
+
await redisClient.connect();
|
|
43
|
+
return redisClient;
|
|
44
|
+
},
|
|
45
|
+
inject: [ConfigService, LoggerService],
|
|
46
|
+
};
|
|
47
|
+
export const RedisProviders = (provide) => [
|
|
48
|
+
{
|
|
49
|
+
provide: provide || REDIS_SERVICE_KEY,
|
|
50
|
+
useFactory: (redisClient, configService, loggerService) => new RedisService(redisClient, configService, loggerService),
|
|
51
|
+
inject: [REDIS_CLIENT_KEY, ConfigService, LoggerService],
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
provide: RedisService,
|
|
55
|
+
useExisting: provide || REDIS_SERVICE_KEY,
|
|
56
|
+
},
|
|
57
|
+
];
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import { Injectable, Inject } from '@nestjs/common';
|
|
3
|
+
import { ConfigService } from '@edirect/config';
|
|
4
|
+
import { LoggerService } from '@edirect/logger';
|
|
5
|
+
import { REDIS_CLIENT_KEY } from './constants';
|
|
6
|
+
let RedisService = class RedisService {
|
|
7
|
+
redisClient;
|
|
8
|
+
configService;
|
|
9
|
+
loggerService;
|
|
10
|
+
constructor(redisClient, configService, loggerService) {
|
|
11
|
+
this.redisClient = redisClient;
|
|
12
|
+
this.configService = configService;
|
|
13
|
+
this.loggerService = loggerService;
|
|
14
|
+
}
|
|
15
|
+
async get(key) {
|
|
16
|
+
try {
|
|
17
|
+
const ret = await this.redisClient.get(key);
|
|
18
|
+
return ret ? JSON.parse(ret) : null;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
this.loggerService.error('Error when executing GET command in redis', JSON.stringify(error));
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async mget(keys) {
|
|
26
|
+
try {
|
|
27
|
+
const ret = await this.redisClient.mGet(keys);
|
|
28
|
+
return ret ? ret.map((item) => (item ? JSON.parse(item) : null)) : [];
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
this.loggerService.error('Error when executing MGET command in redis', JSON.stringify(error));
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async hget(hash, field) {
|
|
36
|
+
try {
|
|
37
|
+
const ret = await this.redisClient.hGet(hash, field);
|
|
38
|
+
return ret ? JSON.parse(ret) : null;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
this.loggerService.error('Error when executing HGET command in redis', JSON.stringify(error));
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async hgetall(hash) {
|
|
46
|
+
try {
|
|
47
|
+
const ret = await this.redisClient.hGetAll(hash);
|
|
48
|
+
const result = {};
|
|
49
|
+
for (const [key, value] of Object.entries(ret)) {
|
|
50
|
+
result[key] = value ? JSON.parse(value) : null;
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
this.loggerService.error('Error when executing HGETALL command in redis', JSON.stringify(error));
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async set(key, data, ttl, ttlType = 'EX') {
|
|
60
|
+
try {
|
|
61
|
+
if (!ttl)
|
|
62
|
+
ttl = parseInt(this.configService.get('REDIS_TTL') || '0', 10);
|
|
63
|
+
if (data instanceof Object)
|
|
64
|
+
data = JSON.stringify(data);
|
|
65
|
+
if (ttl > 0) {
|
|
66
|
+
await this.redisClient.set(key, data, {
|
|
67
|
+
expiration: {
|
|
68
|
+
type: ttlType,
|
|
69
|
+
value: ttl,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
await this.redisClient.set(key, data);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
this.loggerService.error(`Error when executing SET${ttlType === 'EX' ? '' : ttlType} command in redis`, JSON.stringify(error));
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async del(key) {
|
|
83
|
+
try {
|
|
84
|
+
await this.redisClient.del(key);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
this.loggerService.error('Error when executing DEL command in redis', JSON.stringify(error));
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async setnx(key, data) {
|
|
92
|
+
try {
|
|
93
|
+
if (data instanceof Object)
|
|
94
|
+
data = JSON.stringify(data);
|
|
95
|
+
return (await this.redisClient.setNX(key, data)) === 1;
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
this.loggerService.error('Error when executing SETNX command in redis', JSON.stringify(error));
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async asyncPing() {
|
|
103
|
+
return (await this.redisClient.ping()) === 'PONG';
|
|
104
|
+
}
|
|
105
|
+
async keys(key) {
|
|
106
|
+
try {
|
|
107
|
+
const ret = await this.redisClient.keys(key);
|
|
108
|
+
if (Array.isArray(ret)) {
|
|
109
|
+
return ret.map((item) => (item ? JSON.parse(item) : null));
|
|
110
|
+
}
|
|
111
|
+
return ret ? JSON.parse(ret) : [];
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
this.loggerService.error('Error when executing KEYS command in redis', JSON.stringify(error));
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
RedisService = __decorate([
|
|
120
|
+
Injectable(),
|
|
121
|
+
__param(0, Inject(REDIS_CLIENT_KEY)),
|
|
122
|
+
__param(1, Inject(ConfigService)),
|
|
123
|
+
__param(2, Inject(LoggerService)),
|
|
124
|
+
__metadata("design:paramtypes", [Object, ConfigService,
|
|
125
|
+
LoggerService])
|
|
126
|
+
], RedisService);
|
|
127
|
+
export { RedisService };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edirect/redis",
|
|
3
|
-
"version": "11.0.
|
|
3
|
+
"version": "11.0.7",
|
|
4
4
|
"packageScope": "@edirect",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.esm.js",
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@nestjs/common": "^11.1.6",
|
|
24
|
-
"
|
|
25
|
-
"@edirect/config": "11.0.
|
|
26
|
-
"@edirect/logger": "11.0.
|
|
24
|
+
"redis": "^5.8.2",
|
|
25
|
+
"@edirect/config": "11.0.7",
|
|
26
|
+
"@edirect/logger": "11.0.7"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@nx/rollup": "21.3.11"
|