@expo/entity-cache-adapter-local-memory 0.25.1 → 0.26.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 +7 -4
- package/build/GenericLocalMemoryCacher.js +4 -5
- package/build/GenericLocalMemoryCacher.js.map +1 -1
- package/build/LocalMemoryCacheAdapter.js +1 -1
- package/build/LocalMemoryCacheAdapter.js.map +1 -1
- package/build/__tests__/GenericLocalMemoryCacher-test.d.ts +1 -0
- package/build/__tests__/GenericLocalMemoryCacher-test.js +31 -0
- package/build/__tests__/GenericLocalMemoryCacher-test.js.map +1 -0
- package/build/__tests__/LocalMemoryCacheAdapter-test.js +11 -25
- package/build/__tests__/LocalMemoryCacheAdapter-test.js.map +1 -1
- package/build/index.js +5 -1
- package/build/index.js.map +1 -1
- package/package.json +3 -3
- package/src/GenericLocalMemoryCacher.ts +4 -76
- package/src/LocalMemoryCacheAdapter.ts +1 -1
- package/src/__tests__/GenericLocalMemoryCacher-test.ts +28 -0
- package/src/__tests__/LocalMemoryCacheAdapter-test.ts +6 -24
package/README.md
CHANGED
|
@@ -3,23 +3,26 @@
|
|
|
3
3
|
Cross-request [LRU](https://github.com/isaacs/node-lru-cache) cache adapter for `@expo/entity`. Use
|
|
4
4
|
this cache with caution - it is nonstandard. The cache is shared between requests in the node process.
|
|
5
5
|
|
|
6
|
+
Note: This uses version 6.0.0 of `node-lru-cache` since it the most tuned version for our use case (low TTL + LRU). Upgrading
|
|
7
|
+
to 7.x will cause high memory usage for the entity cache adapter use case since it allocates fixed-size data structures up front to tune for the non-TTL use case: https://github.com/isaacs/node-lru-cache/issues/208.
|
|
8
|
+
|
|
6
9
|
[Documentation](https://expo.github.io/entity/modules/_expo_cache_adapter_local_memory.html)
|
|
7
10
|
|
|
8
11
|
## Why NOT use this cache
|
|
9
12
|
|
|
10
|
-
Because this is an in-memory cache, cross-
|
|
13
|
+
Because this is an in-memory cache, cross-machine invalidation is not possible. Do not use this cache
|
|
11
14
|
if you have the following use cases:
|
|
12
15
|
|
|
13
|
-
- The objects stored
|
|
16
|
+
- The objects stored are mutable
|
|
14
17
|
- Reading a stale object from the cache is not acceptable in your application
|
|
15
|
-
- Cross-
|
|
18
|
+
- Cross-machine invalidation is not possible
|
|
16
19
|
|
|
17
20
|
## Typical use cases
|
|
18
21
|
|
|
19
22
|
If your application sees many requests fetching the same objects, you can save a trip to your cache
|
|
20
23
|
cluster and backing datastore by using this in-memory cache. Here are some good use cases:
|
|
21
24
|
|
|
22
|
-
- The objects stored are mostly immutable
|
|
25
|
+
- The objects stored are mostly immutable, and reading a stale object for a short TTL is acceptable
|
|
23
26
|
- You have a low TTL setting in your cache
|
|
24
27
|
|
|
25
28
|
## Usage
|
|
@@ -18,12 +18,11 @@ class GenericLocalMemoryCacher {
|
|
|
18
18
|
const DEFAULT_LRU_CACHE_MAX_AGE_SECONDS = 10;
|
|
19
19
|
const DEFAULT_LRU_CACHE_SIZE = 10000;
|
|
20
20
|
const maxAgeSeconds = (_a = options.ttlSeconds) !== null && _a !== void 0 ? _a : DEFAULT_LRU_CACHE_MAX_AGE_SECONDS;
|
|
21
|
-
|
|
21
|
+
return new lru_cache_1.default({
|
|
22
22
|
max: (_b = options.maxSize) !== null && _b !== void 0 ? _b : DEFAULT_LRU_CACHE_SIZE,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
};
|
|
26
|
-
return new lru_cache_1.default(lruCacheOptions);
|
|
23
|
+
length: (value) => (value === exports.DOES_NOT_EXIST_LOCAL_MEMORY_CACHE ? 0 : 1),
|
|
24
|
+
maxAge: maxAgeSeconds * 1000, // convert to ms
|
|
25
|
+
});
|
|
27
26
|
}
|
|
28
27
|
static createNoOpCache() {
|
|
29
28
|
return new lru_cache_1.default({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GenericLocalMemoryCacher.js","sourceRoot":"","sources":["../src/GenericLocalMemoryCacher.ts"],"names":[],"mappings":";;;;;;AAAA,yCAAkF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C,SAAS,EAAE,KAAK,SAAS,EAAE,CAAC,CAChE,CAAC;QACF,OAAO,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACtC,CAAC;CACF;AAxED,2CAwEC"}
|
|
@@ -31,7 +31,7 @@ class LocalMemoryCacheAdapter extends entity_1.EntityCacheAdapter {
|
|
|
31
31
|
}
|
|
32
32
|
makeCacheKey(fieldName, fieldValue) {
|
|
33
33
|
const columnName = this.entityConfiguration.entityToDBFieldsKeyMapping.get(fieldName);
|
|
34
|
-
(0, invariant_1.default)(columnName, `database field mapping missing for ${fieldName}`);
|
|
34
|
+
(0, invariant_1.default)(columnName, `database field mapping missing for ${String(fieldName)}`);
|
|
35
35
|
const parts = [
|
|
36
36
|
this.entityConfiguration.tableName,
|
|
37
37
|
`${this.entityConfiguration.cacheKeyVersion}`,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LocalMemoryCacheAdapter.js","sourceRoot":"","sources":["../src/LocalMemoryCacheAdapter.ts"],"names":[],"mappings":";;;;;AAAA,yCAAiG;AACjG,0DAAkC;AAElC,0FAAwF;AAExF,MAAqB,uBAAiC,SAAQ,2BAA2B;IAGvF,YACE,mBAAiD,EACjD,gBAA2C;QAE3C,KAAK,CAAC,mBAAmB,CAAC,CAAC;QAC3B,IAAI,CAAC,wBAAwB,GAAG,IAAI,kCAAwB,CAAC,gBAAgB,CAAC,CAAC;IACjF,CAAC;IAEM,KAAK,CAAC,aAAa,CACxB,SAAY,EACZ,WAA+C;QAE/C,MAAM,sCAAsC,GAAG,IAAI,GAAG,CACpD,WAAW,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE,UAAU,CAAC,EAAE,UAAU,CAAC,CAAC,CACxF,CAAC;QACF,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,wBAAwB,CAAC,aAAa,CACpE,KAAK,CAAC,IAAI,CAAC,sCAAsC,CAAC,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ACJ,CAAC;IAEM,KAAK,CAAC,kBAAkB,CAC7B,SAAY,EACZ,WAA+C;QAE/C,MAAM,IAAI,CAAC,wBAAwB,CAAC,kBAAkB,CACpD,WAAW,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC,CAC1E,CAAC;IACJ,CAAC;IAEM,KAAK,CAAC,mBAAmB,CAC9B,SAAY,EACZ,WAA+C;QAE/C,MAAM,IAAI,CAAC,wBAAwB,CAAC,mBAAmB,CACrD,WAAW,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC,CAC1E,CAAC;IACJ,CAAC;IAEO,YAAY,CAClB,SAAY,EACZ,UAAmC;QAEnC,MAAM,UAAU,GAAG,IAAI,CAAC,mBAAmB,CAAC,0BAA0B,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACtF,IAAA,mBAAS,EAAC,UAAU,EAAE,sCAAsC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;QACjF,MAAM,KAAK,GAAG;YACZ,IAAI,CAAC,mBAAmB,CAAC,SAAS;YAClC,GAAG,IAAI,CAAC,mBAAmB,CAAC,eAAe,EAAE;YAC7C,UAAU;YACV,MAAM,CAAC,UAAU,CAAC;SACnB,CAAC;QACF,OAAO,IAAI,CAAC,wBAAwB,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAC3D,CAAC;CACF;AA1ED,0CA0EC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const GenericLocalMemoryCacher_1 = __importDefault(require("../GenericLocalMemoryCacher"));
|
|
7
|
+
describe(GenericLocalMemoryCacher_1.default, () => {
|
|
8
|
+
describe(GenericLocalMemoryCacher_1.default.createLRUCache, () => {
|
|
9
|
+
it('creates a cache with default options', () => {
|
|
10
|
+
const cache = GenericLocalMemoryCacher_1.default.createLRUCache();
|
|
11
|
+
expect(cache.max).toBe(10000);
|
|
12
|
+
expect(cache.maxAge).toBe(10000);
|
|
13
|
+
});
|
|
14
|
+
it('respects specified options', () => {
|
|
15
|
+
const cache = GenericLocalMemoryCacher_1.default.createLRUCache({
|
|
16
|
+
ttlSeconds: 3,
|
|
17
|
+
maxSize: 10,
|
|
18
|
+
});
|
|
19
|
+
expect(cache.max).toBe(10);
|
|
20
|
+
expect(cache.maxAge).toBe(3000);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
describe(GenericLocalMemoryCacher_1.default.createNoOpCache, () => {
|
|
24
|
+
it('creates a no-op cache', () => {
|
|
25
|
+
const cache = GenericLocalMemoryCacher_1.default.createNoOpCache();
|
|
26
|
+
cache.set('a', { hello: 'world' });
|
|
27
|
+
expect(cache.get('a')).toBeUndefined();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=GenericLocalMemoryCacher-test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GenericLocalMemoryCacher-test.js","sourceRoot":"","sources":["../../src/__tests__/GenericLocalMemoryCacher-test.ts"],"names":[],"mappings":";;;;;AAAA,2FAAmE;AAEnE,QAAQ,CAAC,kCAAwB,EAAE,GAAG,EAAE;IACtC,QAAQ,CAAC,kCAAwB,CAAC,cAAc,EAAE,GAAG,EAAE;QACrD,EAAE,CAAC,sCAAsC,EAAE,GAAG,EAAE;YAC9C,MAAM,KAAK,GAAG,kCAAwB,CAAC,cAAc,EAAE,CAAC;YACxD,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC9B,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnC,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,4BAA4B,EAAE,GAAG,EAAE;YACpC,MAAM,KAAK,GAAG,kCAAwB,CAAC,cAAc,CAAC;gBACpD,UAAU,EAAE,CAAC;gBACb,OAAO,EAAE,EAAE;aACZ,CAAC,CAAC;YACH,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAC3B,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,kCAAwB,CAAC,eAAe,EAAE,GAAG,EAAE;QACtD,EAAE,CAAC,uBAAuB,EAAE,GAAG,EAAE;YAC/B,MAAM,KAAK,GAAG,kCAAwB,CAAC,eAAe,EAAsB,CAAC;YAC7E,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;YACnC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC;QACzC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -37,10 +41,7 @@ const entityConfiguration = new entity_1.EntityConfiguration({
|
|
|
37
41
|
describe(LocalMemoryCacheAdapter_1.default, () => {
|
|
38
42
|
describe('loadManyAsync', () => {
|
|
39
43
|
it('returns appropriate cache results', async () => {
|
|
40
|
-
const cacheAdapter = new LocalMemoryCacheAdapter_1.default(entityConfiguration, GenericLocalMemoryCacher_1.default.createLRUCache(
|
|
41
|
-
maxSize: Number.MAX_SAFE_INTEGER,
|
|
42
|
-
ttlSeconds: Number.MAX_SAFE_INTEGER,
|
|
43
|
-
}));
|
|
44
|
+
const cacheAdapter = new LocalMemoryCacheAdapter_1.default(entityConfiguration, GenericLocalMemoryCacher_1.default.createLRUCache());
|
|
44
45
|
const cacheHits = new Map([['test-id-1', { id: 'test-id-1' }]]);
|
|
45
46
|
await cacheAdapter.cacheManyAsync('id', cacheHits);
|
|
46
47
|
await cacheAdapter.cacheDBMissesAsync('id', ['test-id-2']);
|
|
@@ -58,20 +59,14 @@ describe(LocalMemoryCacheAdapter_1.default, () => {
|
|
|
58
59
|
expect(results.size).toBe(3);
|
|
59
60
|
});
|
|
60
61
|
it('returns empty map when passed empty array of fieldValues', async () => {
|
|
61
|
-
const cacheAdapter = new LocalMemoryCacheAdapter_1.default(entityConfiguration, GenericLocalMemoryCacher_1.default.createLRUCache(
|
|
62
|
-
maxSize: Number.MAX_SAFE_INTEGER,
|
|
63
|
-
ttlSeconds: Number.MAX_SAFE_INTEGER,
|
|
64
|
-
}));
|
|
62
|
+
const cacheAdapter = new LocalMemoryCacheAdapter_1.default(entityConfiguration, GenericLocalMemoryCacher_1.default.createLRUCache());
|
|
65
63
|
const results = await cacheAdapter.loadManyAsync('id', []);
|
|
66
64
|
expect(results).toEqual(new Map());
|
|
67
65
|
});
|
|
68
66
|
});
|
|
69
67
|
describe('cacheManyAsync', () => {
|
|
70
68
|
it('correctly caches all objects', async () => {
|
|
71
|
-
const localMemoryCache = GenericLocalMemoryCacher_1.default.createLRUCache({
|
|
72
|
-
maxSize: Number.MAX_SAFE_INTEGER,
|
|
73
|
-
ttlSeconds: Number.MAX_SAFE_INTEGER,
|
|
74
|
-
});
|
|
69
|
+
const localMemoryCache = GenericLocalMemoryCacher_1.default.createLRUCache({});
|
|
75
70
|
const cacheAdapter = new LocalMemoryCacheAdapter_1.default(entityConfiguration, localMemoryCache);
|
|
76
71
|
await cacheAdapter.cacheManyAsync('id', new Map([['test-id-1', { id: 'test-id-1' }]]));
|
|
77
72
|
const cacheKey = cacheAdapter['makeCacheKey']('id', 'test-id-1');
|
|
@@ -82,10 +77,7 @@ describe(LocalMemoryCacheAdapter_1.default, () => {
|
|
|
82
77
|
});
|
|
83
78
|
describe('cacheDBMissesAsync', () => {
|
|
84
79
|
it('correctly caches misses', async () => {
|
|
85
|
-
const localMemoryCache = GenericLocalMemoryCacher_1.default.createLRUCache({
|
|
86
|
-
maxSize: Number.MAX_SAFE_INTEGER,
|
|
87
|
-
ttlSeconds: Number.MAX_SAFE_INTEGER,
|
|
88
|
-
});
|
|
80
|
+
const localMemoryCache = GenericLocalMemoryCacher_1.default.createLRUCache({});
|
|
89
81
|
const cacheAdapter = new LocalMemoryCacheAdapter_1.default(entityConfiguration, localMemoryCache);
|
|
90
82
|
await cacheAdapter.cacheDBMissesAsync('id', ['test-id-1']);
|
|
91
83
|
const cacheKey = cacheAdapter['makeCacheKey']('id', 'test-id-1');
|
|
@@ -94,10 +86,7 @@ describe(LocalMemoryCacheAdapter_1.default, () => {
|
|
|
94
86
|
});
|
|
95
87
|
describe('invalidateManyAsync', () => {
|
|
96
88
|
it('invalidates correctly', async () => {
|
|
97
|
-
const localMemoryCache = GenericLocalMemoryCacher_1.default.createLRUCache({
|
|
98
|
-
maxSize: Number.MAX_SAFE_INTEGER,
|
|
99
|
-
ttlSeconds: Number.MAX_SAFE_INTEGER,
|
|
100
|
-
});
|
|
89
|
+
const localMemoryCache = GenericLocalMemoryCacher_1.default.createLRUCache({});
|
|
101
90
|
const cacheAdapter = new LocalMemoryCacheAdapter_1.default(entityConfiguration, localMemoryCache);
|
|
102
91
|
await cacheAdapter.cacheManyAsync('id', new Map([['test-id-1', { id: 'test-id-1' }]]));
|
|
103
92
|
await cacheAdapter.cacheDBMissesAsync('id', ['test-id-2']);
|
|
@@ -107,10 +96,7 @@ describe(LocalMemoryCacheAdapter_1.default, () => {
|
|
|
107
96
|
expect(results.get('test-id-2')).toMatchObject({ status: entity_1.CacheStatus.MISS });
|
|
108
97
|
});
|
|
109
98
|
it('returns when passed empty array of fieldValues', async () => {
|
|
110
|
-
const cacheAdapter = new LocalMemoryCacheAdapter_1.default(entityConfiguration, GenericLocalMemoryCacher_1.default.createLRUCache({
|
|
111
|
-
maxSize: Number.MAX_SAFE_INTEGER,
|
|
112
|
-
ttlSeconds: Number.MAX_SAFE_INTEGER,
|
|
113
|
-
}));
|
|
99
|
+
const cacheAdapter = new LocalMemoryCacheAdapter_1.default(entityConfiguration, GenericLocalMemoryCacher_1.default.createLRUCache({}));
|
|
114
100
|
await cacheAdapter.invalidateManyAsync('id', []);
|
|
115
101
|
});
|
|
116
102
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LocalMemoryCacheAdapter-test.js","sourceRoot":"","sources":["../../src/__tests__/LocalMemoryCacheAdapter-test.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"LocalMemoryCacheAdapter-test.js","sourceRoot":"","sources":["../../src/__tests__/LocalMemoryCacheAdapter-test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAA2E;AAE3E,wFAEqC;AACrC,yFAAiE;AAMjE,MAAM,mBAAmB,GAAG,IAAI,4BAAmB,CAAa;IAC9D,OAAO,EAAE,IAAI;IACb,SAAS,EAAE,MAAM;IACjB,MAAM,EAAE;QACN,EAAE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EAAE,MAAM,EAAE,oBAAW,CAAC,IAAI,EAAE,CAAC,CAAC;YAC7E,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,aAAa,CAAC,EAAE,MAAM,EAAE,oBAAW,CAAC,IAAI,EAAE,CAAC,CAAC;QAC/E,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,gDAAgD,EAAE,KAAK,IAAI,EAAE;YAC9D,MAAM,YAAY,GAAG,IAAI,iCAAuB,CAC9C,mBAAmB,EACnB,kCAAwB,CAAC,cAAc,CAAa,EAAE,CAAC,CACxD,CAAC;YACF,MAAM,YAAY,CAAC,mBAAmB,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACnD,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/build/index.js
CHANGED
|
@@ -6,7 +6,11 @@
|
|
|
6
6
|
*/
|
|
7
7
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
8
|
if (k2 === undefined) k2 = k;
|
|
9
|
-
Object.
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
10
14
|
}) : (function(o, m, k, k2) {
|
|
11
15
|
if (k2 === undefined) k2 = k;
|
|
12
16
|
o[k2] = m[k];
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,iCAAiC;AACjC;;;GAGG
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,iCAAiC;AACjC;;;GAGG;;;;;;;;;;;;;;;;;;;;AAEH,uEAAiF;AAAxE,qJAAA,OAAO,OAA4B;AAC5C,6DAA2C;AAC3C,qEAA+E;AAAtE,mJAAA,OAAO,OAA2B;AAC3C,qFAA+F;AAAtF,mKAAA,OAAO,OAAmC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@expo/entity-cache-adapter-local-memory",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.0",
|
|
4
4
|
"description": "Cross-request local memory cache adapter for @expo/entity",
|
|
5
5
|
"files": [
|
|
6
6
|
"build",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"@expo/entity": "*"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"lru-cache": "^
|
|
33
|
+
"lru-cache": "^6.0.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@expo/entity": "^0.
|
|
36
|
+
"@expo/entity": "^0.26.0"
|
|
37
37
|
}
|
|
38
38
|
}
|
|
@@ -7,76 +7,6 @@ export const DOES_NOT_EXIST_LOCAL_MEMORY_CACHE = Symbol('doesNotExist');
|
|
|
7
7
|
type LocalMemoryCacheValue<TFields> = Readonly<TFields> | typeof DOES_NOT_EXIST_LOCAL_MEMORY_CACHE;
|
|
8
8
|
export type LocalMemoryCache<TFields> = LRUCache<string, LocalMemoryCacheValue<TFields>>;
|
|
9
9
|
|
|
10
|
-
type LRUCacheOptionsV7<K, V> = {
|
|
11
|
-
/**
|
|
12
|
-
* the number of most recently used items to keep.
|
|
13
|
-
* note that we may store fewer items than this if maxSize is hit.
|
|
14
|
-
*/
|
|
15
|
-
max: number;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* if you wish to track item size, you must provide a maxSize
|
|
19
|
-
* note that we still will only keep up to max *actual items*,
|
|
20
|
-
* so size tracking may cause fewer than max items to be stored.
|
|
21
|
-
* At the extreme, a single item of maxSize size will cause everything
|
|
22
|
-
* else in the cache to be dropped when it is added. Use with caution!
|
|
23
|
-
* Note also that size tracking can negatively impact performance,
|
|
24
|
-
* though for most cases, only minimally.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
maxSize?: number;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* function to calculate size of items. useful if storing strings or
|
|
31
|
-
* buffers or other items where memory size depends on the object itself.
|
|
32
|
-
* also note that oversized items do NOT immediately get dropped from
|
|
33
|
-
* the cache, though they will cause faster turnover in the storage.
|
|
34
|
-
*/
|
|
35
|
-
sizeCalculation?: (value: V, key: K) => number;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* function to call when the item is removed from the cache
|
|
39
|
-
* Note that using this can negatively impact performance.
|
|
40
|
-
*/
|
|
41
|
-
dispose?: (value: V, key: K) => void;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* max time to live for items before they are considered stale
|
|
45
|
-
* note that stale items are NOT preemptively removed by default,
|
|
46
|
-
* and MAY live in the cache, contributing to its LRU max, long after
|
|
47
|
-
* they have expired.
|
|
48
|
-
* Also, as this cache is optimized for LRU/MRU operations, some of
|
|
49
|
-
* the staleness/TTL checks will reduce performance, as they will incur
|
|
50
|
-
* overhead by deleting items.
|
|
51
|
-
* Must be a positive integer in ms, defaults to 0, which means "no TTL"
|
|
52
|
-
*/
|
|
53
|
-
ttl?: number;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* return stale items from cache.get() before disposing of them
|
|
57
|
-
* boolean, default false
|
|
58
|
-
*/
|
|
59
|
-
allowStale?: boolean;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* update the age of items on cache.get(), renewing their TTL
|
|
63
|
-
* boolean, default false
|
|
64
|
-
*/
|
|
65
|
-
updateAgeOnGet?: boolean;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* update the age of items on cache.has(), renewing their TTL
|
|
69
|
-
* boolean, default false
|
|
70
|
-
*/
|
|
71
|
-
updateAgeOnHas?: boolean;
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* update the "recently-used"-ness of items on cache.has()
|
|
75
|
-
* boolean, default false
|
|
76
|
-
*/
|
|
77
|
-
updateRecencyOnHas?: boolean;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
10
|
export default class GenericLocalMemoryCacher<TFields> implements IEntityGenericCacher<TFields> {
|
|
81
11
|
constructor(private readonly localMemoryCache: LocalMemoryCache<TFields>) {}
|
|
82
12
|
|
|
@@ -86,13 +16,11 @@ export default class GenericLocalMemoryCacher<TFields> implements IEntityGeneric
|
|
|
86
16
|
const DEFAULT_LRU_CACHE_MAX_AGE_SECONDS = 10;
|
|
87
17
|
const DEFAULT_LRU_CACHE_SIZE = 10000;
|
|
88
18
|
const maxAgeSeconds = options.ttlSeconds ?? DEFAULT_LRU_CACHE_MAX_AGE_SECONDS;
|
|
89
|
-
|
|
19
|
+
return new LRUCache<string, LocalMemoryCacheValue<TFields>>({
|
|
90
20
|
max: options.maxSize ?? DEFAULT_LRU_CACHE_SIZE,
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
};
|
|
95
|
-
return new LRUCache<string, LocalMemoryCacheValue<TFields>>(lruCacheOptions as any);
|
|
21
|
+
length: (value) => (value === DOES_NOT_EXIST_LOCAL_MEMORY_CACHE ? 0 : 1),
|
|
22
|
+
maxAge: maxAgeSeconds * 1000, // convert to ms
|
|
23
|
+
});
|
|
96
24
|
}
|
|
97
25
|
|
|
98
26
|
static createNoOpCache<TFields>(): LocalMemoryCache<TFields> {
|
|
@@ -68,7 +68,7 @@ export default class LocalMemoryCacheAdapter<TFields> extends EntityCacheAdapter
|
|
|
68
68
|
fieldValue: NonNullable<TFields[N]>
|
|
69
69
|
): string {
|
|
70
70
|
const columnName = this.entityConfiguration.entityToDBFieldsKeyMapping.get(fieldName);
|
|
71
|
-
invariant(columnName, `database field mapping missing for ${fieldName}`);
|
|
71
|
+
invariant(columnName, `database field mapping missing for ${String(fieldName)}`);
|
|
72
72
|
const parts = [
|
|
73
73
|
this.entityConfiguration.tableName,
|
|
74
74
|
`${this.entityConfiguration.cacheKeyVersion}`,
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import GenericLocalMemoryCacher from '../GenericLocalMemoryCacher';
|
|
2
|
+
|
|
3
|
+
describe(GenericLocalMemoryCacher, () => {
|
|
4
|
+
describe(GenericLocalMemoryCacher.createLRUCache, () => {
|
|
5
|
+
it('creates a cache with default options', () => {
|
|
6
|
+
const cache = GenericLocalMemoryCacher.createLRUCache();
|
|
7
|
+
expect(cache.max).toBe(10000);
|
|
8
|
+
expect(cache.maxAge).toBe(10000);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('respects specified options', () => {
|
|
12
|
+
const cache = GenericLocalMemoryCacher.createLRUCache({
|
|
13
|
+
ttlSeconds: 3,
|
|
14
|
+
maxSize: 10,
|
|
15
|
+
});
|
|
16
|
+
expect(cache.max).toBe(10);
|
|
17
|
+
expect(cache.maxAge).toBe(3000);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe(GenericLocalMemoryCacher.createNoOpCache, () => {
|
|
22
|
+
it('creates a no-op cache', () => {
|
|
23
|
+
const cache = GenericLocalMemoryCacher.createNoOpCache<{ hello: 'world' }>();
|
|
24
|
+
cache.set('a', { hello: 'world' });
|
|
25
|
+
expect(cache.get('a')).toBeUndefined();
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -24,10 +24,7 @@ describe(LocalMemoryCacheAdapter, () => {
|
|
|
24
24
|
it('returns appropriate cache results', async () => {
|
|
25
25
|
const cacheAdapter = new LocalMemoryCacheAdapter(
|
|
26
26
|
entityConfiguration,
|
|
27
|
-
GenericLocalMemoryCacher.createLRUCache(
|
|
28
|
-
maxSize: Number.MAX_SAFE_INTEGER,
|
|
29
|
-
ttlSeconds: Number.MAX_SAFE_INTEGER,
|
|
30
|
-
})
|
|
27
|
+
GenericLocalMemoryCacher.createLRUCache()
|
|
31
28
|
);
|
|
32
29
|
|
|
33
30
|
const cacheHits = new Map<string, Readonly<BlahFields>>([['test-id-1', { id: 'test-id-1' }]]);
|
|
@@ -52,10 +49,7 @@ describe(LocalMemoryCacheAdapter, () => {
|
|
|
52
49
|
it('returns empty map when passed empty array of fieldValues', async () => {
|
|
53
50
|
const cacheAdapter = new LocalMemoryCacheAdapter(
|
|
54
51
|
entityConfiguration,
|
|
55
|
-
GenericLocalMemoryCacher.createLRUCache(
|
|
56
|
-
maxSize: Number.MAX_SAFE_INTEGER,
|
|
57
|
-
ttlSeconds: Number.MAX_SAFE_INTEGER,
|
|
58
|
-
})
|
|
52
|
+
GenericLocalMemoryCacher.createLRUCache()
|
|
59
53
|
);
|
|
60
54
|
const results = await cacheAdapter.loadManyAsync('id', []);
|
|
61
55
|
expect(results).toEqual(new Map());
|
|
@@ -64,10 +58,7 @@ describe(LocalMemoryCacheAdapter, () => {
|
|
|
64
58
|
|
|
65
59
|
describe('cacheManyAsync', () => {
|
|
66
60
|
it('correctly caches all objects', async () => {
|
|
67
|
-
const localMemoryCache = GenericLocalMemoryCacher.createLRUCache<BlahFields>({
|
|
68
|
-
maxSize: Number.MAX_SAFE_INTEGER,
|
|
69
|
-
ttlSeconds: Number.MAX_SAFE_INTEGER,
|
|
70
|
-
});
|
|
61
|
+
const localMemoryCache = GenericLocalMemoryCacher.createLRUCache<BlahFields>({});
|
|
71
62
|
|
|
72
63
|
const cacheAdapter = new LocalMemoryCacheAdapter(entityConfiguration, localMemoryCache);
|
|
73
64
|
await cacheAdapter.cacheManyAsync('id', new Map([['test-id-1', { id: 'test-id-1' }]]));
|
|
@@ -81,10 +72,7 @@ describe(LocalMemoryCacheAdapter, () => {
|
|
|
81
72
|
|
|
82
73
|
describe('cacheDBMissesAsync', () => {
|
|
83
74
|
it('correctly caches misses', async () => {
|
|
84
|
-
const localMemoryCache = GenericLocalMemoryCacher.createLRUCache<BlahFields>({
|
|
85
|
-
maxSize: Number.MAX_SAFE_INTEGER,
|
|
86
|
-
ttlSeconds: Number.MAX_SAFE_INTEGER,
|
|
87
|
-
});
|
|
75
|
+
const localMemoryCache = GenericLocalMemoryCacher.createLRUCache<BlahFields>({});
|
|
88
76
|
|
|
89
77
|
const cacheAdapter = new LocalMemoryCacheAdapter(entityConfiguration, localMemoryCache);
|
|
90
78
|
await cacheAdapter.cacheDBMissesAsync('id', ['test-id-1']);
|
|
@@ -96,10 +84,7 @@ describe(LocalMemoryCacheAdapter, () => {
|
|
|
96
84
|
|
|
97
85
|
describe('invalidateManyAsync', () => {
|
|
98
86
|
it('invalidates correctly', async () => {
|
|
99
|
-
const localMemoryCache = GenericLocalMemoryCacher.createLRUCache<BlahFields>({
|
|
100
|
-
maxSize: Number.MAX_SAFE_INTEGER,
|
|
101
|
-
ttlSeconds: Number.MAX_SAFE_INTEGER,
|
|
102
|
-
});
|
|
87
|
+
const localMemoryCache = GenericLocalMemoryCacher.createLRUCache<BlahFields>({});
|
|
103
88
|
|
|
104
89
|
const cacheAdapter = new LocalMemoryCacheAdapter(entityConfiguration, localMemoryCache);
|
|
105
90
|
await cacheAdapter.cacheManyAsync('id', new Map([['test-id-1', { id: 'test-id-1' }]]));
|
|
@@ -114,10 +99,7 @@ describe(LocalMemoryCacheAdapter, () => {
|
|
|
114
99
|
it('returns when passed empty array of fieldValues', async () => {
|
|
115
100
|
const cacheAdapter = new LocalMemoryCacheAdapter(
|
|
116
101
|
entityConfiguration,
|
|
117
|
-
GenericLocalMemoryCacher.createLRUCache<BlahFields>({
|
|
118
|
-
maxSize: Number.MAX_SAFE_INTEGER,
|
|
119
|
-
ttlSeconds: Number.MAX_SAFE_INTEGER,
|
|
120
|
-
})
|
|
102
|
+
GenericLocalMemoryCacher.createLRUCache<BlahFields>({})
|
|
121
103
|
);
|
|
122
104
|
await cacheAdapter.invalidateManyAsync('id', []);
|
|
123
105
|
});
|