@travetto/cache 6.0.1 → 7.0.0-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/README.md +1 -1
- package/package.json +5 -5
- package/src/decorator.ts +10 -10
- package/src/service.ts +1 -1
package/README.md
CHANGED
|
@@ -83,7 +83,7 @@ The [@Cache](https://github.com/travetto/travetto/tree/main/module/cache/src/dec
|
|
|
83
83
|
* `reinstate` the function to execute on return of a cached value. This allows for any necessary operations to conform to expected output (e.g. re-establishing class instances, etc.). This method should not be used often, as the return values of the methods should naturally serialize to/from `JSON` and the values should be usable either way.
|
|
84
84
|
|
|
85
85
|
### EvictCache
|
|
86
|
-
Additionally, there is support for planned eviction via the [@EvictCache](https://github.com/travetto/travetto/tree/main/module/cache/src/decorator.ts#
|
|
86
|
+
Additionally, there is support for planned eviction via the [@EvictCache](https://github.com/travetto/travetto/tree/main/module/cache/src/decorator.ts#L44) decorator. On successful execution of a method with this decorator, the matching keySpace/key value will be evicted from the cache. This requires coordination between multiple methods, to use the same `keySpace` and `key` to compute the expected key.
|
|
87
87
|
|
|
88
88
|
**Code: Using decorators to cache/evict user access**
|
|
89
89
|
```typescript
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/cache",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-rc.1",
|
|
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": "^
|
|
29
|
-
"@travetto/model": "^
|
|
28
|
+
"@travetto/di": "^7.0.0-rc.0",
|
|
29
|
+
"@travetto/model": "^7.0.0-rc.0"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@travetto/test": "^
|
|
33
|
-
"@travetto/transformer": "^
|
|
32
|
+
"@travetto/test": "^7.0.0-rc.0",
|
|
33
|
+
"@travetto/transformer": "^7.0.0-rc.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/transformer": {
|
package/src/decorator.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { CoreCacheConfig, CacheConfig, CacheAware, CacheConfigSymbol, EvictConfi
|
|
|
7
7
|
* Indicates a method is intended to cache. The return type must be properly serializable
|
|
8
8
|
* @param field The field of the cache source
|
|
9
9
|
* @param config The additional cache configuration
|
|
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
13
|
export function Cache<F extends string, U extends Record<F, CacheService>>(field: F, cfg?: CacheConfig): MethodDecorator;
|
|
@@ -21,15 +21,15 @@ export function Cache<F extends string, U extends Record<F, CacheService>>(
|
|
|
21
21
|
config = cfg;
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
-
const dec = function <R extends Promise<unknown>>(target: U & CacheAware, propertyKey: string,
|
|
24
|
+
const dec = 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
|
-
const handler =
|
|
27
|
+
const handler = descriptor.value!;
|
|
28
28
|
// Allows for DI to run, as the service will not be bound until after the decorator is run
|
|
29
|
-
|
|
29
|
+
descriptor.value = castTo(function (this: typeof target) {
|
|
30
30
|
return this[field].cache(this, propertyKey, handler, [...arguments]);
|
|
31
31
|
});
|
|
32
|
-
Object.defineProperty(
|
|
32
|
+
Object.defineProperty(descriptor.value, 'name', { value: propertyKey, writable: false });
|
|
33
33
|
};
|
|
34
34
|
return castTo(dec);
|
|
35
35
|
}
|
|
@@ -39,17 +39,17 @@ export function Cache<F extends string, U extends Record<F, CacheService>>(
|
|
|
39
39
|
* freshest data will be collected
|
|
40
40
|
* @param field The field of the cache source
|
|
41
41
|
* @param config The additional cache configuration
|
|
42
|
-
* @
|
|
42
|
+
* @kind decorator
|
|
43
43
|
*/
|
|
44
44
|
export function EvictCache<F extends string, U extends Record<F, CacheService>>(field: F, config: CoreCacheConfig = {}) {
|
|
45
|
-
return function <R extends Promise<unknown>>(target: U & CacheAware, propertyKey: string,
|
|
45
|
+
return function <R extends Promise<unknown>>(target: U & CacheAware, propertyKey: string, descriptor: MethodDescriptor<R>): void {
|
|
46
46
|
config.keySpace ??= `${target.constructor.name}.${propertyKey}`;
|
|
47
47
|
(target[EvictConfigSymbol] ??= {})[propertyKey] = config;
|
|
48
|
-
const handler =
|
|
48
|
+
const handler = descriptor.value!;
|
|
49
49
|
// Allows for DI to run, as the service will not be bound until after the decorator is run
|
|
50
|
-
|
|
50
|
+
descriptor.value = castTo(function (this: typeof target) {
|
|
51
51
|
return this[field].evict(this, propertyKey, handler, [...arguments]);
|
|
52
52
|
});
|
|
53
|
-
Object.defineProperty(
|
|
53
|
+
Object.defineProperty(descriptor.value, 'name', { value: propertyKey, writable: false });
|
|
54
54
|
};
|
|
55
55
|
}
|
package/src/service.ts
CHANGED
|
@@ -33,7 +33,7 @@ export class CacheService {
|
|
|
33
33
|
|
|
34
34
|
#modelService: ModelExpirySupport;
|
|
35
35
|
|
|
36
|
-
constructor(@Inject(CacheModelSymbol,
|
|
36
|
+
constructor(@Inject({ qualifier: CacheModelSymbol, resolution: 'loose' }) modelService: ModelExpirySupport) {
|
|
37
37
|
this.#modelService = modelService;
|
|
38
38
|
}
|
|
39
39
|
|