@travetto/cache 5.1.0 → 6.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/package.json +5 -5
- package/src/service.ts +4 -4
- package/src/util.ts +2 -23
- package/support/test/service.ts +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/cache",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.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": "^
|
|
29
|
-
"@travetto/model": "^
|
|
28
|
+
"@travetto/di": "^6.0.0-rc.0",
|
|
29
|
+
"@travetto/model": "^6.0.0-rc.0"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@travetto/test": "^
|
|
33
|
-
"@travetto/transformer": "^
|
|
32
|
+
"@travetto/test": "^6.0.0-rc.0",
|
|
33
|
+
"@travetto/transformer": "^6.0.0-rc.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/transformer": {
|
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, Runtime, TimeUtil } from '@travetto/runtime';
|
|
4
|
+
import { AppError, Runtime, TimeUtil, Util } from '@travetto/runtime';
|
|
5
5
|
import { isIndexedSupported, isStorageSupported } from '@travetto/model/src/internal/service/common';
|
|
6
6
|
|
|
7
7
|
import { CacheError } from './error';
|
|
@@ -75,7 +75,7 @@ export class CacheService {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
const res = await this.#modelService.get(CacheRecord, id);
|
|
78
|
-
return
|
|
78
|
+
return Util.decodeSafeJSON(res.entry);
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
/**
|
|
@@ -84,7 +84,7 @@ export class CacheService {
|
|
|
84
84
|
* @returns
|
|
85
85
|
*/
|
|
86
86
|
async set(id: string, keySpace: string, entry: unknown, maxAge?: number): Promise<unknown> {
|
|
87
|
-
const entryText =
|
|
87
|
+
const entryText = Util.encodeSafeJSON(entry);
|
|
88
88
|
|
|
89
89
|
const store = await this.#modelService.upsert(CacheRecord,
|
|
90
90
|
CacheRecord.from({
|
|
@@ -96,7 +96,7 @@ export class CacheService {
|
|
|
96
96
|
}),
|
|
97
97
|
);
|
|
98
98
|
|
|
99
|
-
return
|
|
99
|
+
return Util.decodeSafeJSON(store.entry);
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
/**
|
package/src/util.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BinaryUtil } from '@travetto/runtime';
|
|
1
|
+
import { BinaryUtil, Util } from '@travetto/runtime';
|
|
2
2
|
|
|
3
3
|
import { CoreCacheConfig } from './types';
|
|
4
4
|
|
|
@@ -7,34 +7,13 @@ import { CoreCacheConfig } from './types';
|
|
|
7
7
|
*/
|
|
8
8
|
export class CacheUtil {
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
* Convert value to safe JSON for persistence
|
|
12
|
-
* @param value The value to make safe for storage
|
|
13
|
-
* @param all Should functions and regex be included
|
|
14
|
-
*/
|
|
15
|
-
static toSafeJSON(value: unknown, all = false): string {
|
|
16
|
-
const replacer = all ?
|
|
17
|
-
((key: string, val: unknown): unknown => (val && val instanceof RegExp) ? val.source : (typeof val === 'function' ? val.toString() : val)) :
|
|
18
|
-
undefined;
|
|
19
|
-
|
|
20
|
-
return Buffer.from(JSON.stringify(value, replacer)).toString('base64');
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Read safe JSON back into an object
|
|
25
|
-
* @param value The value to read as safe JSON
|
|
26
|
-
*/
|
|
27
|
-
static fromSafeJSON(value: string | undefined): unknown {
|
|
28
|
-
return value ? JSON.parse(Buffer.from(value, 'base64').toString('utf8')) : undefined;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
10
|
/**
|
|
32
11
|
* Generate key given config, cache source and input params
|
|
33
12
|
*/
|
|
34
13
|
static generateKey(config: CoreCacheConfig, params: unknown[]): string {
|
|
35
14
|
const input = config.params?.(params) ?? params;
|
|
36
15
|
const keyParams = config.key?.(...input) ?? input;
|
|
37
|
-
const key = `${config.keySpace!}_${
|
|
16
|
+
const key = `${config.keySpace!}_${Util.encodeSafeJSON(keyParams)}`;
|
|
38
17
|
return BinaryUtil.hash(key, 32);
|
|
39
18
|
}
|
|
40
19
|
}
|
package/support/test/service.ts
CHANGED
|
@@ -12,7 +12,6 @@ import { Schema } from '@travetto/schema';
|
|
|
12
12
|
|
|
13
13
|
import { Cache, EvictCache } from '../../src/decorator';
|
|
14
14
|
import { CacheModelSymbol, CacheService } from '../../src/service';
|
|
15
|
-
import { CacheUtil } from '../../src/util';
|
|
16
15
|
|
|
17
16
|
@Schema()
|
|
18
17
|
class User { }
|
|
@@ -170,8 +169,6 @@ export abstract class CacheServiceSuite {
|
|
|
170
169
|
const val5 = await service.complexInput({ a: /abc/ }, 20);
|
|
171
170
|
assert(val3 !== val4);
|
|
172
171
|
assert.deepStrictEqual(val3, val5);
|
|
173
|
-
|
|
174
|
-
assert(CacheUtil.toSafeJSON(/abc/, true) !== CacheUtil.toSafeJSON(/cde/, true));
|
|
175
172
|
}
|
|
176
173
|
|
|
177
174
|
@Test()
|