@travetto/cache 6.0.0 → 7.0.0-rc.0

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 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#L38) 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.
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": "6.0.0",
3
+ "version": "7.0.0-rc.0",
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": "^6.0.0",
29
- "@travetto/model": "^6.0.0"
28
+ "@travetto/di": "^7.0.0-rc.0",
29
+ "@travetto/model": "^7.0.0-rc.0"
30
30
  },
31
31
  "peerDependencies": {
32
- "@travetto/test": "^6.0.0",
33
- "@travetto/transformer": "^6.0.0"
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
- * @augments `@travetto/cache:Cache`
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;
@@ -24,6 +24,12 @@ export function Cache<F extends string, U extends Record<F, CacheService>>(
24
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 = _descriptor.value!.bind(castTo(target));
28
+ // Allows for DI to run, as the service will not be bound until after the decorator is run
29
+ _descriptor.value = castTo(function (this: typeof target) {
30
+ return this[field].cache(this, propertyKey, handler, [...arguments]);
31
+ });
32
+ Object.defineProperty(_descriptor.value, 'name', { value: propertyKey, writable: false });
27
33
  };
28
34
  return castTo(dec);
29
35
  }
@@ -33,11 +39,17 @@ export function Cache<F extends string, U extends Record<F, CacheService>>(
33
39
  * freshest data will be collected
34
40
  * @param field The field of the cache source
35
41
  * @param config The additional cache configuration
36
- * @augments `@travetto/cache:Evict`
42
+ * @kind decorator
37
43
  */
38
44
  export function EvictCache<F extends string, U extends Record<F, CacheService>>(field: F, config: CoreCacheConfig = {}) {
39
45
  return function <R extends Promise<unknown>>(target: U & CacheAware, propertyKey: string, _descriptor: MethodDescriptor<R>): void {
40
46
  config.keySpace ??= `${target.constructor.name}.${propertyKey}`;
41
47
  (target[EvictConfigSymbol] ??= {})[propertyKey] = config;
48
+ const handler = _descriptor.value!.bind(castTo(target));
49
+ // Allows for DI to run, as the service will not be bound until after the decorator is run
50
+ _descriptor.value = castTo(function (this: typeof target) {
51
+ return this[field].evict(this, propertyKey, handler, [...arguments]);
52
+ });
53
+ Object.defineProperty(_descriptor.value, 'name', { value: propertyKey, writable: false });
42
54
  };
43
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, { resolution: 'loose' }) modelService: ModelExpirySupport) {
36
+ constructor(@Inject({ qualifier: CacheModelSymbol, resolution: 'loose' }) modelService: ModelExpirySupport) {
37
37
  this.#modelService = modelService;
38
38
  }
39
39
 
@@ -1,68 +0,0 @@
1
- import ts from 'typescript';
2
-
3
- import { TransformerState, DecoratorMeta, OnMethod } from '@travetto/transformer';
4
-
5
- /**
6
- * Transform the cache headers
7
- */
8
- export class CacheTransformer {
9
-
10
- /**
11
- * When `@Cache` and `@Evict` are present
12
- */
13
- @OnMethod('Cache', 'Evict')
14
- static instrumentCache(state: TransformerState, node: ts.MethodDeclaration, dm?: DecoratorMeta): ts.MethodDeclaration {
15
-
16
- const isCache = !!state.findDecorator(this, node, 'Cache');
17
- const dec = dm?.dec;
18
-
19
- // If valid function
20
- if (dec && ts.isCallExpression(dec.expression)) {
21
- const params = dec.expression.arguments;
22
- const mainExpression = params[0];
23
-
24
- const op = isCache ? 'cache' : 'evict';
25
-
26
- // Create an arrow function to retain the `this` value.
27
- const fn = state.factory.createArrowFunction(
28
- [state.factory.createModifier(ts.SyntaxKind.AsyncKeyword)],
29
- undefined,
30
- [],
31
- undefined,
32
- undefined,
33
- node.body!
34
- );
35
-
36
- // Return new method calling evict or cache depending on decorator.
37
- return state.factory.updateMethodDeclaration(
38
- node,
39
- node.modifiers,
40
- node.asteriskToken,
41
- node.name,
42
- node.questionToken,
43
- node.typeParameters,
44
- node.parameters,
45
- node.type,
46
- state.factory.createBlock([
47
- state.factory.createReturnStatement(
48
- state.factory.createCallExpression(
49
- state.factory.createPropertyAccessExpression(
50
- state.factory.createElementAccessExpression(state.factory.createThis(), mainExpression), op
51
- ),
52
- undefined,
53
- [
54
- state.factory.createThis(),
55
- state.factory.createStringLiteral(node.name.getText()),
56
- fn,
57
- state.factory.createArrayLiteralExpression([
58
- state.factory.createSpreadElement(state.createIdentifier('arguments'))
59
- ])
60
- ]
61
- )
62
- )
63
- ])
64
- );
65
- }
66
- return node;
67
- }
68
- }