@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,64 +1,119 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Cache,
|
|
3
|
-
CacheEvict,
|
|
4
|
-
type
|
|
5
|
-
type
|
|
6
|
-
type
|
|
7
|
-
type
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
type
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
export type
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
providerName: 'memory',
|
|
42
|
-
key:
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
};
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
1
|
+
import {
|
|
2
|
+
Cache,
|
|
3
|
+
CacheEvict,
|
|
4
|
+
type CacheErrorCodec,
|
|
5
|
+
type CacheErrorPolicy,
|
|
6
|
+
type CacheEvictOptions,
|
|
7
|
+
type CacheKeyResolver,
|
|
8
|
+
type CacheOptions,
|
|
9
|
+
type CacheProvider,
|
|
10
|
+
} from '@jintianxiayu/cache-decorator';
|
|
11
|
+
|
|
12
|
+
type Equal<Left, Right> =
|
|
13
|
+
(<Value>() => Value extends Left ? 1 : 2) extends <Value>() => Value extends Right ? 1 : 2 ? true : false;
|
|
14
|
+
type Assert<Condition extends true> = Condition;
|
|
15
|
+
type LegacyMethodDecorator = (_target: object, _propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
16
|
+
|
|
17
|
+
export type CacheOptionKeysMatch = Assert<Equal<keyof CacheOptions, 'ttl' | 'providerName' | 'key' | 'errorCache'>>;
|
|
18
|
+
export type CacheErrorPolicyKeysMatch = Assert<Equal<keyof CacheErrorPolicy, 'ttl' | 'shouldCache' | 'codec'>>;
|
|
19
|
+
export type CacheErrorCodecKeysMatch = Assert<Equal<keyof CacheErrorCodec, 'encode' | 'decode'>>;
|
|
20
|
+
export type CacheEvictOptionKeysMatch = Assert<Equal<keyof CacheEvictOptions, 'key' | 'allEntries' | 'providerName'>>;
|
|
21
|
+
export type CacheKeyResolverMatch = Assert<Equal<CacheKeyResolver, null | string | ((...args: unknown[]) => string)>>;
|
|
22
|
+
|
|
23
|
+
const provider: CacheProvider = {
|
|
24
|
+
get: <Value>(_key: string): Value | undefined => undefined,
|
|
25
|
+
set: <Value>(_key: string, _value: Value, _ttl?: number): void => {
|
|
26
|
+
return;
|
|
27
|
+
},
|
|
28
|
+
delete: (_key: string): void => {
|
|
29
|
+
return;
|
|
30
|
+
},
|
|
31
|
+
clear: (): void => {
|
|
32
|
+
return;
|
|
33
|
+
},
|
|
34
|
+
deleteByPattern: (_pattern: string): void => {
|
|
35
|
+
return;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const cacheDecorator: LegacyMethodDecorator = Cache('contract-cache', {
|
|
40
|
+
ttl: 60,
|
|
41
|
+
providerName: 'memory',
|
|
42
|
+
key: (...args: unknown[]) => String(args[0]),
|
|
43
|
+
});
|
|
44
|
+
const cacheWithoutErrorPolicy: LegacyMethodDecorator = Cache('contract-cache');
|
|
45
|
+
const cacheWithErrorTtl: LegacyMethodDecorator = Cache('contract-cache', { errorCache: { ttl: 10 } });
|
|
46
|
+
const codec: CacheErrorCodec = {
|
|
47
|
+
encode: (error: unknown): unknown => ({ message: error instanceof Error ? error.message : String(error) }),
|
|
48
|
+
decode: (payload: unknown): unknown => payload,
|
|
49
|
+
};
|
|
50
|
+
const cacheWithCompleteErrorPolicy: LegacyMethodDecorator = Cache('contract-cache', {
|
|
51
|
+
errorCache: {
|
|
52
|
+
ttl: 5,
|
|
53
|
+
shouldCache: (error: unknown): boolean => error instanceof Error,
|
|
54
|
+
codec,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
const evictDecorator: LegacyMethodDecorator = CacheEvict('contract-cache', {
|
|
58
|
+
providerName: 'memory',
|
|
59
|
+
key: null,
|
|
60
|
+
allEntries: false,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const invalidLoggingOption: CacheOptions = {
|
|
64
|
+
// @ts-expect-error CacheOptions 不提供第二套日志开关。
|
|
65
|
+
logging: true,
|
|
66
|
+
};
|
|
67
|
+
const invalidDebugOption: CacheOptions = {
|
|
68
|
+
// @ts-expect-error CacheOptions 不提供 debug 开关。
|
|
69
|
+
debug: true,
|
|
70
|
+
};
|
|
71
|
+
const invalidLoggerOption: CacheEvictOptions = {
|
|
72
|
+
// @ts-expect-error CacheEvictOptions 不接受 Logger 实例或回调。
|
|
73
|
+
logger: undefined,
|
|
74
|
+
};
|
|
75
|
+
const invalidMissingErrorTtl: CacheOptions = {
|
|
76
|
+
// @ts-expect-error 启用异常缓存时 ttl 必填。
|
|
77
|
+
errorCache: {},
|
|
78
|
+
};
|
|
79
|
+
const invalidEncodeOnlyCodec: CacheOptions = {
|
|
80
|
+
errorCache: {
|
|
81
|
+
ttl: 5,
|
|
82
|
+
// @ts-expect-error codec 必须同时提供 encode 和 decode。
|
|
83
|
+
codec: { encode: (error: unknown): unknown => error },
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
const invalidDecodeOnlyCodec: CacheOptions = {
|
|
87
|
+
errorCache: {
|
|
88
|
+
ttl: 5,
|
|
89
|
+
// @ts-expect-error codec 必须同时提供 encode 和 decode。
|
|
90
|
+
codec: { decode: (payload: unknown): unknown => payload },
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
const invalidPredicateSignature: CacheOptions = {
|
|
94
|
+
errorCache: {
|
|
95
|
+
ttl: 5,
|
|
96
|
+
// @ts-expect-error 筛选器必须接受 unknown 并同步返回 boolean。
|
|
97
|
+
shouldCache: (_error: string): boolean => true,
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
const invalidEvictErrorPolicy: CacheEvictOptions = {
|
|
101
|
+
// @ts-expect-error CacheEvictOptions 不接受异常缓存策略。
|
|
102
|
+
errorCache: { ttl: 5 },
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
void provider;
|
|
106
|
+
void cacheDecorator;
|
|
107
|
+
void cacheWithoutErrorPolicy;
|
|
108
|
+
void cacheWithErrorTtl;
|
|
109
|
+
void cacheWithCompleteErrorPolicy;
|
|
110
|
+
void codec;
|
|
111
|
+
void evictDecorator;
|
|
112
|
+
void invalidLoggingOption;
|
|
113
|
+
void invalidDebugOption;
|
|
114
|
+
void invalidLoggerOption;
|
|
115
|
+
void invalidMissingErrorTtl;
|
|
116
|
+
void invalidEncodeOnlyCodec;
|
|
117
|
+
void invalidDecodeOnlyCodec;
|
|
118
|
+
void invalidPredicateSignature;
|
|
119
|
+
void invalidEvictErrorPolicy;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../../../tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"noEmit": true,
|
|
5
|
-
"skipLibCheck": false,
|
|
6
|
-
"paths": {
|
|
7
|
-
"@jintianxiayu/cache-decorator": ["../../dist/index.d.ts"]
|
|
8
|
-
},
|
|
9
|
-
"types": ["node"]
|
|
10
|
-
},
|
|
11
|
-
"include": ["contract.ts"]
|
|
12
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"noEmit": true,
|
|
5
|
+
"skipLibCheck": false,
|
|
6
|
+
"paths": {
|
|
7
|
+
"@jintianxiayu/cache-decorator": ["../../dist/index.d.ts"]
|
|
8
|
+
},
|
|
9
|
+
"types": ["node"]
|
|
10
|
+
},
|
|
11
|
+
"include": ["contract.ts"]
|
|
12
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "./dist",
|
|
5
|
-
"rootDir": "./src"
|
|
6
|
-
},
|
|
7
|
-
"include": ["src/**/*"]
|
|
8
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "./dist",
|
|
5
|
+
"rootDir": "./src"
|
|
6
|
+
},
|
|
7
|
+
"include": ["src/**/*"]
|
|
8
|
+
}
|