@travetto/cache 7.0.0-rc.2 → 7.0.0-rc.4
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/package.json +5 -5
- package/src/decorator.ts +8 -8
- package/src/service.ts +21 -27
- package/src/types.ts +1 -1
- package/src/util.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/cache",
|
|
3
|
-
"version": "7.0.0-rc.
|
|
3
|
+
"version": "7.0.0-rc.4",
|
|
4
4
|
"description": "Caching functionality with decorators for declarative use.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"directory": "module/cache"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@travetto/di": "^7.0.0-rc.
|
|
29
|
-
"@travetto/model": "^7.0.0-rc.
|
|
28
|
+
"@travetto/di": "^7.0.0-rc.3",
|
|
29
|
+
"@travetto/model": "^7.0.0-rc.3"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@travetto/test": "^7.0.0-rc.
|
|
33
|
-
"@travetto/transformer": "^7.0.0-rc.
|
|
32
|
+
"@travetto/test": "^7.0.0-rc.3",
|
|
33
|
+
"@travetto/transformer": "^7.0.0-rc.3"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/transformer": {
|
package/src/decorator.ts
CHANGED
|
@@ -10,18 +10,18 @@ import { CoreCacheConfig, CacheConfig, CacheAware, CacheConfigSymbol, EvictConfi
|
|
|
10
10
|
* @kind decorator
|
|
11
11
|
*/
|
|
12
12
|
export function Cache<F extends string, U extends Record<F, CacheService>>(field: F, maxAge: number | TimeSpan, config?: Omit<CacheConfig, 'maxAge'>): MethodDecorator;
|
|
13
|
-
export function Cache<F extends string, U extends Record<F, CacheService>>(field: F,
|
|
13
|
+
export function Cache<F extends string, U extends Record<F, CacheService>>(field: F, input?: CacheConfig): MethodDecorator;
|
|
14
14
|
export function Cache<F extends string, U extends Record<F, CacheService>>(
|
|
15
|
-
field: F,
|
|
15
|
+
field: F, input?: number | TimeSpan | CacheConfig, config: Exclude<CacheConfig, 'maxAge'> = {}
|
|
16
16
|
): MethodDecorator {
|
|
17
|
-
if (
|
|
18
|
-
if (typeof
|
|
19
|
-
config.maxAge = TimeUtil.asMillis(
|
|
17
|
+
if (input !== undefined) {
|
|
18
|
+
if (typeof input === 'string' || typeof input === 'number') {
|
|
19
|
+
config.maxAge = TimeUtil.asMillis(input);
|
|
20
20
|
} else {
|
|
21
|
-
config =
|
|
21
|
+
config = input;
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
-
const
|
|
24
|
+
const decorator = function <R extends Promise<unknown>>(target: U & CacheAware, propertyKey: string, descriptor: MethodDescriptor<R>): void {
|
|
25
25
|
config.keySpace ??= `${target.constructor.name}.${propertyKey}`;
|
|
26
26
|
(target[CacheConfigSymbol] ??= {})[propertyKey] = config;
|
|
27
27
|
const handler = descriptor.value!;
|
|
@@ -31,7 +31,7 @@ export function Cache<F extends string, U extends Record<F, CacheService>>(
|
|
|
31
31
|
});
|
|
32
32
|
Object.defineProperty(descriptor.value, 'name', { value: propertyKey, writable: false });
|
|
33
33
|
};
|
|
34
|
-
return castTo(
|
|
34
|
+
return castTo(decorator);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
package/src/service.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ExpiresAt, Index, Model, ModelExpirySupport, NotFoundError, ModelStorageUtil, ModelIndexedUtil } from '@travetto/model';
|
|
2
2
|
import { Text } from '@travetto/schema';
|
|
3
3
|
import { Inject, Injectable } from '@travetto/di';
|
|
4
|
-
import { AppError,
|
|
4
|
+
import { AppError, JSONUtil, TimeUtil } from '@travetto/runtime';
|
|
5
5
|
|
|
6
6
|
import { CacheError } from './error.ts';
|
|
7
7
|
import { CacheUtil } from './util.ts';
|
|
@@ -14,7 +14,7 @@ const INFINITE_MAX_AGE = TimeUtil.asMillis(10, 'y');
|
|
|
14
14
|
type: 'unsorted',
|
|
15
15
|
fields: [{ keySpace: 1 }]
|
|
16
16
|
})
|
|
17
|
-
@Model({ autoCreate:
|
|
17
|
+
@Model({ autoCreate: 'production' })
|
|
18
18
|
export class CacheRecord {
|
|
19
19
|
id: string;
|
|
20
20
|
@Text()
|
|
@@ -37,12 +37,6 @@ export class CacheService {
|
|
|
37
37
|
this.#modelService = modelService;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
async postConstruct(): Promise<void> {
|
|
41
|
-
if (ModelStorageUtil.isSupported(this.#modelService) && (Runtime.dynamic || this.#modelService.config?.autoCreate)) {
|
|
42
|
-
await this.#modelService.createModel?.(CacheRecord);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
40
|
/**
|
|
47
41
|
* Get an item throwing an error if missing or expired. Allows for extending expiry based on access
|
|
48
42
|
* @param id Record identifier
|
|
@@ -72,7 +66,7 @@ export class CacheService {
|
|
|
72
66
|
}
|
|
73
67
|
}
|
|
74
68
|
|
|
75
|
-
return
|
|
69
|
+
return JSONUtil.parseBase64(entry);
|
|
76
70
|
}
|
|
77
71
|
|
|
78
72
|
/**
|
|
@@ -81,7 +75,7 @@ export class CacheService {
|
|
|
81
75
|
* @returns
|
|
82
76
|
*/
|
|
83
77
|
async set(id: string, keySpace: string, entry: unknown, maxAge?: number): Promise<unknown> {
|
|
84
|
-
const entryText =
|
|
78
|
+
const entryText = JSONUtil.stringifyBase64(entry);
|
|
85
79
|
|
|
86
80
|
const store = await this.#modelService.upsert(CacheRecord,
|
|
87
81
|
CacheRecord.from({
|
|
@@ -93,7 +87,7 @@ export class CacheService {
|
|
|
93
87
|
}),
|
|
94
88
|
);
|
|
95
89
|
|
|
96
|
-
return
|
|
90
|
+
return JSONUtil.parseBase64(store.entry);
|
|
97
91
|
}
|
|
98
92
|
|
|
99
93
|
/**
|
|
@@ -137,16 +131,16 @@ export class CacheService {
|
|
|
137
131
|
* @param extendOnAccess should the expiry be extended on access
|
|
138
132
|
*/
|
|
139
133
|
async getOptional(id: string, extendOnAccess = true): Promise<unknown | undefined> {
|
|
140
|
-
let
|
|
134
|
+
let result: unknown;
|
|
141
135
|
|
|
142
136
|
try {
|
|
143
|
-
|
|
144
|
-
} catch (
|
|
145
|
-
if (!(
|
|
146
|
-
throw
|
|
137
|
+
result = await this.get(id, extendOnAccess);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if (!(error instanceof CacheError) && !(error instanceof NotFoundError)) {
|
|
140
|
+
throw error;
|
|
147
141
|
}
|
|
148
142
|
}
|
|
149
|
-
return
|
|
143
|
+
return result;
|
|
150
144
|
}
|
|
151
145
|
|
|
152
146
|
/**
|
|
@@ -162,18 +156,18 @@ export class CacheService {
|
|
|
162
156
|
|
|
163
157
|
const id = CacheUtil.generateKey(config, params);
|
|
164
158
|
|
|
165
|
-
let
|
|
159
|
+
let result = await this.getOptional(id, config.extendOnAccess);
|
|
166
160
|
|
|
167
|
-
if (
|
|
161
|
+
if (result === undefined) {
|
|
168
162
|
const data = await fn.apply(target, params);
|
|
169
|
-
|
|
163
|
+
result = await this.set(id, config.keySpace!, data, config.maxAge);
|
|
170
164
|
}
|
|
171
165
|
|
|
172
166
|
if (config.reinstate) { // Reinstate result value if needed
|
|
173
|
-
|
|
167
|
+
result = config.reinstate(result);
|
|
174
168
|
}
|
|
175
169
|
|
|
176
|
-
return
|
|
170
|
+
return result;
|
|
177
171
|
}
|
|
178
172
|
|
|
179
173
|
/**
|
|
@@ -187,14 +181,14 @@ export class CacheService {
|
|
|
187
181
|
async evict(target: CacheAware, method: string, fn: Function, params: unknown[]): Promise<unknown> {
|
|
188
182
|
const config = target[EvictConfigSymbol]![method];
|
|
189
183
|
const id = CacheUtil.generateKey(config, params);
|
|
190
|
-
const
|
|
184
|
+
const result = await fn.apply(target, params);
|
|
191
185
|
try {
|
|
192
186
|
await this.delete(id); // Ignore failure on delete
|
|
193
|
-
} catch (
|
|
194
|
-
if (!(
|
|
195
|
-
throw
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (!(error instanceof NotFoundError)) {
|
|
189
|
+
throw error;
|
|
196
190
|
}
|
|
197
191
|
}
|
|
198
|
-
return
|
|
192
|
+
return result;
|
|
199
193
|
}
|
|
200
194
|
}
|
package/src/types.ts
CHANGED
package/src/util.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BinaryUtil,
|
|
1
|
+
import { BinaryUtil, JSONUtil } from '@travetto/runtime';
|
|
2
2
|
|
|
3
3
|
import { CoreCacheConfig } from './types.ts';
|
|
4
4
|
|
|
@@ -13,7 +13,7 @@ export class CacheUtil {
|
|
|
13
13
|
static generateKey(config: CoreCacheConfig, params: unknown[]): string {
|
|
14
14
|
const input = config.params?.(params) ?? params;
|
|
15
15
|
const keyParams = config.key?.(...input) ?? input;
|
|
16
|
-
const key = `${config.keySpace!}_${
|
|
16
|
+
const key = `${config.keySpace!}_${JSONUtil.stringifyBase64(keyParams)}`;
|
|
17
17
|
return BinaryUtil.hash(key, 32);
|
|
18
18
|
}
|
|
19
19
|
}
|