plac-micro-common 1.3.65 → 1.3.67
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/constants/redis.constant.d.ts +6 -0
- package/dist/constants/redis.constant.js +6 -0
- package/dist/libs/redis/redis.constant.d.ts +1 -13
- package/dist/libs/redis/redis.constant.js +2 -15
- package/dist/libs/redis/redis.module.js +16 -73
- package/dist/libs/redis/redis.service.d.ts +9 -13
- package/dist/libs/redis/redis.service.js +28 -42
- package/package.json +1 -1
|
@@ -20,6 +20,12 @@ exports.REDIS_CACHE_KEYS = {
|
|
|
20
20
|
},
|
|
21
21
|
};
|
|
22
22
|
exports.REDIS_CACHE_TTL = {
|
|
23
|
+
AppInfo: {
|
|
24
|
+
Default: 60 * 60 * 12, // 12 hour
|
|
25
|
+
},
|
|
26
|
+
AppClientInfo: {
|
|
27
|
+
Default: 60 * 60 * 12, // 12 hour
|
|
28
|
+
},
|
|
23
29
|
Dropdown: 60 * 60 * 6, // 6 hours
|
|
24
30
|
UserInfo: 60 * 15, // 15 minutes
|
|
25
31
|
};
|
|
@@ -1,16 +1,4 @@
|
|
|
1
|
-
export declare const
|
|
2
|
-
/**
|
|
3
|
-
* Named client token helper (if you want to inject a specific client directly)
|
|
4
|
-
* Example: @Inject(getRedisClientToken('geo')) private geoRedis: Redis
|
|
5
|
-
*/
|
|
6
|
-
export declare const getRedisClientToken: (name: string) => string;
|
|
7
|
-
/**
|
|
8
|
-
* Backward compatible tokens:
|
|
9
|
-
* - REDIS_CLIENT is the "main" redis client
|
|
10
|
-
* - REDIS_GEO_CLIENT is the "geo" redis client
|
|
11
|
-
*/
|
|
12
|
-
export declare const REDIS_CLIENT: string;
|
|
13
|
-
export declare const REDIS_GEO_CLIENT: string;
|
|
1
|
+
export declare const REDIS_CLIENT = "REDIS_CLIENT";
|
|
14
2
|
export type RedisModuleOptions = {
|
|
15
3
|
url?: string;
|
|
16
4
|
host?: string;
|
|
@@ -1,17 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.
|
|
5
|
-
/**
|
|
6
|
-
* Named client token helper (if you want to inject a specific client directly)
|
|
7
|
-
* Example: @Inject(getRedisClientToken('geo')) private geoRedis: Redis
|
|
8
|
-
*/
|
|
9
|
-
const getRedisClientToken = (name) => `REDIS_CLIENT:${name}`;
|
|
10
|
-
exports.getRedisClientToken = getRedisClientToken;
|
|
11
|
-
/**
|
|
12
|
-
* Backward compatible tokens:
|
|
13
|
-
* - REDIS_CLIENT is the "main" redis client
|
|
14
|
-
* - REDIS_GEO_CLIENT is the "geo" redis client
|
|
15
|
-
*/
|
|
16
|
-
exports.REDIS_CLIENT = (0, exports.getRedisClientToken)("main");
|
|
17
|
-
exports.REDIS_GEO_CLIENT = (0, exports.getRedisClientToken)("geo");
|
|
3
|
+
exports.REDIS_CLIENT = void 0;
|
|
4
|
+
exports.REDIS_CLIENT = "REDIS_CLIENT";
|
|
@@ -16,92 +16,35 @@ const config_1 = require("@nestjs/config");
|
|
|
16
16
|
const ioredis_1 = __importDefault(require("ioredis"));
|
|
17
17
|
const redis_constant_1 = require("./redis.constant");
|
|
18
18
|
const redis_service_1 = require("./redis.service");
|
|
19
|
-
function parseNames(v) {
|
|
20
|
-
// default includes geo for backward compatibility with your existing code
|
|
21
|
-
const raw = (v ?? "main,geo")
|
|
22
|
-
.split(",")
|
|
23
|
-
.map((s) => s.trim())
|
|
24
|
-
.filter(Boolean);
|
|
25
|
-
// ensure unique
|
|
26
|
-
return Array.from(new Set(raw));
|
|
27
|
-
}
|
|
28
|
-
function upper(name) {
|
|
29
|
-
return name.replace(/[^a-zA-Z0-9]/g, "_").toUpperCase();
|
|
30
|
-
}
|
|
31
19
|
let RedisModule = RedisModule_1 = class RedisModule {
|
|
32
20
|
static forRootAsync() {
|
|
33
21
|
return {
|
|
34
22
|
module: RedisModule_1,
|
|
35
23
|
imports: [config_1.ConfigModule],
|
|
36
24
|
providers: [
|
|
37
|
-
// 1) Build ALL clients from env and store as a map
|
|
38
25
|
{
|
|
39
|
-
provide: redis_constant_1.
|
|
26
|
+
provide: redis_constant_1.REDIS_CLIENT,
|
|
40
27
|
inject: [config_1.ConfigService],
|
|
41
28
|
useFactory: (config) => {
|
|
42
|
-
const
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const db = config.get(`REDIS_${N}_DB`) ?? baseDb;
|
|
57
|
-
const keyPrefix = config.get(`REDIS_${N}_KEY_PREFIX`) ?? baseKeyPrefix;
|
|
58
|
-
const client = url
|
|
59
|
-
? new ioredis_1.default(url, { keyPrefix })
|
|
60
|
-
: new ioredis_1.default({
|
|
61
|
-
host,
|
|
62
|
-
port,
|
|
63
|
-
password,
|
|
64
|
-
db,
|
|
65
|
-
keyPrefix,
|
|
66
|
-
});
|
|
67
|
-
client.on("error", (err) => {
|
|
68
|
-
// eslint-disable-next-line no-console
|
|
69
|
-
console.error(`[redis:${name}] error:`, err?.message || err);
|
|
70
|
-
});
|
|
71
|
-
map[name] = client;
|
|
72
|
-
}
|
|
73
|
-
return map;
|
|
29
|
+
const url = config.get("REDIS_URL");
|
|
30
|
+
const host = config.get("REDIS_HOST");
|
|
31
|
+
const port = config.get("REDIS_PORT");
|
|
32
|
+
const password = config.get("REDIS_PASSWORD") || undefined;
|
|
33
|
+
const db = config.get("REDIS_DB") ?? 0;
|
|
34
|
+
const keyPrefix = config.get("REDIS_KEY_PREFIX") || undefined;
|
|
35
|
+
const client = url
|
|
36
|
+
? new ioredis_1.default(url, { keyPrefix })
|
|
37
|
+
: new ioredis_1.default({ host, port, password, db, keyPrefix });
|
|
38
|
+
client.on("error", (err) => {
|
|
39
|
+
// eslint-disable-next-line no-console
|
|
40
|
+
console.error(`[redis] error:`, err?.message || err);
|
|
41
|
+
});
|
|
42
|
+
return client;
|
|
74
43
|
},
|
|
75
44
|
},
|
|
76
|
-
// 2) Provide each named token for DI (optional but useful)
|
|
77
|
-
// e.g. @Inject(getRedisClientToken('geo')) geoRedis: Redis
|
|
78
|
-
{
|
|
79
|
-
provide: (0, redis_constant_1.getRedisClientToken)("main"),
|
|
80
|
-
inject: [redis_constant_1.REDIS_CLIENTS],
|
|
81
|
-
useFactory: (clients) => {
|
|
82
|
-
if (!clients.main)
|
|
83
|
-
throw new Error(`[redis] missing client "main" (check REDIS_NAMES)`);
|
|
84
|
-
return clients.main;
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
{
|
|
88
|
-
provide: (0, redis_constant_1.getRedisClientToken)("geo"),
|
|
89
|
-
inject: [redis_constant_1.REDIS_CLIENTS],
|
|
90
|
-
useFactory: (clients) => {
|
|
91
|
-
if (!clients.geo)
|
|
92
|
-
throw new Error(`[redis] missing client "geo" (check REDIS_NAMES)`);
|
|
93
|
-
return clients.geo;
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
// 3) Service uses the map; you can use any client name dynamically
|
|
97
|
-
redis_service_1.RedisService,
|
|
98
|
-
],
|
|
99
|
-
exports: [
|
|
100
|
-
redis_constant_1.REDIS_CLIENTS,
|
|
101
45
|
redis_service_1.RedisService,
|
|
102
|
-
(0, redis_constant_1.getRedisClientToken)("main"),
|
|
103
|
-
(0, redis_constant_1.getRedisClientToken)("geo"),
|
|
104
46
|
],
|
|
47
|
+
exports: [redis_constant_1.REDIS_CLIENT, redis_service_1.RedisService],
|
|
105
48
|
};
|
|
106
49
|
}
|
|
107
50
|
};
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
import { OnModuleDestroy } from "@nestjs/common";
|
|
2
2
|
import { Redis } from "ioredis";
|
|
3
|
-
type RedisClientMap = Record<string, Redis>;
|
|
4
3
|
export declare class RedisService implements OnModuleDestroy {
|
|
5
|
-
private readonly
|
|
6
|
-
constructor(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
del(key: string
|
|
10
|
-
exists(key: string
|
|
11
|
-
client(
|
|
12
|
-
getJson<T>(key: string
|
|
13
|
-
setJson(key: string, value: unknown, ttlSeconds?: number
|
|
14
|
-
getGeoJson<T>(key: string): Promise<T | null>;
|
|
15
|
-
setGeoJson(key: string, value: unknown, ttlSeconds?: number): Promise<"OK">;
|
|
4
|
+
private readonly redis;
|
|
5
|
+
constructor(redis: Redis);
|
|
6
|
+
get(key: string): Promise<string | null>;
|
|
7
|
+
set(key: string, value: string, ttlSeconds?: number): Promise<"OK">;
|
|
8
|
+
del(key: string): Promise<number>;
|
|
9
|
+
exists(key: string): Promise<number>;
|
|
10
|
+
client(): Redis;
|
|
11
|
+
getJson<T>(key: string): Promise<T | null>;
|
|
12
|
+
setJson(key: string, value: unknown, ttlSeconds?: number): Promise<"OK">;
|
|
16
13
|
onModuleDestroy(): Promise<void>;
|
|
17
14
|
}
|
|
18
|
-
export {};
|
|
@@ -14,35 +14,33 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.RedisService = void 0;
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
|
+
const ioredis_1 = require("ioredis");
|
|
17
18
|
const redis_constant_1 = require("./redis.constant");
|
|
18
19
|
let RedisService = class RedisService {
|
|
19
|
-
constructor(
|
|
20
|
-
this.
|
|
21
|
-
}
|
|
22
|
-
pick(name = "main") {
|
|
23
|
-
const client = this.clients[name];
|
|
24
|
-
if (!client) {
|
|
25
|
-
const available = Object.keys(this.clients).join(", ");
|
|
26
|
-
throw new Error(`[redis] client "${name}" not found. Available: ${available}`);
|
|
27
|
-
}
|
|
28
|
-
return client;
|
|
20
|
+
constructor(redis) {
|
|
21
|
+
this.redis = redis;
|
|
29
22
|
}
|
|
30
23
|
// -------------------- RAW --------------------
|
|
31
|
-
get(key
|
|
32
|
-
return this.
|
|
24
|
+
get(key) {
|
|
25
|
+
return this.redis.get(key);
|
|
33
26
|
}
|
|
34
|
-
|
|
35
|
-
|
|
27
|
+
set(key, value, ttlSeconds) {
|
|
28
|
+
if (ttlSeconds && ttlSeconds > 0)
|
|
29
|
+
return this.redis.set(key, value, "EX", ttlSeconds);
|
|
30
|
+
return this.redis.set(key, value);
|
|
36
31
|
}
|
|
37
|
-
|
|
38
|
-
return this.
|
|
32
|
+
del(key) {
|
|
33
|
+
return this.redis.del(key);
|
|
39
34
|
}
|
|
40
|
-
|
|
41
|
-
return this.
|
|
35
|
+
exists(key) {
|
|
36
|
+
return this.redis.exists(key);
|
|
37
|
+
}
|
|
38
|
+
client() {
|
|
39
|
+
return this.redis;
|
|
42
40
|
}
|
|
43
41
|
// -------------------- JSON --------------------
|
|
44
|
-
async getJson(key
|
|
45
|
-
const val = await this.
|
|
42
|
+
async getJson(key) {
|
|
43
|
+
const val = await this.redis.get(key);
|
|
46
44
|
if (!val)
|
|
47
45
|
return null;
|
|
48
46
|
try {
|
|
@@ -52,36 +50,24 @@ let RedisService = class RedisService {
|
|
|
52
50
|
return null;
|
|
53
51
|
}
|
|
54
52
|
}
|
|
55
|
-
async setJson(key, value, ttlSeconds
|
|
53
|
+
async setJson(key, value, ttlSeconds) {
|
|
56
54
|
const payload = JSON.stringify(value);
|
|
57
|
-
const c = this.pick(clientName);
|
|
58
55
|
if (ttlSeconds && ttlSeconds > 0)
|
|
59
|
-
return
|
|
60
|
-
return
|
|
61
|
-
}
|
|
62
|
-
// Backward-compat convenience (optional)
|
|
63
|
-
async getGeoJson(key) {
|
|
64
|
-
return this.getJson(key, "geo");
|
|
65
|
-
}
|
|
66
|
-
async setGeoJson(key, value, ttlSeconds) {
|
|
67
|
-
return this.setJson(key, value, ttlSeconds, "geo");
|
|
56
|
+
return this.redis.set(key, payload, "EX", ttlSeconds);
|
|
57
|
+
return this.redis.set(key, payload);
|
|
68
58
|
}
|
|
69
59
|
async onModuleDestroy() {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
catch {
|
|
77
|
-
c.disconnect();
|
|
78
|
-
}
|
|
60
|
+
try {
|
|
61
|
+
await this.redis.quit();
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
this.redis.disconnect();
|
|
79
65
|
}
|
|
80
66
|
}
|
|
81
67
|
};
|
|
82
68
|
exports.RedisService = RedisService;
|
|
83
69
|
exports.RedisService = RedisService = __decorate([
|
|
84
70
|
(0, common_1.Injectable)(),
|
|
85
|
-
__param(0, (0, common_1.Inject)(redis_constant_1.
|
|
86
|
-
__metadata("design:paramtypes", [
|
|
71
|
+
__param(0, (0, common_1.Inject)(redis_constant_1.REDIS_CLIENT)),
|
|
72
|
+
__metadata("design:paramtypes", [ioredis_1.Redis])
|
|
87
73
|
], RedisService);
|