@jintianxiayu/cache-decorator 1.0.0 → 1.0.2
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/CHANGELOG.md +25 -11
- package/README.md +364 -270
- package/dist/core/cache-error.d.ts +75 -0
- package/dist/core/cache-error.d.ts.map +1 -0
- package/dist/core/cache-error.js +168 -0
- package/dist/core/cache-error.js.map +1 -0
- package/dist/core/cache-logger.d.ts +3 -2
- package/dist/core/cache-logger.d.ts.map +1 -1
- package/dist/core/cache-logger.js +2 -0
- package/dist/core/cache-logger.js.map +1 -1
- package/dist/decorators/cache-evict.d.ts.map +1 -1
- package/dist/decorators/cache-evict.js +15 -10
- package/dist/decorators/cache-evict.js.map +1 -1
- package/dist/decorators/cache.d.ts +6 -0
- package/dist/decorators/cache.d.ts.map +1 -1
- package/dist/decorators/cache.js +113 -36
- package/dist/decorators/cache.js.map +1 -1
- package/jest.config.js +11 -11
- package/package.json +1 -1
- package/src/adapters/ioredis-cache-client.ts +89 -89
- package/src/adapters/node-redis-cache-client.ts +95 -95
- package/src/adapters/redis-key-prefix.ts +38 -38
- package/src/core/cache-error.ts +233 -0
- package/src/core/cache-logger.ts +116 -105
- package/src/core/key-builder.ts +33 -33
- package/src/core/native-cache.ts +55 -55
- package/src/core/pending-cache.ts +29 -29
- package/src/core/redis-cache-client.ts +160 -160
- package/src/core/redis-cache.ts +104 -104
- package/src/decorators/cache-evict.ts +137 -129
- package/src/decorators/cache.ts +323 -203
- package/src/index.ts +11 -11
- package/test/cache-evict-logging.test.ts +398 -362
- package/test/cache-logger.integration.test.ts +159 -129
- package/test/cache-logger.test.ts +156 -153
- package/test/cache-logging.test.ts +929 -544
- package/test/cache.test.ts +1017 -255
- package/test/fixtures/cache-logger-child.mjs +108 -108
- package/test/fixtures/cache-provider-failure-child.mjs +70 -0
- package/test/helpers/legacy-redis-cache.ts +41 -22
- package/test/helpers/package-consumer.ts +231 -231
- package/test/helpers/redis-fixture.ts +142 -142
- package/test/ioredis-cache-client.test.ts +143 -143
- package/test/legacy-redis-cache.test.ts +51 -0
- package/test/native-cache.test.ts +77 -77
- package/test/node-redis-cache-client.test.ts +149 -149
- package/test/pending-cache.test.ts +69 -69
- package/test/redis-cache-client-lifecycle.test.ts +112 -112
- package/test/redis-cache-client-types.test.ts +184 -184
- package/test/redis-cache-client.integration.test.ts +355 -327
- package/test/redis-cache-decorator.test.ts +601 -201
- package/test/redis-cache-provider.test.ts +269 -269
- package/test/type-contract/contract.ts +119 -64
- package/test/type-contract/tsconfig.json +12 -12
- package/tsconfig.json +8 -8
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../src/decorators/cache.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../src/decorators/cache.ts"],"names":[],"mappings":";;;AAuPA,sBAyEC;AAhUD,4BAA0B;AAC1B,qDAQ6B;AAC7B,uDAA+F;AAE/F,6EAAwE;AACxE,qDAAiD;AACjD,yDAAqD;AAsFrD,MAAM,YAAY,GAAG,IAAI,4BAAY,EAAE,CAAC;AAExC;;;;;GAKG;AACH,SAAS,oBAAoB,CACzB,YAAgC,EAChC,UAA2B;IAE3B,IAAI,CAAC;QACD,OAAO,+CAAqB,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;IACnD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,IAAA,4BAAa,EAAC,wBAAwB,EAAE,EAAE,GAAG,UAAU,EAAE,SAAS,EAAE,qBAAqB,EAAE,KAAK,EAAE,CAAC,CAAC;QACpG,OAAO,SAAS,CAAC;IACrB,CAAC;AACL,CAAC;AAED;;;;GAIG;AACH,SAAS,kBAAkB,CAAI,OAA6B;IACxD,IAAI,SAA+B,CAAC;IACpC,IAAI,CAAC;QACD,SAAS,GAAG,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,OAAO,CAA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AC;gBACD,IAAA,4BAAa,EAAC,YAAY,EAAE,UAAU,CAAC,CAAC;gBAExC,IAAI,MAAS,CAAC;gBACd,IAAI,CAAC;oBACD,MAAM,GAAG,CAAC,MAAM,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAM,CAAC;gBAC3D,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACb,mBAAmB,CAAC,EAAE,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,CAAC,CAAC;oBAC5E,MAAM,KAAK,CAAC;gBAChB,CAAC;gBACD,kBAAkB,CAAC;oBACf,QAAQ;oBACR,QAAQ;oBACR,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE;oBACxB,SAAS,EAAE,OAAO;oBAClB,GAAG,EAAE,OAAO,EAAE,GAAG;oBACjB,UAAU;iBACb,CAAC,CAAC;gBACH,OAAO,MAAM,CAAC;YAClB,CAAC,CAAC,EAAE,CAAC;YAEL,YAAY,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;YACpC,OAAO,OAAO,CAAC;QACnB,CAAC,CAAC;IACN,CAAC,CAAC;AACN,CAAC;AAED,2EAAwE;AAA/D,gIAAA,qBAAqB,OAAA"}
|
package/jest.config.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
preset: 'ts-jest',
|
|
3
|
-
testEnvironment: 'node',
|
|
4
|
-
roots: ['<rootDir>'],
|
|
5
|
-
testMatch: ['**/test/**/*.test.ts'],
|
|
6
|
-
moduleFileExtensions: ['ts', 'js', 'json'],
|
|
7
|
-
collectCoverageFrom: ['src/**/*.ts', '!src/**/*.d.ts'],
|
|
8
|
-
transform: {
|
|
9
|
-
'^.+\\.ts$': ['ts-jest', { tsconfig: '<rootDir>/../../tsconfig.test.json' }],
|
|
10
|
-
},
|
|
11
|
-
};
|
|
1
|
+
module.exports = {
|
|
2
|
+
preset: 'ts-jest',
|
|
3
|
+
testEnvironment: 'node',
|
|
4
|
+
roots: ['<rootDir>'],
|
|
5
|
+
testMatch: ['**/test/**/*.test.ts'],
|
|
6
|
+
moduleFileExtensions: ['ts', 'js', 'json'],
|
|
7
|
+
collectCoverageFrom: ['src/**/*.ts', '!src/**/*.d.ts'],
|
|
8
|
+
transform: {
|
|
9
|
+
'^.+\\.ts$': ['ts-jest', { tsconfig: '<rootDir>/../../tsconfig.test.json' }],
|
|
10
|
+
},
|
|
11
|
+
};
|
package/package.json
CHANGED
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
import { IoredisCacheClientSource, RedisCacheClient } from '../core/redis-cache-client';
|
|
2
|
-
import { createRedisScanPattern, stripRedisKeyPrefix } from './redis-key-prefix';
|
|
3
|
-
|
|
4
|
-
function normalizeGetResponse(response: unknown): string | null {
|
|
5
|
-
if (typeof response === 'string' || response === null) {
|
|
6
|
-
return response;
|
|
7
|
-
}
|
|
8
|
-
throw new TypeError('Expected Redis GET to return a string or null');
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function validateOkResponse(command: string, response: unknown): void {
|
|
12
|
-
if (response !== 'OK') {
|
|
13
|
-
throw new TypeError(`Expected Redis ${command} to return "OK"`);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function validateDeleteResponse(response: unknown): void {
|
|
18
|
-
if (typeof response !== 'number' || !Number.isInteger(response) || response < 0) {
|
|
19
|
-
throw new TypeError('Expected Redis DEL to return a non-negative integer');
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function normalizeScanResponse(
|
|
24
|
-
response: unknown,
|
|
25
|
-
keyPrefix: string
|
|
26
|
-
): { readonly cursor: string; readonly keys: readonly string[] } {
|
|
27
|
-
if (
|
|
28
|
-
!Array.isArray(response) ||
|
|
29
|
-
response.length !== 2 ||
|
|
30
|
-
typeof response[0] !== 'string' ||
|
|
31
|
-
!Array.isArray(response[1]) ||
|
|
32
|
-
!response[1].every((key: unknown) => typeof key === 'string')
|
|
33
|
-
) {
|
|
34
|
-
throw new TypeError('Expected ioredis SCAN to return a [cursor, keys] tuple');
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
cursor: response[0],
|
|
38
|
-
keys: response[1].map((key: string) => stripRedisKeyPrefix(keyPrefix, key)),
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 将调用方的 ioredis 连接适配为客户端无关的缓存命令,不创建或关闭连接。
|
|
44
|
-
* @param client 使用默认字符串响应映射的 ioredis 普通客户端。
|
|
45
|
-
* @returns 复用该连接并恢复逻辑 key 的 Redis 缓存客户端。
|
|
46
|
-
* @throws 返回对象的命令方法传播客户端错误;非标准响应拒绝为 TypeError。
|
|
47
|
-
*/
|
|
48
|
-
export function createIoredisCacheClient(client: IoredisCacheClientSource): RedisCacheClient {
|
|
49
|
-
const keyPrefix = client.options.keyPrefix ?? '';
|
|
50
|
-
return {
|
|
51
|
-
/** @inheritdoc */
|
|
52
|
-
async get(key: string): Promise<string | null> {
|
|
53
|
-
return normalizeGetResponse(await client.get(key));
|
|
54
|
-
},
|
|
55
|
-
/** @inheritdoc */
|
|
56
|
-
async set({ key, value, ttlSeconds }): Promise<void> {
|
|
57
|
-
if (ttlSeconds === undefined) {
|
|
58
|
-
validateOkResponse('SET', await client.set(key, value));
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
validateOkResponse('SETEX', await client.setex(key, ttlSeconds, value));
|
|
62
|
-
},
|
|
63
|
-
/** @inheritdoc */
|
|
64
|
-
async deleteMany(keys: readonly string[]): Promise<void> {
|
|
65
|
-
if (keys.length === 0) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
validateDeleteResponse(await client.del(...keys));
|
|
69
|
-
},
|
|
70
|
-
/** @inheritdoc */
|
|
71
|
-
async scan({ cursor, pattern, count }): Promise<{
|
|
72
|
-
readonly cursor: string;
|
|
73
|
-
readonly keys: readonly string[];
|
|
74
|
-
}> {
|
|
75
|
-
const response = await client.scan(
|
|
76
|
-
cursor,
|
|
77
|
-
'MATCH',
|
|
78
|
-
createRedisScanPattern(keyPrefix, pattern),
|
|
79
|
-
'COUNT',
|
|
80
|
-
count
|
|
81
|
-
);
|
|
82
|
-
return normalizeScanResponse(response, keyPrefix);
|
|
83
|
-
},
|
|
84
|
-
/** @inheritdoc */
|
|
85
|
-
async flushDatabase(): Promise<void> {
|
|
86
|
-
validateOkResponse('FLUSHDB', await client.flushdb());
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
}
|
|
1
|
+
import { IoredisCacheClientSource, RedisCacheClient } from '../core/redis-cache-client';
|
|
2
|
+
import { createRedisScanPattern, stripRedisKeyPrefix } from './redis-key-prefix';
|
|
3
|
+
|
|
4
|
+
function normalizeGetResponse(response: unknown): string | null {
|
|
5
|
+
if (typeof response === 'string' || response === null) {
|
|
6
|
+
return response;
|
|
7
|
+
}
|
|
8
|
+
throw new TypeError('Expected Redis GET to return a string or null');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function validateOkResponse(command: string, response: unknown): void {
|
|
12
|
+
if (response !== 'OK') {
|
|
13
|
+
throw new TypeError(`Expected Redis ${command} to return "OK"`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function validateDeleteResponse(response: unknown): void {
|
|
18
|
+
if (typeof response !== 'number' || !Number.isInteger(response) || response < 0) {
|
|
19
|
+
throw new TypeError('Expected Redis DEL to return a non-negative integer');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function normalizeScanResponse(
|
|
24
|
+
response: unknown,
|
|
25
|
+
keyPrefix: string
|
|
26
|
+
): { readonly cursor: string; readonly keys: readonly string[] } {
|
|
27
|
+
if (
|
|
28
|
+
!Array.isArray(response) ||
|
|
29
|
+
response.length !== 2 ||
|
|
30
|
+
typeof response[0] !== 'string' ||
|
|
31
|
+
!Array.isArray(response[1]) ||
|
|
32
|
+
!response[1].every((key: unknown) => typeof key === 'string')
|
|
33
|
+
) {
|
|
34
|
+
throw new TypeError('Expected ioredis SCAN to return a [cursor, keys] tuple');
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
cursor: response[0],
|
|
38
|
+
keys: response[1].map((key: string) => stripRedisKeyPrefix(keyPrefix, key)),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 将调用方的 ioredis 连接适配为客户端无关的缓存命令,不创建或关闭连接。
|
|
44
|
+
* @param client 使用默认字符串响应映射的 ioredis 普通客户端。
|
|
45
|
+
* @returns 复用该连接并恢复逻辑 key 的 Redis 缓存客户端。
|
|
46
|
+
* @throws 返回对象的命令方法传播客户端错误;非标准响应拒绝为 TypeError。
|
|
47
|
+
*/
|
|
48
|
+
export function createIoredisCacheClient(client: IoredisCacheClientSource): RedisCacheClient {
|
|
49
|
+
const keyPrefix = client.options.keyPrefix ?? '';
|
|
50
|
+
return {
|
|
51
|
+
/** @inheritdoc */
|
|
52
|
+
async get(key: string): Promise<string | null> {
|
|
53
|
+
return normalizeGetResponse(await client.get(key));
|
|
54
|
+
},
|
|
55
|
+
/** @inheritdoc */
|
|
56
|
+
async set({ key, value, ttlSeconds }): Promise<void> {
|
|
57
|
+
if (ttlSeconds === undefined) {
|
|
58
|
+
validateOkResponse('SET', await client.set(key, value));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
validateOkResponse('SETEX', await client.setex(key, ttlSeconds, value));
|
|
62
|
+
},
|
|
63
|
+
/** @inheritdoc */
|
|
64
|
+
async deleteMany(keys: readonly string[]): Promise<void> {
|
|
65
|
+
if (keys.length === 0) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
validateDeleteResponse(await client.del(...keys));
|
|
69
|
+
},
|
|
70
|
+
/** @inheritdoc */
|
|
71
|
+
async scan({ cursor, pattern, count }): Promise<{
|
|
72
|
+
readonly cursor: string;
|
|
73
|
+
readonly keys: readonly string[];
|
|
74
|
+
}> {
|
|
75
|
+
const response = await client.scan(
|
|
76
|
+
cursor,
|
|
77
|
+
'MATCH',
|
|
78
|
+
createRedisScanPattern(keyPrefix, pattern),
|
|
79
|
+
'COUNT',
|
|
80
|
+
count
|
|
81
|
+
);
|
|
82
|
+
return normalizeScanResponse(response, keyPrefix);
|
|
83
|
+
},
|
|
84
|
+
/** @inheritdoc */
|
|
85
|
+
async flushDatabase(): Promise<void> {
|
|
86
|
+
validateOkResponse('FLUSHDB', await client.flushdb());
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
import { NodeRedisCacheClientOptions, NodeRedisCacheClientSource, RedisCacheClient } from '../core/redis-cache-client';
|
|
2
|
-
import { createRedisScanPattern, stripRedisKeyPrefix } from './redis-key-prefix';
|
|
3
|
-
|
|
4
|
-
function normalizeGetResponse(response: unknown): string | null {
|
|
5
|
-
if (typeof response === 'string' || response === null) {
|
|
6
|
-
return response;
|
|
7
|
-
}
|
|
8
|
-
throw new TypeError('Expected Redis GET to return a string or null');
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function validateOkResponse(command: string, response: unknown): void {
|
|
12
|
-
if (response !== 'OK') {
|
|
13
|
-
throw new TypeError(`Expected Redis ${command} to return "OK"`);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function validateDeleteResponse(response: unknown): void {
|
|
18
|
-
if (typeof response !== 'number' || !Number.isInteger(response) || response < 0) {
|
|
19
|
-
throw new TypeError('Expected Redis DEL to return a non-negative integer');
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
24
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function normalizeScanResponse(
|
|
28
|
-
response: unknown,
|
|
29
|
-
keyPrefix: string
|
|
30
|
-
): { readonly cursor: string; readonly keys: readonly string[] } {
|
|
31
|
-
if (
|
|
32
|
-
!isRecord(response) ||
|
|
33
|
-
typeof response.cursor !== 'string' ||
|
|
34
|
-
!Array.isArray(response.keys) ||
|
|
35
|
-
!response.keys.every((key: unknown) => typeof key === 'string')
|
|
36
|
-
) {
|
|
37
|
-
throw new TypeError('Expected node-redis SCAN to return a { cursor, keys } object');
|
|
38
|
-
}
|
|
39
|
-
return {
|
|
40
|
-
cursor: response.cursor,
|
|
41
|
-
keys: response.keys.map((key: string) => stripRedisKeyPrefix(keyPrefix, key)),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 将调用方的 node-redis 连接适配为客户端无关的缓存命令,不创建或关闭连接。
|
|
47
|
-
* @param client 使用默认字符串响应映射的 node-redis 普通客户端。
|
|
48
|
-
* @param options 可选的字面 key 前缀;缺省时不改写逻辑 key。
|
|
49
|
-
* @returns 复用该连接并统一物理 key 前缀的 Redis 缓存客户端。
|
|
50
|
-
* @throws 返回对象的命令方法传播客户端错误;非标准响应拒绝为 TypeError。
|
|
51
|
-
*/
|
|
52
|
-
export function createNodeRedisCacheClient(
|
|
53
|
-
client: NodeRedisCacheClientSource,
|
|
54
|
-
options?: NodeRedisCacheClientOptions
|
|
55
|
-
): RedisCacheClient {
|
|
56
|
-
const keyPrefix = options?.keyPrefix ?? '';
|
|
57
|
-
const createPhysicalKey = (key: string): string => `${keyPrefix}${key}`;
|
|
58
|
-
return {
|
|
59
|
-
/** @inheritdoc */
|
|
60
|
-
async get(key: string): Promise<string | null> {
|
|
61
|
-
return normalizeGetResponse(await client.get(createPhysicalKey(key)));
|
|
62
|
-
},
|
|
63
|
-
/** @inheritdoc */
|
|
64
|
-
async set({ key, value, ttlSeconds }): Promise<void> {
|
|
65
|
-
const physicalKey = createPhysicalKey(key);
|
|
66
|
-
if (ttlSeconds === undefined) {
|
|
67
|
-
validateOkResponse('SET', await client.set(physicalKey, value));
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
validateOkResponse('SETEX', await client.setEx(physicalKey, ttlSeconds, value));
|
|
71
|
-
},
|
|
72
|
-
/** @inheritdoc */
|
|
73
|
-
async deleteMany(keys: readonly string[]): Promise<void> {
|
|
74
|
-
if (keys.length === 0) {
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
validateDeleteResponse(await client.del(keys.map(createPhysicalKey)));
|
|
78
|
-
},
|
|
79
|
-
/** @inheritdoc */
|
|
80
|
-
async scan({ cursor, pattern, count }): Promise<{
|
|
81
|
-
readonly cursor: string;
|
|
82
|
-
readonly keys: readonly string[];
|
|
83
|
-
}> {
|
|
84
|
-
const response = await client.scan(cursor, {
|
|
85
|
-
MATCH: createRedisScanPattern(keyPrefix, pattern),
|
|
86
|
-
COUNT: count,
|
|
87
|
-
});
|
|
88
|
-
return normalizeScanResponse(response, keyPrefix);
|
|
89
|
-
},
|
|
90
|
-
/** @inheritdoc */
|
|
91
|
-
async flushDatabase(): Promise<void> {
|
|
92
|
-
validateOkResponse('FLUSHDB', await client.flushDb());
|
|
93
|
-
},
|
|
94
|
-
};
|
|
95
|
-
}
|
|
1
|
+
import { NodeRedisCacheClientOptions, NodeRedisCacheClientSource, RedisCacheClient } from '../core/redis-cache-client';
|
|
2
|
+
import { createRedisScanPattern, stripRedisKeyPrefix } from './redis-key-prefix';
|
|
3
|
+
|
|
4
|
+
function normalizeGetResponse(response: unknown): string | null {
|
|
5
|
+
if (typeof response === 'string' || response === null) {
|
|
6
|
+
return response;
|
|
7
|
+
}
|
|
8
|
+
throw new TypeError('Expected Redis GET to return a string or null');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function validateOkResponse(command: string, response: unknown): void {
|
|
12
|
+
if (response !== 'OK') {
|
|
13
|
+
throw new TypeError(`Expected Redis ${command} to return "OK"`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function validateDeleteResponse(response: unknown): void {
|
|
18
|
+
if (typeof response !== 'number' || !Number.isInteger(response) || response < 0) {
|
|
19
|
+
throw new TypeError('Expected Redis DEL to return a non-negative integer');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
24
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function normalizeScanResponse(
|
|
28
|
+
response: unknown,
|
|
29
|
+
keyPrefix: string
|
|
30
|
+
): { readonly cursor: string; readonly keys: readonly string[] } {
|
|
31
|
+
if (
|
|
32
|
+
!isRecord(response) ||
|
|
33
|
+
typeof response.cursor !== 'string' ||
|
|
34
|
+
!Array.isArray(response.keys) ||
|
|
35
|
+
!response.keys.every((key: unknown) => typeof key === 'string')
|
|
36
|
+
) {
|
|
37
|
+
throw new TypeError('Expected node-redis SCAN to return a { cursor, keys } object');
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
cursor: response.cursor,
|
|
41
|
+
keys: response.keys.map((key: string) => stripRedisKeyPrefix(keyPrefix, key)),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 将调用方的 node-redis 连接适配为客户端无关的缓存命令,不创建或关闭连接。
|
|
47
|
+
* @param client 使用默认字符串响应映射的 node-redis 普通客户端。
|
|
48
|
+
* @param options 可选的字面 key 前缀;缺省时不改写逻辑 key。
|
|
49
|
+
* @returns 复用该连接并统一物理 key 前缀的 Redis 缓存客户端。
|
|
50
|
+
* @throws 返回对象的命令方法传播客户端错误;非标准响应拒绝为 TypeError。
|
|
51
|
+
*/
|
|
52
|
+
export function createNodeRedisCacheClient(
|
|
53
|
+
client: NodeRedisCacheClientSource,
|
|
54
|
+
options?: NodeRedisCacheClientOptions
|
|
55
|
+
): RedisCacheClient {
|
|
56
|
+
const keyPrefix = options?.keyPrefix ?? '';
|
|
57
|
+
const createPhysicalKey = (key: string): string => `${keyPrefix}${key}`;
|
|
58
|
+
return {
|
|
59
|
+
/** @inheritdoc */
|
|
60
|
+
async get(key: string): Promise<string | null> {
|
|
61
|
+
return normalizeGetResponse(await client.get(createPhysicalKey(key)));
|
|
62
|
+
},
|
|
63
|
+
/** @inheritdoc */
|
|
64
|
+
async set({ key, value, ttlSeconds }): Promise<void> {
|
|
65
|
+
const physicalKey = createPhysicalKey(key);
|
|
66
|
+
if (ttlSeconds === undefined) {
|
|
67
|
+
validateOkResponse('SET', await client.set(physicalKey, value));
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
validateOkResponse('SETEX', await client.setEx(physicalKey, ttlSeconds, value));
|
|
71
|
+
},
|
|
72
|
+
/** @inheritdoc */
|
|
73
|
+
async deleteMany(keys: readonly string[]): Promise<void> {
|
|
74
|
+
if (keys.length === 0) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
validateDeleteResponse(await client.del(keys.map(createPhysicalKey)));
|
|
78
|
+
},
|
|
79
|
+
/** @inheritdoc */
|
|
80
|
+
async scan({ cursor, pattern, count }): Promise<{
|
|
81
|
+
readonly cursor: string;
|
|
82
|
+
readonly keys: readonly string[];
|
|
83
|
+
}> {
|
|
84
|
+
const response = await client.scan(cursor, {
|
|
85
|
+
MATCH: createRedisScanPattern(keyPrefix, pattern),
|
|
86
|
+
COUNT: count,
|
|
87
|
+
});
|
|
88
|
+
return normalizeScanResponse(response, keyPrefix);
|
|
89
|
+
},
|
|
90
|
+
/** @inheritdoc */
|
|
91
|
+
async flushDatabase(): Promise<void> {
|
|
92
|
+
validateOkResponse('FLUSHDB', await client.flushDb());
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
const REDIS_GLOB_META_CHARACTERS = new Set(['\\', '*', '?', '[', ']']);
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 将普通字符串转换为 Redis glob pattern 中的字面量片段。
|
|
5
|
-
* @param value 仅作为字面前缀使用的字符串。
|
|
6
|
-
* @returns 对反斜杠、星号、问号和方括号增加反斜杠转义后的片段。
|
|
7
|
-
*/
|
|
8
|
-
export function escapeRedisGlobLiteral(value: string): string {
|
|
9
|
-
let escaped = '';
|
|
10
|
-
for (const character of value) {
|
|
11
|
-
escaped += REDIS_GLOB_META_CHARACTERS.has(character) ? `\\${character}` : character;
|
|
12
|
-
}
|
|
13
|
-
return escaped;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 为逻辑 glob pattern 增加按字面量解释的 Redis key 前缀。
|
|
18
|
-
* @param keyPrefix 调用方配置的物理 key 前缀。
|
|
19
|
-
* @param pattern Provider 传入且需要保留 glob 语义的逻辑 pattern。
|
|
20
|
-
* @returns 可直接交给 Redis SCAN MATCH 的物理 pattern。
|
|
21
|
-
*/
|
|
22
|
-
export function createRedisScanPattern(keyPrefix: string, pattern: string): string {
|
|
23
|
-
return `${escapeRedisGlobLiteral(keyPrefix)}${pattern}`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* 验证扫描结果属于指定物理命名空间并恢复逻辑 key。
|
|
28
|
-
* @param keyPrefix 调用方配置的物理 key 前缀。
|
|
29
|
-
* @param physicalKey Redis SCAN 返回的物理 key。
|
|
30
|
-
* @returns 只剥离一次字面前缀后的逻辑 key。
|
|
31
|
-
* @throws 扫描结果不属于指定前缀时抛出 TypeError,避免扩大后续删除范围。
|
|
32
|
-
*/
|
|
33
|
-
export function stripRedisKeyPrefix(keyPrefix: string, physicalKey: string): string {
|
|
34
|
-
if (!physicalKey.startsWith(keyPrefix)) {
|
|
35
|
-
throw new TypeError('Redis SCAN returned a key outside the configured key prefix');
|
|
36
|
-
}
|
|
37
|
-
return physicalKey.slice(keyPrefix.length);
|
|
38
|
-
}
|
|
1
|
+
const REDIS_GLOB_META_CHARACTERS = new Set(['\\', '*', '?', '[', ']']);
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 将普通字符串转换为 Redis glob pattern 中的字面量片段。
|
|
5
|
+
* @param value 仅作为字面前缀使用的字符串。
|
|
6
|
+
* @returns 对反斜杠、星号、问号和方括号增加反斜杠转义后的片段。
|
|
7
|
+
*/
|
|
8
|
+
export function escapeRedisGlobLiteral(value: string): string {
|
|
9
|
+
let escaped = '';
|
|
10
|
+
for (const character of value) {
|
|
11
|
+
escaped += REDIS_GLOB_META_CHARACTERS.has(character) ? `\\${character}` : character;
|
|
12
|
+
}
|
|
13
|
+
return escaped;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 为逻辑 glob pattern 增加按字面量解释的 Redis key 前缀。
|
|
18
|
+
* @param keyPrefix 调用方配置的物理 key 前缀。
|
|
19
|
+
* @param pattern Provider 传入且需要保留 glob 语义的逻辑 pattern。
|
|
20
|
+
* @returns 可直接交给 Redis SCAN MATCH 的物理 pattern。
|
|
21
|
+
*/
|
|
22
|
+
export function createRedisScanPattern(keyPrefix: string, pattern: string): string {
|
|
23
|
+
return `${escapeRedisGlobLiteral(keyPrefix)}${pattern}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 验证扫描结果属于指定物理命名空间并恢复逻辑 key。
|
|
28
|
+
* @param keyPrefix 调用方配置的物理 key 前缀。
|
|
29
|
+
* @param physicalKey Redis SCAN 返回的物理 key。
|
|
30
|
+
* @returns 只剥离一次字面前缀后的逻辑 key。
|
|
31
|
+
* @throws 扫描结果不属于指定前缀时抛出 TypeError,避免扩大后续删除范围。
|
|
32
|
+
*/
|
|
33
|
+
export function stripRedisKeyPrefix(keyPrefix: string, physicalKey: string): string {
|
|
34
|
+
if (!physicalKey.startsWith(keyPrefix)) {
|
|
35
|
+
throw new TypeError('Redis SCAN returned a key outside the configured key prefix');
|
|
36
|
+
}
|
|
37
|
+
return physicalKey.slice(keyPrefix.length);
|
|
38
|
+
}
|