@tsed/platform-cache 8.11.1 → 8.11.2-rc.1
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/lib/esm/decorators/useCache.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/interceptors/PlatformCacheInterceptor.js +0 -1
- package/lib/esm/interfaces/PlatformCacheOptions.js +0 -1
- package/lib/esm/interfaces/PlatformCachedObject.js +0 -1
- package/lib/esm/interfaces/interfaces.js +0 -1
- package/lib/esm/services/PlatformCache.js +0 -1
- package/lib/esm/utils/getPrefix.js +0 -1
- package/lib/esm/utils/isEndpoint.js +0 -1
- package/package.json +11 -8
- package/lib/esm/decorators/useCache.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/interceptors/PlatformCacheInterceptor.js.map +0 -1
- package/lib/esm/interfaces/PlatformCacheOptions.js.map +0 -1
- package/lib/esm/interfaces/PlatformCachedObject.js.map +0 -1
- package/lib/esm/interfaces/interfaces.js.map +0 -1
- package/lib/esm/services/PlatformCache.js.map +0 -1
- package/lib/esm/utils/getPrefix.js.map +0 -1
- package/lib/esm/utils/isEndpoint.js.map +0 -1
- package/src/decorators/useCache.ts +0 -8
- package/src/index.ts +0 -11
- package/src/interceptors/PlatformCacheInterceptor.spec.ts +0 -650
- package/src/interceptors/PlatformCacheInterceptor.ts +0 -236
- package/src/interfaces/PlatformCacheOptions.ts +0 -27
- package/src/interfaces/PlatformCachedObject.ts +0 -7
- package/src/interfaces/interfaces.ts +0 -34
- package/src/services/PlatformCache.spec.ts +0 -363
- package/src/services/PlatformCache.ts +0 -205
- package/src/utils/getPrefix.ts +0 -13
- package/src/utils/isEndpoint.ts +0 -6
- package/vitest.config.mts +0 -21
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
import {AsyncLocalStorage} from "node:async_hooks";
|
|
2
|
-
|
|
3
|
-
import {isClass, isFunction, isString, Type} from "@tsed/core";
|
|
4
|
-
import {Configuration, Inject, InjectorService, Module} from "@tsed/di";
|
|
5
|
-
import {deserialize, JsonDeserializerOptions, serialize} from "@tsed/json-mapper";
|
|
6
|
-
import {Logger} from "@tsed/logger";
|
|
7
|
-
import type {Cache, CachingConfig, MultiCache} from "cache-manager";
|
|
8
|
-
|
|
9
|
-
import {PlatformCacheSettings} from "../interfaces/interfaces.js";
|
|
10
|
-
import {PlatformCachedObject} from "../interfaces/PlatformCachedObject.js";
|
|
11
|
-
import {getPrefix} from "../utils/getPrefix.js";
|
|
12
|
-
|
|
13
|
-
const defaultKeyResolver = (args: any[]) => {
|
|
14
|
-
return args.map((arg: any) => (isClass(arg) ? JSON.stringify(serialize(arg)) : arg)).join(":");
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export type CacheManager = Cache | MultiCache;
|
|
18
|
-
export type Ttl = number | ((result: any) => number);
|
|
19
|
-
|
|
20
|
-
const storage: AsyncLocalStorage<{forceRefresh: boolean}> = new AsyncLocalStorage();
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @platform
|
|
24
|
-
*/
|
|
25
|
-
@Module()
|
|
26
|
-
export class PlatformCache {
|
|
27
|
-
@Configuration()
|
|
28
|
-
protected settings: Configuration;
|
|
29
|
-
|
|
30
|
-
@Inject()
|
|
31
|
-
protected injector: InjectorService;
|
|
32
|
-
|
|
33
|
-
@Inject()
|
|
34
|
-
protected logger: Logger;
|
|
35
|
-
|
|
36
|
-
#cache: CacheManager | undefined;
|
|
37
|
-
|
|
38
|
-
get cache() {
|
|
39
|
-
return this.#cache;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async $onInit() {
|
|
43
|
-
const settings = this.settings.get<PlatformCacheSettings>("cache");
|
|
44
|
-
|
|
45
|
-
if (settings) {
|
|
46
|
-
this.#cache = await this.createCacheManager(settings);
|
|
47
|
-
|
|
48
|
-
await this.injector.emit("$onCreateCacheManager", this.#cache);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
getKeysOf(target: Type<any>, propertyKey: string | symbol) {
|
|
53
|
-
const prefix = getPrefix(target, propertyKey);
|
|
54
|
-
|
|
55
|
-
return this.keys(`${prefix.join(":")}:*`);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
disabled(): boolean {
|
|
59
|
-
return !this.settings.get<PlatformCacheSettings>("cache");
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
defaultKeyResolver() {
|
|
63
|
-
return this.settings.get<(args: any[], ctx?: any) => string>("cache.keyResolver", defaultKeyResolver);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
defaultTtl() {
|
|
67
|
-
return this.settings.get<Ttl>("cache.ttl");
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
calculateTTL(result?: any, currentTtl?: Ttl): number {
|
|
71
|
-
const ttl = currentTtl === undefined ? this.defaultTtl() : currentTtl;
|
|
72
|
-
|
|
73
|
-
return isFunction(ttl) ? ttl(result) : ttl;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
ttl(key: string) {
|
|
77
|
-
if (this.cache && "store" in this.cache && this.cache.store.ttl) {
|
|
78
|
-
return this.cache.store.ttl(key);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return Promise.resolve();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
wrap<T>(key: string, fetch: () => Promise<T>, ttl?: number): Promise<T> {
|
|
85
|
-
if (!this.cache) {
|
|
86
|
-
return fetch();
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return this.cache?.wrap<T>(key, fetch, ttl);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
get<T>(key: string, options: JsonDeserializerOptions = {}): Promise<T | undefined> {
|
|
93
|
-
return Promise.resolve(deserialize(this.cache?.get<T>(key), options));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async set<T>(key: string, value: any, options?: CachingConfig<T>): Promise<T | undefined> {
|
|
97
|
-
await this.cache?.set(key, value, options?.ttl);
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
async getCachedObject(key: string) {
|
|
102
|
-
try {
|
|
103
|
-
return await this.get<PlatformCachedObject>(key);
|
|
104
|
-
} catch (er) {
|
|
105
|
-
this.logger.error({
|
|
106
|
-
event: "CACHE_ERROR",
|
|
107
|
-
method: "getCachedObject",
|
|
108
|
-
error: er
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async setCachedObject(key: string, data: any, opts: {ttl: number} & Record<string, any>) {
|
|
114
|
-
try {
|
|
115
|
-
await this.set<PlatformCachedObject>(
|
|
116
|
-
key,
|
|
117
|
-
{
|
|
118
|
-
...opts,
|
|
119
|
-
data: JSON.stringify(data)
|
|
120
|
-
},
|
|
121
|
-
{
|
|
122
|
-
ttl: opts.ttl
|
|
123
|
-
}
|
|
124
|
-
);
|
|
125
|
-
} catch (er) {
|
|
126
|
-
this.logger.error({
|
|
127
|
-
event: "CACHE_ERROR",
|
|
128
|
-
method: "setCachedObject",
|
|
129
|
-
error: er
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async del(key: string): Promise<void> {
|
|
135
|
-
await this.cache?.del(key);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
async reset(): Promise<void> {
|
|
139
|
-
// @ts-ignore
|
|
140
|
-
await this.cache?.reset();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
keys(...args: any[]): Promise<string[]> {
|
|
144
|
-
if (this.cache && "store" in this.cache && this.cache.store.keys) {
|
|
145
|
-
return this.cache.store.keys(...args);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return Promise.resolve([]);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async deleteKeys(patterns: string): Promise<string[]> {
|
|
152
|
-
const keys = await this.keys(patterns);
|
|
153
|
-
|
|
154
|
-
await Promise.all(keys.map((key: string) => this.del(key)));
|
|
155
|
-
|
|
156
|
-
return keys;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Use micromatch instead native patterns. Use this method if the native store method doesn't support glob patterns
|
|
161
|
-
* @param patterns
|
|
162
|
-
*/
|
|
163
|
-
async getMatchingKeys(patterns: string): Promise<string[]> {
|
|
164
|
-
const [keys, {default: micromatch}] = await Promise.all([this.keys(), import("micromatch")]);
|
|
165
|
-
|
|
166
|
-
return micromatch(keys, patterns);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
async deleteMatchingKeys(patterns: string): Promise<string[]> {
|
|
170
|
-
const keys = await this.getMatchingKeys(patterns);
|
|
171
|
-
|
|
172
|
-
await Promise.all(keys.map((key: string) => this.del(key)));
|
|
173
|
-
|
|
174
|
-
return keys;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
refresh(callback: () => Promise<any> | any) {
|
|
178
|
-
return storage.run({forceRefresh: true}, callback);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
isForceRefresh() {
|
|
182
|
-
return !!storage.getStore()?.forceRefresh;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
protected async createCacheManager(settings: PlatformCacheSettings) {
|
|
186
|
-
const {caches, store = "memory", ttl, ...props} = settings;
|
|
187
|
-
|
|
188
|
-
const {multiCaching, caching} = await import("cache-manager");
|
|
189
|
-
|
|
190
|
-
return caches?.length
|
|
191
|
-
? multiCaching(caches)
|
|
192
|
-
: caching(this.mapStore(store), {
|
|
193
|
-
...props,
|
|
194
|
-
ttl
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
private mapStore(store: "memory" | Function | {create: Function}): any {
|
|
199
|
-
if (!isString(store) && "create" in store) {
|
|
200
|
-
return store.create;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return store;
|
|
204
|
-
}
|
|
205
|
-
}
|
package/src/utils/getPrefix.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import {nameOf, Type} from "@tsed/core";
|
|
2
|
-
import {getInterceptorOptions} from "@tsed/di";
|
|
3
|
-
|
|
4
|
-
import type {PlatformCacheOptions} from "../interfaces/PlatformCacheOptions.js";
|
|
5
|
-
|
|
6
|
-
export function getPrefix(target: Type<any>, propertyKey: string | symbol) {
|
|
7
|
-
const {prefix} = getInterceptorOptions<PlatformCacheOptions>(target, propertyKey);
|
|
8
|
-
if (prefix) {
|
|
9
|
-
return [prefix];
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return [nameOf(target), propertyKey];
|
|
13
|
-
}
|
package/src/utils/isEndpoint.ts
DELETED
package/vitest.config.mts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
import {presets} from "@tsed/vitest/presets";
|
|
3
|
-
import {defineConfig} from "vitest/config";
|
|
4
|
-
|
|
5
|
-
export default defineConfig(
|
|
6
|
-
{
|
|
7
|
-
...presets,
|
|
8
|
-
test: {
|
|
9
|
-
...presets.test,
|
|
10
|
-
coverage: {
|
|
11
|
-
...presets.test.coverage,
|
|
12
|
-
thresholds: {
|
|
13
|
-
statements: 0,
|
|
14
|
-
branches: 0,
|
|
15
|
-
functions: 0,
|
|
16
|
-
lines: 0
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
);
|