@travetto/cache 5.0.0-rc.0 → 5.0.0-rc.10
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 +7 -9
- package/package.json +5 -5
- package/src/decorator.ts +3 -7
- package/src/error.ts +1 -1
- package/src/service.ts +2 -2
- package/src/types.ts +4 -4
- package/src/util.ts +2 -2
- package/support/test/service.ts +2 -2
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#
|
|
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
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
|
|
@@ -41,7 +41,7 @@ The caching framework provides method decorators that enables simple use cases.
|
|
|
41
41
|
|
|
42
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#
|
|
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.
|
|
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#
|
|
72
|
+
The [@Cache](https://github.com/travetto/travetto/tree/main/module/cache/src/decorator.ts#L13) 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#
|
|
85
|
+
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
86
|
|
|
87
87
|
**Code: Using decorators to cache/evict user access**
|
|
88
88
|
```typescript
|
|
@@ -123,19 +123,17 @@ By design, the [CacheService](https://github.com/travetto/travetto/tree/main/mod
|
|
|
123
123
|
**Code: Registering a Custom Model Source**
|
|
124
124
|
```typescript
|
|
125
125
|
import { InjectableFactory } from '@travetto/di';
|
|
126
|
-
import { ModelExpirySupport } from '@travetto/model';
|
|
126
|
+
import { MemoryModelService, ModelExpirySupport } from '@travetto/model';
|
|
127
127
|
import { CacheModelⲐ } from '@travetto/cache';
|
|
128
128
|
|
|
129
129
|
class Config {
|
|
130
130
|
@InjectableFactory(CacheModelⲐ)
|
|
131
131
|
static getModel(): ModelExpirySupport {
|
|
132
|
-
|
|
133
|
-
return new CustomAwesomeModelService();
|
|
132
|
+
return new CustomAwesomeModelService({});
|
|
134
133
|
}
|
|
135
134
|
}
|
|
136
135
|
|
|
137
|
-
|
|
138
|
-
class CustomAwesomeModelService implements ModelExpirySupport {
|
|
136
|
+
class CustomAwesomeModelService extends MemoryModelService {
|
|
139
137
|
// Implement all the things
|
|
140
138
|
}
|
|
141
139
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/cache",
|
|
3
|
-
"version": "5.0.0-rc.
|
|
3
|
+
"version": "5.0.0-rc.10",
|
|
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.
|
|
29
|
-
"@travetto/model": "^5.0.0-rc.
|
|
28
|
+
"@travetto/di": "^5.0.0-rc.9",
|
|
29
|
+
"@travetto/model": "^5.0.0-rc.10"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@travetto/test": "^5.0.0-rc.
|
|
33
|
-
"@travetto/transformer": "^5.0.0-rc.
|
|
32
|
+
"@travetto/test": "^5.0.0-rc.9",
|
|
33
|
+
"@travetto/transformer": "^5.0.0-rc.6"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/transformer": {
|
package/src/decorator.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import { TimeSpan, TimeUtil } from '@travetto/
|
|
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
|
-
}
|
|
33
|
-
return dec;
|
|
28
|
+
};
|
|
29
|
+
return castTo(dec);
|
|
34
30
|
}
|
|
35
31
|
|
|
36
32
|
/**
|
package/src/error.ts
CHANGED
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,
|
|
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) && (
|
|
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
|
-
|
|
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
|
-
|
|
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/
|
|
1
|
+
import { Util } 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
|
|
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
|
*/
|
package/support/test/service.ts
CHANGED
|
@@ -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/
|
|
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
|
|
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
|
|