@travetto/cache 4.1.2 → 5.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
@@ -26,7 +26,7 @@ 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#L50), [MemoryModelService](https://github.com/travetto/travetto/tree/main/module/model/src/provider/memory.ts#L53)
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#L50), [MemoryModelService](https://github.com/travetto/travetto/tree/main/module/model/src/provider/memory.ts#L54)
30
30
  * [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
31
  * [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
32
  * [MongoDB Model Support](https://github.com/travetto/travetto/tree/main/module/model-mongo#readme "Mongo backing for the travetto model module.") - @travetto/model-mongo
@@ -39,9 +39,9 @@ Currently, the following are packages that provide [Expiry](https://github.com/t
39
39
  ## Decorators
40
40
  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
41
 
42
- Additionally, to use the decorators you will need to have a [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L29) 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.
42
+ 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
43
 
44
- [@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.
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
45
 
46
46
  **Code: Using decorators to cache expensive async call**
47
47
  ```typescript
@@ -69,7 +69,7 @@ export class Worker {
69
69
  ```
70
70
 
71
71
  ### Cache
72
- The [@Cache](https://github.com/travetto/travetto/tree/main/module/cache/src/decorator.ts#L13) decorator supports configurations on:
72
+ The [@Cache](https://github.com/travetto/travetto/tree/main/module/cache/src/decorator.ts#L16) decorator supports configurations on:
73
73
  * `name` the field name of the current class which points to the desired cache source.
74
74
  * `config` the additional/optional config options, on a per invocation basis
75
75
 
@@ -82,7 +82,7 @@ The [@Cache](https://github.com/travetto/travetto/tree/main/module/cache/src/dec
82
82
  * `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
83
 
84
84
  ### EvictCache
85
- Additionally, there is support for planned eviction via the [@EvictCache](https://github.com/travetto/travetto/tree/main/module/cache/src/decorator.ts#L40) 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.
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
86
 
87
87
  **Code: Using decorators to cache/evict user access**
88
88
  ```typescript
@@ -118,7 +118,7 @@ export class UserService {
118
118
  ```
119
119
 
120
120
  ## Extending the Cache Service
121
- By design, the [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L29) relies solely on the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") module. Specifically on the [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/service/expiry.ts#L11). This combines basic support for CRUD as well as knowledge of how to manage expirable content. Any model service that honors these contracts is a valid candidate to power the [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L29). The [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L29) is expecting the model service to be registered using the @travetto/cache:model:
121
+ By design, the [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L35) relies solely on the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") module. Specifically on the [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/service/expiry.ts#L11). This combines basic support for CRUD as well as knowledge of how to manage expirable content. Any model service that honors these contracts is a valid candidate to power the [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L35). The [CacheService](https://github.com/travetto/travetto/tree/main/module/cache/src/service.ts#L35) is expecting the model service to be registered using the @travetto/cache:model:
122
122
 
123
123
  **Code: Registering a Custom Model Source**
124
124
  ```typescript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/cache",
3
- "version": "4.1.2",
3
+ "version": "5.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": "^4.1.0",
29
- "@travetto/model": "^4.1.2"
28
+ "@travetto/di": "^5.0.0-rc.0",
29
+ "@travetto/model": "^5.0.0-rc.0"
30
30
  },
31
31
  "peerDependencies": {
32
- "@travetto/test": "^4.1.0",
33
- "@travetto/transformer": "^4.1.0"
32
+ "@travetto/test": "^5.0.0-rc.0",
33
+ "@travetto/transformer": "^5.0.0-rc.0"
34
34
  },
35
35
  "peerDependenciesMeta": {
36
36
  "@travetto/transformer": {
package/src/decorator.ts CHANGED
@@ -1,9 +1,12 @@
1
- import { MethodDescriptor, TimeSpan, TimeUtil } from '@travetto/base';
1
+ import { TimeSpan, TimeUtil } from '@travetto/base';
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
+
7
10
  /**
8
11
  * Indicates a method is intended to cache. The return type must be properly serializable
9
12
  * @param field The field of the cache source
@@ -17,7 +20,7 @@ export function Cache<F extends string, U extends Record<F, CacheService>>(
17
20
  ): MethodDecorator {
18
21
  if (cfg !== undefined) {
19
22
  if (typeof cfg === 'string' || typeof cfg === 'number') {
20
- config.maxAge = TimeUtil.timeToMs(cfg);
23
+ config.maxAge = TimeUtil.asMillis(cfg);
21
24
  } else {
22
25
  config = cfg;
23
26
  }
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 } from '@travetto/base';
4
+ import { AppError, Env, TimeUtil } from '@travetto/base';
5
5
  import { isIndexedSupported, isStorageSupported } from '@travetto/model/src/internal/service/common';
6
6
 
7
7
  import { CacheError } from './error';
@@ -10,7 +10,7 @@ import { CacheAware, CacheConfigⲐ, EvictConfigⲐ } from './internal/types';
10
10
 
11
11
  export const CacheModelⲐ = Symbol.for('@travetto/cache:model');
12
12
 
13
- const INFINITE_MAX_AGE = '5000-01-01';
13
+ const INFINITE_MAX_AGE = TimeUtil.asMillis(10, 'y');
14
14
 
15
15
  @Index({
16
16
  name: 'keySpace',
@@ -68,7 +68,7 @@ export class CacheService {
68
68
  if (delta < threshold) {
69
69
  await this.#modelService.updatePartial(CacheRecord, {
70
70
  id,
71
- expiresAt: new Date(Date.now() + maxAge),
71
+ expiresAt: TimeUtil.fromNow(maxAge),
72
72
  issuedAt: new Date()
73
73
  }); // Do not wait
74
74
  }
@@ -91,7 +91,7 @@ export class CacheService {
91
91
  id,
92
92
  entry: entryText!,
93
93
  keySpace,
94
- expiresAt: new Date(maxAge ? maxAge + Date.now() : INFINITE_MAX_AGE),
94
+ expiresAt: TimeUtil.fromNow(maxAge || INFINITE_MAX_AGE),
95
95
  issuedAt: new Date()
96
96
  }),
97
97
  );
package/src/util.ts CHANGED
@@ -1,6 +1,5 @@
1
- import crypto from 'node:crypto';
1
+ import { Util } from '@travetto/base';
2
2
 
3
- import { ObjectUtil } from '@travetto/base';
4
3
  import { CoreCacheConfig } from './types';
5
4
 
6
5
  /**
@@ -15,7 +14,7 @@ export class CacheUtil {
15
14
  */
16
15
  static toSafeJSON(value: unknown, all = false): string {
17
16
  const replacer = all ?
18
- ((key: string, val: unknown): unknown => (val && val instanceof RegExp) ? val.source : (ObjectUtil.isFunction(val) ? val.toString() : val)) :
17
+ ((key: string, val: unknown): unknown => (val && val instanceof RegExp) ? val.source : (typeof val === 'function' ? val.toString() : val)) :
19
18
  undefined;
20
19
 
21
20
  return Buffer.from(JSON.stringify(value, replacer)).toString('base64');
@@ -36,6 +35,6 @@ export class CacheUtil {
36
35
  const input = config.params?.(params) ?? params;
37
36
  const keyParams = config.key?.(...input) ?? input;
38
37
  const key = `${config.keySpace!}_${this.toSafeJSON(keyParams)}`;
39
- return crypto.createHash('sha1').update(key).digest('hex').substring(0, 32); // Force to uuid
38
+ return Util.hash(key, 32);
40
39
  }
41
40
  }