@travetto/cache 5.0.14 → 5.0.16
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/LICENSE +1 -1
- package/README.md +2 -2
- package/package.json +5 -5
- package/src/decorator.ts +3 -3
- package/src/internal/types.ts +4 -4
- package/src/service.ts +5 -5
- package/support/test/service.ts +2 -2
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -126,10 +126,10 @@ By design, the [CacheService](https://github.com/travetto/travetto/tree/main/mod
|
|
|
126
126
|
import { InjectableFactory } from '@travetto/di';
|
|
127
127
|
import { ModelExpirySupport } from '@travetto/model';
|
|
128
128
|
import { MemoryModelService } from '@travetto/model-memory';
|
|
129
|
-
import {
|
|
129
|
+
import { CacheModelSymbol } from '@travetto/cache';
|
|
130
130
|
|
|
131
131
|
class Config {
|
|
132
|
-
@InjectableFactory(
|
|
132
|
+
@InjectableFactory(CacheModelSymbol)
|
|
133
133
|
static getModel(): ModelExpirySupport {
|
|
134
134
|
return new CustomAwesomeModelService({});
|
|
135
135
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/cache",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.16",
|
|
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.
|
|
29
|
-
"@travetto/model": "^5.0.
|
|
28
|
+
"@travetto/di": "^5.0.15",
|
|
29
|
+
"@travetto/model": "^5.0.16"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@travetto/test": "^5.0.
|
|
33
|
-
"@travetto/transformer": "^5.0.
|
|
32
|
+
"@travetto/test": "^5.0.17",
|
|
33
|
+
"@travetto/transformer": "^5.0.12"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/transformer": {
|
package/src/decorator.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { castTo, MethodDescriptor, TimeSpan, TimeUtil } from '@travetto/runtime'
|
|
|
2
2
|
|
|
3
3
|
import { CacheService } from './service';
|
|
4
4
|
import { CoreCacheConfig, CacheConfig } from './types';
|
|
5
|
-
import { CacheAware,
|
|
5
|
+
import { CacheAware, CacheConfigSymbol, EvictConfigSymbol } from './internal/types';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Indicates a method is intended to cache. The return type must be properly serializable
|
|
@@ -24,7 +24,7 @@ export function Cache<F extends string, U extends Record<F, CacheService>>(
|
|
|
24
24
|
}
|
|
25
25
|
const dec = function <R extends Promise<unknown>>(target: U & CacheAware, propertyKey: string, descriptor: MethodDescriptor<R>): void {
|
|
26
26
|
config.keySpace ??= `${target.constructor.name}.${propertyKey}`;
|
|
27
|
-
(target[
|
|
27
|
+
(target[CacheConfigSymbol] ??= {})[propertyKey] = config;
|
|
28
28
|
};
|
|
29
29
|
return castTo(dec);
|
|
30
30
|
}
|
|
@@ -39,6 +39,6 @@ export function Cache<F extends string, U extends Record<F, CacheService>>(
|
|
|
39
39
|
export function EvictCache<F extends string, U extends Record<F, CacheService>>(field: F, config: CoreCacheConfig = {}) {
|
|
40
40
|
return function <R extends Promise<unknown>>(target: U & CacheAware, propertyKey: string, descriptor: MethodDescriptor<R>): void {
|
|
41
41
|
config.keySpace ??= `${target.constructor.name}.${propertyKey}`;
|
|
42
|
-
(target[
|
|
42
|
+
(target[EvictConfigSymbol] ??= {})[propertyKey] = config;
|
|
43
43
|
};
|
|
44
44
|
}
|
package/src/internal/types.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { CacheConfig, CoreCacheConfig } from '../types';
|
|
2
2
|
|
|
3
|
-
export const
|
|
4
|
-
export const
|
|
3
|
+
export const CacheConfigSymbol = Symbol.for('@travetto/cache:cache');
|
|
4
|
+
export const EvictConfigSymbol = Symbol.for('@travetto/cache:evict');
|
|
5
5
|
|
|
6
6
|
export interface CacheAware {
|
|
7
|
-
[
|
|
8
|
-
[
|
|
7
|
+
[CacheConfigSymbol]?: Record<string, CacheConfig>;
|
|
8
|
+
[EvictConfigSymbol]?: Record<string, CoreCacheConfig>;
|
|
9
9
|
}
|
package/src/service.ts
CHANGED
|
@@ -6,9 +6,9 @@ import { isIndexedSupported, isStorageSupported } from '@travetto/model/src/inte
|
|
|
6
6
|
|
|
7
7
|
import { CacheError } from './error';
|
|
8
8
|
import { CacheUtil } from './util';
|
|
9
|
-
import { CacheAware,
|
|
9
|
+
import { CacheAware, CacheConfigSymbol, EvictConfigSymbol } from './internal/types';
|
|
10
10
|
|
|
11
|
-
export const
|
|
11
|
+
export const CacheModelSymbol = Symbol.for('@travetto/cache:model');
|
|
12
12
|
|
|
13
13
|
const INFINITE_MAX_AGE = TimeUtil.asMillis(10, 'y');
|
|
14
14
|
|
|
@@ -36,7 +36,7 @@ export class CacheService {
|
|
|
36
36
|
|
|
37
37
|
#modelService: ModelExpirySupport;
|
|
38
38
|
|
|
39
|
-
constructor(@Inject(
|
|
39
|
+
constructor(@Inject(CacheModelSymbol, { resolution: 'loose' }) modelService: ModelExpirySupport) {
|
|
40
40
|
this.#modelService = modelService;
|
|
41
41
|
}
|
|
42
42
|
|
|
@@ -161,7 +161,7 @@ export class CacheService {
|
|
|
161
161
|
* @param params input parameters
|
|
162
162
|
*/
|
|
163
163
|
async cache(target: CacheAware, method: string, fn: Function, params: unknown[]): Promise<unknown | undefined> {
|
|
164
|
-
const config = target[
|
|
164
|
+
const config = target[CacheConfigSymbol]![method];
|
|
165
165
|
|
|
166
166
|
const id = CacheUtil.generateKey(config, params);
|
|
167
167
|
|
|
@@ -188,7 +188,7 @@ export class CacheService {
|
|
|
188
188
|
* @param params Input params to the function
|
|
189
189
|
*/
|
|
190
190
|
async evict(target: CacheAware, method: string, fn: Function, params: unknown[]): Promise<unknown> {
|
|
191
|
-
const config = target[
|
|
191
|
+
const config = target[EvictConfigSymbol]![method];
|
|
192
192
|
const id = CacheUtil.generateKey(config, params);
|
|
193
193
|
const val = await fn.apply(target, params);
|
|
194
194
|
try {
|
package/support/test/service.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { castTo, Class } from '@travetto/runtime';
|
|
|
11
11
|
import { Schema } from '@travetto/schema';
|
|
12
12
|
|
|
13
13
|
import { Cache, EvictCache } from '../../src/decorator';
|
|
14
|
-
import {
|
|
14
|
+
import { CacheModelSymbol, CacheService } from '../../src/service';
|
|
15
15
|
import { CacheUtil } from '../../src/util';
|
|
16
16
|
|
|
17
17
|
@Schema()
|
|
@@ -82,7 +82,7 @@ class SampleService {
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
@Suite()
|
|
85
|
-
@ModelSuite(
|
|
85
|
+
@ModelSuite(CacheModelSymbol)
|
|
86
86
|
@InjectableSuite()
|
|
87
87
|
export abstract class CacheServiceSuite {
|
|
88
88
|
|