@autofleet/matmon 2.1.7 → 2.2.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/lib/cache.d.ts +15 -23
- package/lib/cache.js +23 -20
- package/lib/locking.d.ts +1 -1
- package/lib/locking.js +3 -4
- package/lib/logger.js +1 -1
- package/lib/orm-cache/adapter.d.ts +1 -1
- package/lib/orm-cache/index.js +2 -3
- package/lib/orm-cache/sequelize-adapter.js +3 -3
- package/lib/redis/index.js +1 -4
- package/lib/redis/index.test.d.ts +0 -0
- package/lib/redis/index.test.js +0 -0
- package/package.json +4 -8
package/lib/cache.d.ts
CHANGED
|
@@ -1,24 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
interface GetMultipleWithCacheOptions<V = unknown> {
|
|
16
|
-
getFromCache?: (query: any) => Promise<V>;
|
|
17
|
-
multiGetterFromCache?: (queries: any[]) => Promise<V[]>;
|
|
18
|
-
setInCache: (key: string, value: V) => Promise<void>;
|
|
19
|
-
getter?: (query: any) => Promise<V>;
|
|
20
|
-
multiGetter?: (queries: any[]) => Promise<V[]>;
|
|
1
|
+
export declare const getNewLRU: (lifeTimeInSec: any, size: any) => any;
|
|
2
|
+
export declare const getWithCacheSupport: ({ cacheKey, cacheGet, cacheSet, fetching, skipCache, }: {
|
|
3
|
+
cacheKey: any;
|
|
4
|
+
cacheGet: any;
|
|
5
|
+
cacheSet: any;
|
|
6
|
+
fetching: any;
|
|
7
|
+
skipCache: any;
|
|
8
|
+
}) => Promise<any>;
|
|
9
|
+
export declare const getMultipleWithCache: ({ getFromCache, multiGetterFromCache, setInCache, getter, multiGetter, idField, }: {
|
|
10
|
+
getFromCache?: any;
|
|
11
|
+
multiGetterFromCache?: any;
|
|
12
|
+
setInCache: any;
|
|
13
|
+
getter?: any;
|
|
14
|
+
multiGetter?: any;
|
|
21
15
|
idField?: string;
|
|
22
|
-
}
|
|
23
|
-
export declare const getMultipleWithCache: <V = unknown>({ getFromCache, multiGetterFromCache, setInCache, getter, multiGetter, idField, }: GetMultipleWithCacheOptions<V>) => (queries: any[]) => Promise<V[]>;
|
|
24
|
-
export {};
|
|
16
|
+
}) => (queries: any[]) => Promise<any[]>;
|
package/lib/cache.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
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
|
-
|
|
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);
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
9
5
|
}) : (function(o, m, k, k2) {
|
|
10
6
|
if (k2 === undefined) k2 = k;
|
|
11
7
|
o[k2] = m[k];
|
|
@@ -18,7 +14,7 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
18
14
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
15
|
if (mod && mod.__esModule) return mod;
|
|
20
16
|
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
18
|
__setModuleDefault(result, mod);
|
|
23
19
|
return result;
|
|
24
20
|
};
|
|
@@ -39,7 +35,9 @@ const getOptions = ({ lifeTimeInSec, size = DEFAULT_CACHE_SIZE, }) => ({
|
|
|
39
35
|
maxAge: process.env.NODE_ENV !== 'test' ? 1000 * lifeTimeInSec : 0,
|
|
40
36
|
});
|
|
41
37
|
const getMutexByCacheKey = (key) => {
|
|
42
|
-
|
|
38
|
+
if (!MUTEX_MAP[key]) {
|
|
39
|
+
MUTEX_MAP[key] = locking.getMutex();
|
|
40
|
+
}
|
|
43
41
|
return MUTEX_MAP[key];
|
|
44
42
|
};
|
|
45
43
|
const deleteMutexByCacheKey = (key) => {
|
|
@@ -47,12 +45,11 @@ const deleteMutexByCacheKey = (key) => {
|
|
|
47
45
|
delete MUTEX_MAP[key];
|
|
48
46
|
}
|
|
49
47
|
};
|
|
50
|
-
|
|
48
|
+
exports.getNewLRU = (lifeTimeInSec, size) => new lru_cache_1.default(getOptions({
|
|
51
49
|
lifeTimeInSec,
|
|
52
50
|
size,
|
|
53
51
|
}));
|
|
54
|
-
exports.
|
|
55
|
-
const getWithCacheSupport = async ({ cacheKey, cacheGet, cacheSet, fetching, skipCache, }) => {
|
|
52
|
+
exports.getWithCacheSupport = async ({ cacheKey, cacheGet, cacheSet, fetching, skipCache, }) => {
|
|
56
53
|
if (skipCache || process.env.NODE_ENV === 'test') {
|
|
57
54
|
const res = await fetching();
|
|
58
55
|
await cacheSet(res);
|
|
@@ -80,29 +77,36 @@ const getWithCacheSupport = async ({ cacheKey, cacheGet, cacheSet, fetching, ski
|
|
|
80
77
|
}
|
|
81
78
|
return valueToReturn;
|
|
82
79
|
};
|
|
83
|
-
exports.getWithCacheSupport = getWithCacheSupport;
|
|
84
80
|
const getIdField = (query, idField) => {
|
|
85
81
|
if (typeof query === 'string') {
|
|
86
82
|
return query;
|
|
87
83
|
}
|
|
88
84
|
return query[idField];
|
|
89
85
|
};
|
|
90
|
-
|
|
86
|
+
exports.getMultipleWithCache = ({ getFromCache = undefined, multiGetterFromCache = undefined, setInCache, getter = undefined, multiGetter = undefined, idField = 'id', }) => async (queries) => {
|
|
91
87
|
const queriesMap = new Map(queries.filter(Boolean).map(query => [getIdField(query, idField), query]));
|
|
92
88
|
const resultMap = new Map();
|
|
93
89
|
const valuesToPullFromCache = [...queriesMap.values()];
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
90
|
+
let valuesFromCache;
|
|
91
|
+
if (multiGetterFromCache) {
|
|
92
|
+
valuesFromCache = await multiGetterFromCache(valuesToPullFromCache);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
valuesFromCache = await Promise.all(valuesToPullFromCache.map(query => getFromCache(query)));
|
|
96
|
+
}
|
|
97
|
+
valuesFromCache.filter(Boolean).map((value) => {
|
|
98
98
|
queriesMap.delete(getIdField(value, idField));
|
|
99
99
|
resultMap.set(getIdField(value, idField), value);
|
|
100
|
+
return value;
|
|
100
101
|
});
|
|
101
102
|
let valuesFromGetter = [];
|
|
102
103
|
if (queriesMap.size > 0) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
if (multiGetter) {
|
|
105
|
+
valuesFromGetter = await multiGetter([...queriesMap.values()]);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
valuesFromGetter = await Promise.all([...queriesMap.values()].map(id => getter(id)));
|
|
109
|
+
}
|
|
106
110
|
valuesFromGetter.forEach((value) => {
|
|
107
111
|
setInCache(value[idField], value);
|
|
108
112
|
resultMap.set(getIdField(value, idField), value);
|
|
@@ -115,4 +119,3 @@ const getMultipleWithCache = ({ getFromCache, multiGetterFromCache, setInCache,
|
|
|
115
119
|
return resultMap.get(getIdField(query, idField));
|
|
116
120
|
});
|
|
117
121
|
};
|
|
118
|
-
exports.getMultipleWithCache = getMultipleWithCache;
|
package/lib/locking.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { MutexInterface } from 'async-mutex';
|
|
2
|
-
export declare const wrapWithMutex: (mutex:
|
|
2
|
+
export declare const wrapWithMutex: (mutex: any, funcToRun: any) => Promise<void>;
|
|
3
3
|
export declare const getMutex: () => MutexInterface;
|
package/lib/locking.js
CHANGED
|
@@ -4,7 +4,8 @@ exports.getMutex = exports.wrapWithMutex = void 0;
|
|
|
4
4
|
const async_mutex_1 = require("async-mutex");
|
|
5
5
|
const CACHE_LOCK_TIMEOUT_MILIS = 3000;
|
|
6
6
|
const LOCK_TIMEOUT_MESSAGE = 'mutex - locking timeout';
|
|
7
|
-
|
|
7
|
+
// eslint-disable-next-line import/prefer-default-export
|
|
8
|
+
exports.wrapWithMutex = async (mutex, funcToRun) => {
|
|
8
9
|
const release = await mutex.acquire();
|
|
9
10
|
try {
|
|
10
11
|
await funcToRun();
|
|
@@ -13,6 +14,4 @@ const wrapWithMutex = async (mutex, funcToRun) => {
|
|
|
13
14
|
release();
|
|
14
15
|
}
|
|
15
16
|
};
|
|
16
|
-
exports.
|
|
17
|
-
const getMutex = () => (0, async_mutex_1.withTimeout)(new async_mutex_1.Mutex(), CACHE_LOCK_TIMEOUT_MILIS, new Error(LOCK_TIMEOUT_MESSAGE));
|
|
18
|
-
exports.getMutex = getMutex;
|
|
17
|
+
exports.getMutex = () => async_mutex_1.withTimeout(new async_mutex_1.Mutex(), CACHE_LOCK_TIMEOUT_MILIS, new Error(LOCK_TIMEOUT_MESSAGE));
|
package/lib/logger.js
CHANGED
|
@@ -4,5 +4,5 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const logger_1 = __importDefault(require("@autofleet/logger"));
|
|
7
|
-
const logger =
|
|
7
|
+
const logger = logger_1.default();
|
|
8
8
|
exports.default = logger;
|
package/lib/orm-cache/index.js
CHANGED
|
@@ -11,7 +11,7 @@ const logger_1 = __importDefault(require("../logger"));
|
|
|
11
11
|
var ORMTypes;
|
|
12
12
|
(function (ORMTypes) {
|
|
13
13
|
ORMTypes["SEQUELIZE"] = "sequelize";
|
|
14
|
-
})(ORMTypes
|
|
14
|
+
})(ORMTypes = exports.ORMTypes || (exports.ORMTypes = {}));
|
|
15
15
|
const ORMInstanceFactory = (options) => {
|
|
16
16
|
switch (options.type) {
|
|
17
17
|
case ORMTypes.SEQUELIZE: {
|
|
@@ -22,7 +22,7 @@ const ORMInstanceFactory = (options) => {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
|
-
|
|
25
|
+
exports.ORMCache = (options) => {
|
|
26
26
|
const { models } = options;
|
|
27
27
|
logger_1.default.info('Starting ORM Cache', { options });
|
|
28
28
|
const adapter = ORMInstanceFactory(options);
|
|
@@ -36,4 +36,3 @@ const ORMCache = (options) => {
|
|
|
36
36
|
adapter.injectGetWithCacheFunction(cache, modelOptions);
|
|
37
37
|
});
|
|
38
38
|
};
|
|
39
|
-
exports.ORMCache = ORMCache;
|
|
@@ -83,7 +83,7 @@ class SequelizeAdapter {
|
|
|
83
83
|
const instanceKey = generateInstanceKey(modelOptions.name, instance.id);
|
|
84
84
|
this.debug('Adding dependencies', { instanceKey, dependencyKeys });
|
|
85
85
|
const addDependenciesMulti = cache.getClient().multi();
|
|
86
|
-
const addDependenciesMultiAsync =
|
|
86
|
+
const addDependenciesMultiAsync = util_1.promisify(addDependenciesMulti.exec).bind(addDependenciesMulti);
|
|
87
87
|
dependencyKeys.reduce((multi, key) => multi.sadd(key, instanceKey), addDependenciesMulti);
|
|
88
88
|
return addDependenciesMultiAsync();
|
|
89
89
|
};
|
|
@@ -113,7 +113,7 @@ class SequelizeAdapter {
|
|
|
113
113
|
const instanceKey = generateInstanceKey(modelOptions.name, instance.id);
|
|
114
114
|
this.debug('Removing dependencies', { instance, instanceKey, dependencyKeys });
|
|
115
115
|
const removeMulti = cache.getClient().multi();
|
|
116
|
-
const removeMultiAsync =
|
|
116
|
+
const removeMultiAsync = util_1.promisify(removeMulti.exec).bind(removeMulti);
|
|
117
117
|
dependencyKeys.map(key => removeMulti.srem(key, instanceKey));
|
|
118
118
|
removeMulti.del(instanceKey);
|
|
119
119
|
return removeMultiAsync();
|
|
@@ -122,7 +122,7 @@ class SequelizeAdapter {
|
|
|
122
122
|
const dependentInstancesKeys = await cache.getClient().smembersAsync(generateDependencyKey(modelOptions.name, association, associationId));
|
|
123
123
|
this.debug('Invalidating dependent instances', { dependentInstancesKeys });
|
|
124
124
|
const removeMulti = cache.getClient().multi();
|
|
125
|
-
const removeMultiAsync =
|
|
125
|
+
const removeMultiAsync = util_1.promisify(removeMulti.exec).bind(removeMulti);
|
|
126
126
|
const dependenciesToRemove = await Promise.all(dependentInstancesKeys.map(async (instanceKey) => {
|
|
127
127
|
const instance = JSON.parse(await cache.getClient().getAsync(instanceKey));
|
|
128
128
|
if (!instance) {
|
package/lib/redis/index.js
CHANGED
|
@@ -27,7 +27,7 @@ class RedisCache {
|
|
|
27
27
|
host: options.host || HOST,
|
|
28
28
|
port: options.port || PORT,
|
|
29
29
|
});
|
|
30
|
-
this.locker =
|
|
30
|
+
this.locker = util_1.promisify(redis_lock_1.default(this.client, options.lockRetries ?? 10));
|
|
31
31
|
this.lockTimeout = options.lockTimeout ?? DEFAULT_LOCK_TIMEOUT;
|
|
32
32
|
this.lockDuration = options.lockDuration ?? DEFAULT_LOCK_DURATION;
|
|
33
33
|
this.baseTTL = options.ttl ?? DEFAULT_BASE_TTL;
|
|
@@ -62,9 +62,6 @@ class RedisCache {
|
|
|
62
62
|
const keysWithPrefix = keys.map(key => KEY_PREFIX + key);
|
|
63
63
|
let values;
|
|
64
64
|
try {
|
|
65
|
-
if (keysWithPrefix.length === 0) {
|
|
66
|
-
return [];
|
|
67
|
-
}
|
|
68
65
|
// Try to get the value from redis.
|
|
69
66
|
values = await this.client.mgetAsync(keysWithPrefix);
|
|
70
67
|
}
|
|
File without changes
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/matmon",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "manage cache",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -27,6 +27,8 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/Autofleet/matmon",
|
|
29
29
|
"dependencies": {
|
|
30
|
+
"@autofleet/logger": "4.0.6",
|
|
31
|
+
"@types/node": "^14.14.20",
|
|
30
32
|
"async-mutex": "^0.2.6",
|
|
31
33
|
"bluebird": "^3.7.2",
|
|
32
34
|
"dotenv": "^8.2.0",
|
|
@@ -37,13 +39,7 @@
|
|
|
37
39
|
"sequelize-typescript": "^2.1.0"
|
|
38
40
|
},
|
|
39
41
|
"devDependencies": {
|
|
40
|
-
"
|
|
41
|
-
"@types/lru-cache": "^5.1.1",
|
|
42
|
-
"@types/node": "^14.14.20",
|
|
43
|
-
"typescript": "^5.5.2"
|
|
44
|
-
},
|
|
45
|
-
"peerDependencies": {
|
|
46
|
-
"@autofleet/logger": "^4"
|
|
42
|
+
"typescript": "^3.9.5"
|
|
47
43
|
},
|
|
48
44
|
"files": [
|
|
49
45
|
"lib/**/*"
|