@travetto/cache 7.0.0-rc.2 → 7.0.0-rc.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/cache",
3
- "version": "7.0.0-rc.2",
3
+ "version": "7.0.0-rc.4",
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": "^7.0.0-rc.1",
29
- "@travetto/model": "^7.0.0-rc.1"
28
+ "@travetto/di": "^7.0.0-rc.3",
29
+ "@travetto/model": "^7.0.0-rc.3"
30
30
  },
31
31
  "peerDependencies": {
32
- "@travetto/test": "^7.0.0-rc.1",
33
- "@travetto/transformer": "^7.0.0-rc.1"
32
+ "@travetto/test": "^7.0.0-rc.3",
33
+ "@travetto/transformer": "^7.0.0-rc.3"
34
34
  },
35
35
  "peerDependenciesMeta": {
36
36
  "@travetto/transformer": {
package/src/decorator.ts CHANGED
@@ -10,18 +10,18 @@ import { CoreCacheConfig, CacheConfig, CacheAware, CacheConfigSymbol, EvictConfi
10
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
- export function Cache<F extends string, U extends Record<F, CacheService>>(field: F, cfg?: CacheConfig): MethodDecorator;
13
+ export function Cache<F extends string, U extends Record<F, CacheService>>(field: F, input?: CacheConfig): MethodDecorator;
14
14
  export function Cache<F extends string, U extends Record<F, CacheService>>(
15
- field: F, cfg?: number | TimeSpan | CacheConfig, config: Exclude<CacheConfig, 'maxAge'> = {}
15
+ field: F, input?: number | TimeSpan | CacheConfig, config: Exclude<CacheConfig, 'maxAge'> = {}
16
16
  ): MethodDecorator {
17
- if (cfg !== undefined) {
18
- if (typeof cfg === 'string' || typeof cfg === 'number') {
19
- config.maxAge = TimeUtil.asMillis(cfg);
17
+ if (input !== undefined) {
18
+ if (typeof input === 'string' || typeof input === 'number') {
19
+ config.maxAge = TimeUtil.asMillis(input);
20
20
  } else {
21
- config = cfg;
21
+ config = input;
22
22
  }
23
23
  }
24
- const dec = function <R extends Promise<unknown>>(target: U & CacheAware, propertyKey: string, descriptor: MethodDescriptor<R>): void {
24
+ const decorator = 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
27
  const handler = descriptor.value!;
@@ -31,7 +31,7 @@ export function Cache<F extends string, U extends Record<F, CacheService>>(
31
31
  });
32
32
  Object.defineProperty(descriptor.value, 'name', { value: propertyKey, writable: false });
33
33
  };
34
- return castTo(dec);
34
+ return castTo(decorator);
35
35
  }
36
36
 
37
37
  /**
package/src/service.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ExpiresAt, Index, Model, ModelExpirySupport, NotFoundError, ModelStorageUtil, ModelIndexedUtil } from '@travetto/model';
2
2
  import { Text } from '@travetto/schema';
3
3
  import { Inject, Injectable } from '@travetto/di';
4
- import { AppError, Runtime, TimeUtil, Util } from '@travetto/runtime';
4
+ import { AppError, JSONUtil, TimeUtil } from '@travetto/runtime';
5
5
 
6
6
  import { CacheError } from './error.ts';
7
7
  import { CacheUtil } from './util.ts';
@@ -14,7 +14,7 @@ const INFINITE_MAX_AGE = TimeUtil.asMillis(10, 'y');
14
14
  type: 'unsorted',
15
15
  fields: [{ keySpace: 1 }]
16
16
  })
17
- @Model({ autoCreate: false })
17
+ @Model({ autoCreate: 'production' })
18
18
  export class CacheRecord {
19
19
  id: string;
20
20
  @Text()
@@ -37,12 +37,6 @@ export class CacheService {
37
37
  this.#modelService = modelService;
38
38
  }
39
39
 
40
- async postConstruct(): Promise<void> {
41
- if (ModelStorageUtil.isSupported(this.#modelService) && (Runtime.dynamic || this.#modelService.config?.autoCreate)) {
42
- await this.#modelService.createModel?.(CacheRecord);
43
- }
44
- }
45
-
46
40
  /**
47
41
  * Get an item throwing an error if missing or expired. Allows for extending expiry based on access
48
42
  * @param id Record identifier
@@ -72,7 +66,7 @@ export class CacheService {
72
66
  }
73
67
  }
74
68
 
75
- return Util.decodeSafeJSON(entry);
69
+ return JSONUtil.parseBase64(entry);
76
70
  }
77
71
 
78
72
  /**
@@ -81,7 +75,7 @@ export class CacheService {
81
75
  * @returns
82
76
  */
83
77
  async set(id: string, keySpace: string, entry: unknown, maxAge?: number): Promise<unknown> {
84
- const entryText = Util.encodeSafeJSON(entry);
78
+ const entryText = JSONUtil.stringifyBase64(entry);
85
79
 
86
80
  const store = await this.#modelService.upsert(CacheRecord,
87
81
  CacheRecord.from({
@@ -93,7 +87,7 @@ export class CacheService {
93
87
  }),
94
88
  );
95
89
 
96
- return Util.decodeSafeJSON(store.entry);
90
+ return JSONUtil.parseBase64(store.entry);
97
91
  }
98
92
 
99
93
  /**
@@ -137,16 +131,16 @@ export class CacheService {
137
131
  * @param extendOnAccess should the expiry be extended on access
138
132
  */
139
133
  async getOptional(id: string, extendOnAccess = true): Promise<unknown | undefined> {
140
- let res: unknown;
134
+ let result: unknown;
141
135
 
142
136
  try {
143
- res = await this.get(id, extendOnAccess);
144
- } catch (err) {
145
- if (!(err instanceof CacheError) && !(err instanceof NotFoundError)) {
146
- throw err;
137
+ result = await this.get(id, extendOnAccess);
138
+ } catch (error) {
139
+ if (!(error instanceof CacheError) && !(error instanceof NotFoundError)) {
140
+ throw error;
147
141
  }
148
142
  }
149
- return res;
143
+ return result;
150
144
  }
151
145
 
152
146
  /**
@@ -162,18 +156,18 @@ export class CacheService {
162
156
 
163
157
  const id = CacheUtil.generateKey(config, params);
164
158
 
165
- let res = await this.getOptional(id, config.extendOnAccess);
159
+ let result = await this.getOptional(id, config.extendOnAccess);
166
160
 
167
- if (res === undefined) {
161
+ if (result === undefined) {
168
162
  const data = await fn.apply(target, params);
169
- res = await this.set(id, config.keySpace!, data, config.maxAge);
163
+ result = await this.set(id, config.keySpace!, data, config.maxAge);
170
164
  }
171
165
 
172
166
  if (config.reinstate) { // Reinstate result value if needed
173
- res = config.reinstate(res);
167
+ result = config.reinstate(result);
174
168
  }
175
169
 
176
- return res;
170
+ return result;
177
171
  }
178
172
 
179
173
  /**
@@ -187,14 +181,14 @@ export class CacheService {
187
181
  async evict(target: CacheAware, method: string, fn: Function, params: unknown[]): Promise<unknown> {
188
182
  const config = target[EvictConfigSymbol]![method];
189
183
  const id = CacheUtil.generateKey(config, params);
190
- const val = await fn.apply(target, params);
184
+ const result = await fn.apply(target, params);
191
185
  try {
192
186
  await this.delete(id); // Ignore failure on delete
193
- } catch (err) {
194
- if (!(err instanceof NotFoundError)) {
195
- throw err;
187
+ } catch (error) {
188
+ if (!(error instanceof NotFoundError)) {
189
+ throw error;
196
190
  }
197
191
  }
198
- return val;
192
+ return result;
199
193
  }
200
194
  }
package/src/types.ts CHANGED
@@ -22,7 +22,7 @@ export interface CoreCacheConfig {
22
22
  keySpace?: string;
23
23
 
24
24
  /**
25
- * How to reconstitute the cached value after JSON.parse
25
+ * How to reconstitute the cached value after JSON parsing
26
26
  */
27
27
  reinstate?: (input: unknown) => unknown;
28
28
  }
package/src/util.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { BinaryUtil, Util } from '@travetto/runtime';
1
+ import { BinaryUtil, JSONUtil } from '@travetto/runtime';
2
2
 
3
3
  import { CoreCacheConfig } from './types.ts';
4
4
 
@@ -13,7 +13,7 @@ export class CacheUtil {
13
13
  static generateKey(config: CoreCacheConfig, params: unknown[]): string {
14
14
  const input = config.params?.(params) ?? params;
15
15
  const keyParams = config.key?.(...input) ?? input;
16
- const key = `${config.keySpace!}_${Util.encodeSafeJSON(keyParams)}`;
16
+ const key = `${config.keySpace!}_${JSONUtil.stringifyBase64(keyParams)}`;
17
17
  return BinaryUtil.hash(key, 32);
18
18
  }
19
19
  }