@travetto/cache 7.0.0-rc.1 → 7.0.0-rc.3
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 +16 -16
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.3",
|
|
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.2",
|
|
29
|
+
"@travetto/model": "^7.0.0-rc.2"
|
|
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.2",
|
|
33
|
+
"@travetto/transformer": "^7.0.0-rc.2"
|
|
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
|
@@ -137,16 +137,16 @@ export class CacheService {
|
|
|
137
137
|
* @param extendOnAccess should the expiry be extended on access
|
|
138
138
|
*/
|
|
139
139
|
async getOptional(id: string, extendOnAccess = true): Promise<unknown | undefined> {
|
|
140
|
-
let
|
|
140
|
+
let result: unknown;
|
|
141
141
|
|
|
142
142
|
try {
|
|
143
|
-
|
|
144
|
-
} catch (
|
|
145
|
-
if (!(
|
|
146
|
-
throw
|
|
143
|
+
result = await this.get(id, extendOnAccess);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (!(error instanceof CacheError) && !(error instanceof NotFoundError)) {
|
|
146
|
+
throw error;
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
|
-
return
|
|
149
|
+
return result;
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
/**
|
|
@@ -162,18 +162,18 @@ export class CacheService {
|
|
|
162
162
|
|
|
163
163
|
const id = CacheUtil.generateKey(config, params);
|
|
164
164
|
|
|
165
|
-
let
|
|
165
|
+
let result = await this.getOptional(id, config.extendOnAccess);
|
|
166
166
|
|
|
167
|
-
if (
|
|
167
|
+
if (result === undefined) {
|
|
168
168
|
const data = await fn.apply(target, params);
|
|
169
|
-
|
|
169
|
+
result = await this.set(id, config.keySpace!, data, config.maxAge);
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
if (config.reinstate) { // Reinstate result value if needed
|
|
173
|
-
|
|
173
|
+
result = config.reinstate(result);
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
-
return
|
|
176
|
+
return result;
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
/**
|
|
@@ -187,14 +187,14 @@ export class CacheService {
|
|
|
187
187
|
async evict(target: CacheAware, method: string, fn: Function, params: unknown[]): Promise<unknown> {
|
|
188
188
|
const config = target[EvictConfigSymbol]![method];
|
|
189
189
|
const id = CacheUtil.generateKey(config, params);
|
|
190
|
-
const
|
|
190
|
+
const result = await fn.apply(target, params);
|
|
191
191
|
try {
|
|
192
192
|
await this.delete(id); // Ignore failure on delete
|
|
193
|
-
} catch (
|
|
194
|
-
if (!(
|
|
195
|
-
throw
|
|
193
|
+
} catch (error) {
|
|
194
|
+
if (!(error instanceof NotFoundError)) {
|
|
195
|
+
throw error;
|
|
196
196
|
}
|
|
197
197
|
}
|
|
198
|
-
return
|
|
198
|
+
return result;
|
|
199
199
|
}
|
|
200
200
|
}
|