@travetto/cache 5.0.0-rc.1 → 5.0.0-rc.11

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
@@ -26,7 +26,6 @@ npm install @travetto/model-{provider}
26
26
  yarn add @travetto/model-{provider}
27
27
  ```
28
28
  Currently, the following are packages that provide [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/service/expiry.ts#L11):
29
- * [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") - @travetto/model: [FileModelService](https://github.com/travetto/travetto/tree/main/module/model/src/provider/file.ts#L49), [MemoryModelService](https://github.com/travetto/travetto/tree/main/module/model/src/provider/memory.ts#L54)
30
29
  * [DynamoDB Model Support](https://github.com/travetto/travetto/tree/main/module/model-dynamodb#readme "DynamoDB backing for the travetto model module.") - @travetto/model-dynamodb
31
30
  * [Elasticsearch Model Source](https://github.com/travetto/travetto/tree/main/module/model-elasticsearch#readme "Elasticsearch backing for the travetto model module, with real-time modeling support for Elasticsearch mappings.") - @travetto/model-elasticsearch
32
31
  * [MongoDB Model Support](https://github.com/travetto/travetto/tree/main/module/model-mongo#readme "Mongo backing for the travetto model module.") - @travetto/model-mongo
@@ -35,17 +34,19 @@ Currently, the following are packages that provide [Expiry](https://github.com/t
35
34
  * [PostgreSQL Model Service](https://github.com/travetto/travetto/tree/main/module/model-postgres#readme "PostgreSQL backing for the travetto model module, with real-time modeling support for SQL schemas.") - @travetto/model-postgres
36
35
  * [MySQL Model Service](https://github.com/travetto/travetto/tree/main/module/model-mysql#readme "MySQL backing for the travetto model module, with real-time modeling support for SQL schemas.") - @travetto/model-mysql
37
36
  * [SQLite Model Service](https://github.com/travetto/travetto/tree/main/module/model-sqlite#readme "SQLite backing for the travetto model module, with real-time modeling support for SQL schemas.") - @travetto/model-sqlite
37
+ * [Memory Model Support](https://github.com/travetto/travetto/tree/main/module/model-memory#readme "Memory backing for the travetto model module.") - @travetto/model-memory
38
+ * [File Model Support](https://github.com/travetto/travetto/tree/main/module/model-file#readme "File system backing for the travetto model module.") - @travetto/model-file
38
39
 
39
40
  ## Decorators
40
41
  The caching framework provides method decorators that enables simple use cases. One of the requirements to use the caching decorators is that the method arguments, and return values need to be serializable into [JSON](https://www.json.org). Any other data types are not currently supported and would require either manual usage of the caching services directly, or specification of serialization/deserialization routines in the cache config.
41
42
 
42
43
  Additionally, to use the decorators you will need to have a [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L35) object accessible on the class instance. This can be dependency injected, or manually constructed. The decorators will detect the field at time of method execution, which decouples construction of your class from the cache construction.
43
44
 
44
- [@Cache](https://github.com/travetto/travetto/tree/main/module/cache/src/decorator.ts#L16) is a decorator that will cache all successful results, keyed by a computation based on the method arguments. Given the desire for supporting remote caches (e.g. [redis](https://redis.io), [memcached](https://memcached.org)), only asynchronous methods are supported.
45
+ [@Cache](https://github.com/travetto/travetto/tree/main/module/cache/src/decorator.ts#L13) is a decorator that will cache all successful results, keyed by a computation based on the method arguments. Given the desire for supporting remote caches (e.g. [redis](https://redis.io), [memcached](https://memcached.org)), only asynchronous methods are supported.
45
46
 
46
47
  **Code: Using decorators to cache expensive async call**
47
48
  ```typescript
48
- import { MemoryModelService } from '@travetto/model';
49
+ import { MemoryModelService } from '@travetto/model-memory';
49
50
  import { Cache, CacheService } from '@travetto/cache';
50
51
 
