@autofleet/matmon 2.1.5 → 2.1.7
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 +23 -15
- package/lib/cache.js +20 -23
- package/lib/locking.d.ts +1 -1
- package/lib/locking.js +4 -3
- package/lib/logger.d.ts +2 -0
- package/lib/logger.js +1 -1
- package/lib/orm-cache/adapter.d.ts +1 -1
- package/lib/orm-cache/index.js +3 -2
- package/lib/orm-cache/sequelize-adapter.js +3 -3
- package/lib/redis/index.js +4 -1
- package/package.json +8 -4
- package/lib/redis/index.test.d.ts +0 -0
- package/lib/redis/index.test.js +0 -0
package/lib/cache.d.ts
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import LRU from 'lru-cache';
|
|
2
|
+
interface Options {
|
|
3
|
+
lifeTimeInSec: number;
|
|
4
|
+
size?: number;
|
|
5
|
+
}
|
|
6
|
+
export declare const getNewLRU: <K = unknown, V = unknown>(lifeTimeInSec: Options["lifeTimeInSec"], size?: Options["size"]) => LRU<K, V>;
|
|
7
|
+
interface GetWithCacheOptions<V = unknown> {
|
|
8
|
+
cacheKey: string;
|
|
9
|
+
cacheGet: () => Promise<V>;
|
|
10
|
+
cacheSet: (value: V) => Promise<void>;
|
|
11
|
+
fetching: () => Promise<V>;
|
|
12
|
+
skipCache?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare const getWithCacheSupport: <V = unknown>({ cacheKey, cacheGet, cacheSet, fetching, skipCache, }: GetWithCacheOptions<V>) => Promise<any>;
|
|
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[]>;
|
|
15
21
|
idField?: string;
|
|
16
|
-
}
|
|
22
|
+
}
|
|
23
|
+
export declare const getMultipleWithCache: <V = unknown>({ getFromCache, multiGetterFromCache, setInCache, getter, multiGetter, idField, }: GetMultipleWithCacheOptions<V>) => (queries: any[]) => Promise<V[]>;
|
|
24
|
+
export {};
|
package/lib/cache.js
CHANGED
|
@@ -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];
|
|
@@ -14,7 +18,7 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
14
18
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
19
|
if (mod && mod.__esModule) return mod;
|
|
16
20
|
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
22
|
__setModuleDefault(result, mod);
|
|
19
23
|
return result;
|
|
20
24
|
};
|
|
@@ -35,9 +39,7 @@ const getOptions = ({ lifeTimeInSec, size = DEFAULT_CACHE_SIZE, }) => ({
|
|
|
35
39
|
maxAge: process.env.NODE_ENV !== 'test' ? 1000 * lifeTimeInSec : 0,
|
|
36
40
|
});
|
|
37
41
|
const getMutexByCacheKey = (key) => {
|
|
38
|
-
|
|
39
|
-
MUTEX_MAP[key] = locking.getMutex();
|
|
40
|
-
}
|
|
42
|
+
MUTEX_MAP[key] || (MUTEX_MAP[key] = locking.getMutex());
|
|
41
43
|
return MUTEX_MAP[key];
|
|
42
44
|
};
|
|
43
45
|
const deleteMutexByCacheKey = (key) => {
|
|
@@ -45,11 +47,12 @@ const deleteMutexByCacheKey = (key) => {
|
|
|
45
47
|
delete MUTEX_MAP[key];
|
|
46
48
|
}
|
|
47
49
|
};
|
|
48
|
-
|
|
50
|
+
const getNewLRU = (lifeTimeInSec, size) => new lru_cache_1.default(getOptions({
|
|
49
51
|
lifeTimeInSec,
|
|
50
52
|
size,
|
|
51
53
|
}));
|
|
52
|
-
exports.
|
|
54
|
+
exports.getNewLRU = getNewLRU;
|
|
55
|
+
const getWithCacheSupport = async ({ cacheKey, cacheGet, cacheSet, fetching, skipCache, }) => {
|
|
53
56
|
if (skipCache || process.env.NODE_ENV === 'test') {
|
|
54
57
|
const res = await fetching();
|
|
55
58
|
await cacheSet(res);
|
|
@@ -77,36 +80,29 @@ exports.getWithCacheSupport = async ({ cacheKey, cacheGet, cacheSet, fetching, s
|
|
|
77
80
|
}
|
|
78
81
|
return valueToReturn;
|
|
79
82
|
};
|
|
83
|
+
exports.getWithCacheSupport = getWithCacheSupport;
|
|
80
84
|
const getIdField = (query, idField) => {
|
|
81
85
|
if (typeof query === 'string') {
|
|
82
86
|
return query;
|
|
83
87
|
}
|
|
84
88
|
return query[idField];
|
|
85
89
|
};
|
|
86
|
-
|
|
90
|
+
const getMultipleWithCache = ({ getFromCache, multiGetterFromCache, setInCache, getter, multiGetter, idField = 'id', }) => async (queries) => {
|
|
87
91
|
const queriesMap = new Map(queries.filter(Boolean).map(query => [getIdField(query, idField), query]));
|
|
88
92
|
const resultMap = new Map();
|
|
89
93
|
const valuesToPullFromCache = [...queriesMap.values()];
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
else {
|
|
95
|
-
valuesFromCache = await Promise.all(valuesToPullFromCache.map(query => getFromCache(query)));
|
|
96
|
-
}
|
|
97
|
-
valuesFromCache.filter(Boolean).map((value) => {
|
|
94
|
+
const valuesFromCache = await (multiGetterFromCache?.(valuesToPullFromCache) || // Use multiGetterFromCache if it's provided
|
|
95
|
+
Promise.all(valuesToPullFromCache.map(query => getFromCache(query))) // Otherwise, iterate over the queries with getFromCache
|
|
96
|
+
);
|
|
97
|
+
valuesFromCache.filter(Boolean).forEach((value) => {
|
|
98
98
|
queriesMap.delete(getIdField(value, idField));
|
|
99
99
|
resultMap.set(getIdField(value, idField), value);
|
|
100
|
-
return value;
|
|
101
100
|
});
|
|
102
101
|
let valuesFromGetter = [];
|
|
103
102
|
if (queriesMap.size > 0) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
else {
|
|
108
|
-
valuesFromGetter = await Promise.all([...queriesMap.values()].map(id => getter(id)));
|
|
109
|
-
}
|
|
103
|
+
const valuesFromGetter = await (multiGetter?.([...queriesMap.values()]) || // Use multiGetter if it's provided
|
|
104
|
+
Promise.all([...queriesMap.values()].map(id => getter(id))) // Otherwise, iterate over the queries with getter
|
|
105
|
+
);
|
|
110
106
|
valuesFromGetter.forEach((value) => {
|
|
111
107
|
setInCache(value[idField], value);
|
|
112
108
|
resultMap.set(getIdField(value, idField), value);
|
|
@@ -119,3 +115,4 @@ exports.getMultipleWithCache = ({ getFromCache = undefined, multiGetterFromCache
|
|
|
119
115
|
return resultMap.get(getIdField(query, idField));
|
|
120
116
|
});
|
|
121
117
|
};
|
|
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: MutexInterface, funcToRun: () => PromiseLike<void>) => Promise<void>;
|
|
3
3
|
export declare const getMutex: () => MutexInterface;
|
package/lib/locking.js
CHANGED
|
@@ -4,8 +4,7 @@ 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
|
-
|
|
8
|
-
exports.wrapWithMutex = async (mutex, funcToRun) => {
|
|
7
|
+
const wrapWithMutex = async (mutex, funcToRun) => {
|
|
9
8
|
const release = await mutex.acquire();
|
|
10
9
|
try {
|
|
11
10
|
await funcToRun();
|
|
@@ -14,4 +13,6 @@ exports.wrapWithMutex = async (mutex, funcToRun) => {
|
|
|
14
13
|
release();
|
|
15
14
|
}
|
|
16
15
|
};
|
|
17
|
-
exports.
|
|
16
|
+
exports.wrapWithMutex = wrapWithMutex;
|
|
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;
|
package/lib/logger.d.ts
ADDED
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 = logger_1.default();
|
|
7
|
+
const logger = (0, 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 = 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
|
+
const ORMCache = (options) => {
|
|
26
26
|
const { models } = options;
|
|
27
27
|
logger_1.default.info('Starting ORM Cache', { options });
|
|
28
28
|
const adapter = ORMInstanceFactory(options);
|
|
@@ -36,3 +36,4 @@ exports.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 = util_1.promisify(addDependenciesMulti.exec).bind(addDependenciesMulti);
|
|
86
|
+
const addDependenciesMultiAsync = (0, 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 = util_1.promisify(removeMulti.exec).bind(removeMulti);
|
|
116
|
+
const removeMultiAsync = (0, 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 = util_1.promisify(removeMulti.exec).bind(removeMulti);
|
|
125
|
+
const removeMultiAsync = (0, 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 = util_1.promisify(redis_lock_1.default(this.client, options.lockRetries ?? 10));
|
|
30
|
+
this.locker = (0, util_1.promisify)((0, 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,6 +62,9 @@ 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
|
+
}
|
|
65
68
|
// Try to get the value from redis.
|
|
66
69
|
values = await this.client.mgetAsync(keysWithPrefix);
|
|
67
70
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/matmon",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.7",
|
|
4
4
|
"description": "manage cache",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -27,8 +27,6 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/Autofleet/matmon",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@autofleet/logger": "^4.0.2",
|
|
31
|
-
"@types/node": "^14.14.20",
|
|
32
30
|
"async-mutex": "^0.2.6",
|
|
33
31
|
"bluebird": "^3.7.2",
|
|
34
32
|
"dotenv": "^8.2.0",
|
|
@@ -39,7 +37,13 @@
|
|
|
39
37
|
"sequelize-typescript": "^2.1.0"
|
|
40
38
|
},
|
|
41
39
|
"devDependencies": {
|
|
42
|
-
"
|
|
40
|
+
"@autofleet/logger": "^4.0.3",
|
|
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"
|
|
43
47
|
},
|
|
44
48
|
"files": [
|
|
45
49
|
"lib/**/*"
|
|
File without changes
|
package/lib/redis/index.test.js
DELETED
|
File without changes
|