51
52
  async function request(url: string): Promise<string> {
@@ -69,7 +70,7 @@ export class Worker {
69
70
  ```
70
71
 
71
72
  ### Cache
72
- The [@Cache](https://github.com/travetto/travetto/tree/main/module/cache/src/decorator.ts#L16) decorator supports configurations on:
73
+ The [@Cache](https://github.com/travetto/travetto/tree/main/module/cache/src/decorator.ts#L13) decorator supports configurations on:
73
74
  * `name` the field name of the current class which points to the desired cache source.
74
75
  * `config` the additional/optional config options, on a per invocation basis
75
76
 
@@ -82,11 +83,11 @@ The [@Cache](https://github.com/travetto/travetto/tree/main/module/cache/src/dec
82
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.
83
84
 
84
85
  ### EvictCache
85
- Additionally, there is support for planned eviction via the [@EvictCache](https://github.com/travetto/travetto/tree/main/module/cache/src/decorator.ts#L43) 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#L39) 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
87
 
87
88
  **Code: Using decorators to cache/evict user access**
88
89
  ```typescript
89
- import { MemoryModelService } from '@travetto/model';
90
+ import { MemoryModelService } from '@travetto/model-memory';
90
91
  import { Cache, EvictCache, CacheService } from '@travetto/cache';
91
92
 
92
93
  class User { }
@@ -124,18 +125,17 @@ By design, the [CacheService](https://github.com/travetto/travetto/tree/main/mod
124
125
  ```typescript
125
126
  import { InjectableFactory } from '@travetto/di';
126
127
  import { ModelExpirySupport } from '@travetto/model';
128
+ import { MemoryModelService } from '@travetto/model-memory';
127
129
  import { CacheModelⲐ } from '@travetto/cache';
128
130
 
129
131
  class Config {
130
132
  @InjectableFactory(CacheModelⲐ)
131
133
  static getModel(): ModelExpirySupport {
132
- // @ts-expect-error
133
- return new CustomAwesomeModelService();
134
+ return new CustomAwesomeModelService({});
134
135
  }
135
136
  }
136
137
 
137
- // @ts-expect-error
138
- class CustomAwesomeModelService implements ModelExpirySupport {
138
+ class CustomAwesomeModelService extends MemoryModelService {
139
139
  // Implement all the things
140
140
  }
141
141
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/cache",
3
- "version": "5.0.0-rc.1",
3
+ "version": "5.0.0-rc.11",
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": "^5.0.0-rc.1",
29
- "@travetto/model": "^5.0.0-rc.1"
28
+ "@travetto/di": "^5.0.0-rc.10",
29
+ "@travetto/model": "^5.0.0-rc.11"
30
30
  },
31
31
  "peerDependencies": {
32
- "@travetto/test": "^5.0.0-rc.1",
33
- "@travetto/transformer": "^5.0.0-rc.1"
32
+ "@travetto/test": "^5.0.0-rc.10",
33
+ "@travetto/transformer": "^5.0.0-rc.7"
34
34
  },
35
35
  "peerDependenciesMeta": {
36
36
  "@travetto/transformer": {
package/src/decorator.ts CHANGED
@@ -1,12 +1,9 @@
1
- import { TimeSpan, TimeUtil } from '@travetto/base';
1
+ import { castTo, MethodDescriptor, TimeSpan, TimeUtil } from '@travetto/runtime';
2
2
 
3
3
  import { CacheService } from './service';
4
4
  import { CoreCacheConfig, CacheConfig } from './types';
5
5
  import { CacheAware, CacheConfigⲐ, EvictConfigⲐ } from './internal/types';
6
6
 
7
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
- type MethodDescriptor<R = any, V = unknown> = TypedPropertyDescriptor<(this: V, ...params: any[]) => R>;
9
-
10
7
  /**
11
8
  * Indicates a method is intended to cache. The return type must be properly serializable
12
9
  * @param field The field of the cache source
@@ -25,12 +22,11 @@ export function Cache<F extends string, U extends Record<F, CacheService>>(
25
22
  config = cfg;
26
23
  }
27
24
  }
28
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
29
25
  const dec = function <R extends Promise<unknown>>(target: U & CacheAware, propertyKey: string, descriptor: MethodDescriptor<R>): void {
30
26
  config.keySpace ??= `${target.constructor.name}.${propertyKey}`;
31
27
  (target[CacheConfigⲐ] ??= {})[propertyKey] = config;
32
- } as MethodDecorator;
33
- return dec;
28
+ };
29
+ return castTo(dec);
34
30
  }
35
31
 
36
32
  /**
package/src/error.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AppError } from '@travetto/base';
1
+ import { AppError } from '@travetto/runtime';
2
2
 
3
3
  /** Cache Error Class */
4
4
  export class CacheError extends AppError { }
package/src/service.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ExpiresAt, Index, Model, ModelExpirySupport, NotFoundError } from '@travetto/model';
2
2
  import { Text } from '@travetto/schema';
3
3
  import { Inject, Injectable } from '@travetto/di';
4
- import { AppError, Env, TimeUtil } from '@travetto/base';
4
+ import { AppError, Runtime, TimeUtil } from '@travetto/runtime';
5
5
  import { isIndexedSupported, isStorageSupported } from '@travetto/model/src/internal/service/common';
6
6
 
7
7
  import { CacheError } from './error';
@@ -41,7 +41,7 @@ export class CacheService {
41
41
  }
42
42
 
43
43
  async postConstruct(): Promise<void> {
44
- if (isStorageSupported(this.#modelService) && (Env.dynamic || this.#modelService.config?.autoCreate)) {
44
+ if (isStorageSupported(this.#modelService) && (Runtime.dynamic || this.#modelService.config?.autoCreate)) {
45
45
  await this.#modelService.createModel?.(CacheRecord);
46
46
  }
47
47
  }
package/src/types.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { TypedFunction } from '@travetto/runtime';
2
+
1
3
  /**
2
4
  * A minimal config for a cache operation
3
5
  */
@@ -5,13 +7,11 @@ export interface CoreCacheConfig {
5
7
  /**
6
8
  * A method for converting the input params into the final set of param keys
7
9
  */
8
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
- params?: (...params: any[]) => unknown[];
10
+ params?: TypedFunction<unknown[]>;
10
11
  /**
11
12
  * Takes in a set of params and produce the unique cache key
12
13
  */
13
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- key?: (...params: any[]) => string;
14
+ key?: TypedFunction<string>;
15
15
  /**
16
16
  * A namespace for the specific cache operation
17
17
  */
package/src/util.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Util } from '@travetto/base';
1
+ import { BinaryUtil } from '@travetto/runtime';
2
2
 
3
3
  import { CoreCacheConfig } from './types';
4
4
 
@@ -8,7 +8,7 @@ import { CoreCacheConfig } from './types';
8
8
  export class CacheUtil {
9
9
 
10
10
  /**
11
- * Convert value to safe JSON for persistance
11
+ * Convert value to safe JSON for persistence
12
12
  * @param value The value to make safe for storage
13
13
  * @param all Should functions and regex be included
14
14
  */
@@ -35,6 +35,6 @@ export class CacheUtil {
35
35
  const input = config.params?.(params) ?? params;
36
36
  const keyParams = config.key?.(...input) ?? input;
37
37
  const key = `${config.keySpace!}_${this.toSafeJSON(keyParams)}`;
38
- return Util.hash(key, 32);
38
+ return BinaryUtil.hash(key, 32);
39
39
  }
40
40
  }
@@ -7,7 +7,7 @@ import { Inject, Injectable } from '@travetto/di';
7
7
  import { InjectableSuite } from '@travetto/di/support/test/suite';
8
8
  import { ModelSuite } from '@travetto/model/support/test/suite';
9
9
  import { isIndexedSupported } from '@travetto/model/src/internal/service/common';
10
- import { Class } from '@travetto/base';
10
+ import { castTo, Class } from '@travetto/runtime';
11
11
  import { Schema } from '@travetto/schema';
12
12
 
13
13
  import { Cache, EvictCache } from '../../src/decorator';
@@ -58,7 +58,7 @@ class SampleService {
58
58
  return { length: Object.keys(config).length, size };
59
59
  }
60
60
 
61
- @Cache('source', { keySpace: 'user.id', reinstate: x => User.from(x as User) })
61
+ @Cache('source', { keySpace: 'user.id', reinstate: x => User.from(castTo(x)) })
62
62
  async getUser(userId: string) {
63
63
  await timers.setTimeout(100);
64
64