@autofleet/matmon 2.0.4-beta-2 → 2.0.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/lib/cache.js +1 -1
- package/lib/orm-cache/sequelize-adapter.js +34 -41
- package/package.json +1 -2
package/lib/cache.js
CHANGED
|
@@ -37,7 +37,7 @@ const locking = __importStar(require("./locking"));
|
|
|
37
37
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
38
38
|
dotenv_1.default.config();
|
|
39
39
|
const DEFAULT_CACHE_SIZE = 300;
|
|
40
|
-
const MAX_SIZE =
|
|
40
|
+
const MAX_SIZE = 100000;
|
|
41
41
|
const MUTEX_MAP = {};
|
|
42
42
|
const getOptions = ({ lifeTimeInSec, size = DEFAULT_CACHE_SIZE, }) => ({
|
|
43
43
|
max: Math.min(size, MAX_SIZE),
|
|
@@ -12,7 +12,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const newrelic_1 = __importDefault(require("newrelic"));
|
|
16
15
|
const logger_1 = __importDefault(require("../logger"));
|
|
17
16
|
const util_1 = require("util");
|
|
18
17
|
const { AF_SERVICE_NAME } = process.env;
|
|
@@ -21,7 +20,7 @@ const INVALIDATION_HOOKS = ['afterSave', 'afterUpdate', 'afterDestroy'];
|
|
|
21
20
|
const BULK_HOOKS = ['beforeBulkUpdate', 'beforeBulkDestroy'];
|
|
22
21
|
const generateInstanceKey = (name, id) => `${AF_SERVICE_NAME}:${ORM_CACHE_PREFIX}:${name}_${id}`;
|
|
23
22
|
const generateDependencyKey = (modelName, associationName, associationId) => `${AF_SERVICE_NAME}:${ORM_CACHE_PREFIX}:${modelName}_${associationName}_${associationId}_DEPENDENCIES`;
|
|
24
|
-
const getInstanceDependencyKeys = (modelOptions, instance) =>
|
|
23
|
+
const getInstanceDependencyKeys = (modelOptions, instance) => {
|
|
25
24
|
const keys = modelOptions.associations
|
|
26
25
|
.filter((associationOptions) => instance[associationOptions.alias])
|
|
27
26
|
.map((associationOptions) => {
|
|
@@ -42,17 +41,11 @@ const getInstanceDependencyKeys = (modelOptions, instance) => newrelic_1.default
|
|
|
42
41
|
];
|
|
43
42
|
});
|
|
44
43
|
return keys.reduce((flattenArray, array) => flattenArray.concat(array), []);
|
|
45
|
-
}
|
|
44
|
+
};
|
|
46
45
|
const handleTransactionHook = (instance, options, func) => {
|
|
47
46
|
const { transaction } = options;
|
|
48
47
|
if (transaction) {
|
|
49
|
-
|
|
50
|
-
transaction[ORM_CACHE_PREFIX] = new Set();
|
|
51
|
-
}
|
|
52
|
-
if (!transaction[ORM_CACHE_PREFIX].has(instance.id)) {
|
|
53
|
-
transaction.afterCommit(() => func(instance));
|
|
54
|
-
transaction[ORM_CACHE_PREFIX].add(instance.id);
|
|
55
|
-
}
|
|
48
|
+
transaction.afterCommit(() => func(instance));
|
|
56
49
|
}
|
|
57
50
|
else {
|
|
58
51
|
func(instance);
|
|
@@ -67,28 +60,26 @@ class SequelizeAdapter {
|
|
|
67
60
|
return this.ormInstance.models[modelName];
|
|
68
61
|
}
|
|
69
62
|
getModelDependencies(modelName) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
63
|
+
const { associations } = this.ormInstance.models[modelName];
|
|
64
|
+
return [
|
|
65
|
+
...Object.keys(associations).map(association => {
|
|
66
|
+
const sequelizeAssociation = associations[association];
|
|
67
|
+
const associationOptions = {
|
|
68
|
+
alias: sequelizeAssociation.as,
|
|
69
|
+
name: sequelizeAssociation.target.name,
|
|
70
|
+
accessKey: sequelizeAssociation.target.primaryKeyAttribute,
|
|
71
|
+
};
|
|
72
|
+
if (sequelizeAssociation.through) {
|
|
73
|
+
const relationModel = sequelizeAssociation.through.model;
|
|
74
|
+
associationOptions.innerAssociation = {
|
|
75
|
+
alias: relationModel.name,
|
|
76
|
+
name: relationModel.name,
|
|
77
|
+
accessKey: relationModel.primaryKeyAttribute,
|
|
79
78
|
};
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
name: relationModel.name,
|
|
85
|
-
accessKey: relationModel.primaryKeyAttribute,
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
return associationOptions;
|
|
89
|
-
}),
|
|
90
|
-
];
|
|
91
|
-
});
|
|
79
|
+
}
|
|
80
|
+
return associationOptions;
|
|
81
|
+
}),
|
|
82
|
+
];
|
|
92
83
|
}
|
|
93
84
|
debug(message, payload) {
|
|
94
85
|
if (this.debugMode) {
|
|
@@ -96,7 +87,7 @@ class SequelizeAdapter {
|
|
|
96
87
|
}
|
|
97
88
|
}
|
|
98
89
|
injectGetWithCacheFunction(cache, modelOptions) {
|
|
99
|
-
const addDependencies = (instance) =>
|
|
90
|
+
const addDependencies = (instance) => __awaiter(this, void 0, void 0, function* () {
|
|
100
91
|
const dependencyKeys = getInstanceDependencyKeys(modelOptions, instance);
|
|
101
92
|
const instanceKey = generateInstanceKey(modelOptions.name, instance.id);
|
|
102
93
|
this.debug('Adding dependencies', { instanceKey, dependencyKeys });
|
|
@@ -126,19 +117,19 @@ class SequelizeAdapter {
|
|
|
126
117
|
});
|
|
127
118
|
}
|
|
128
119
|
addInvalidationHooks(cache, modelOptions) {
|
|
129
|
-
const invalidateModelInstance = (
|
|
120
|
+
const invalidateModelInstance = (instance) => __awaiter(this, void 0, void 0, function* () {
|
|
130
121
|
const dependencyKeys = getInstanceDependencyKeys(modelOptions, instance);
|
|
131
122
|
const instanceKey = generateInstanceKey(modelOptions.name, instance.id);
|
|
132
|
-
this.debug(
|
|
123
|
+
this.debug('Removing dependencies', { instance, instanceKey, dependencyKeys });
|
|
133
124
|
const removeMulti = cache.getClient().multi();
|
|
134
125
|
const removeMultiAsync = util_1.promisify(removeMulti.exec).bind(removeMulti);
|
|
135
126
|
dependencyKeys.map(key => removeMulti.srem(key, instanceKey));
|
|
136
127
|
removeMulti.del(instanceKey);
|
|
137
128
|
return removeMultiAsync();
|
|
138
129
|
});
|
|
139
|
-
const invalidateModelInstanceByAssociation = (
|
|
130
|
+
const invalidateModelInstanceByAssociation = (association, associationId) => __awaiter(this, void 0, void 0, function* () {
|
|
140
131
|
const dependentInstancesKeys = yield cache.getClient().smembersAsync(generateDependencyKey(modelOptions.name, association, associationId));
|
|
141
|
-
this.debug(
|
|
132
|
+
this.debug('Invalidating dependent instances', { dependentInstancesKeys });
|
|
142
133
|
const removeMulti = cache.getClient().multi();
|
|
143
134
|
const removeMultiAsync = util_1.promisify(removeMulti.exec).bind(removeMulti);
|
|
144
135
|
const dependenciesToRemove = yield Promise.all(dependentInstancesKeys.map((instanceKey) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -151,20 +142,22 @@ class SequelizeAdapter {
|
|
|
151
142
|
removeMulti.del(instanceKey);
|
|
152
143
|
return dependencyKeys;
|
|
153
144
|
})));
|
|
154
|
-
this.debug(
|
|
145
|
+
this.debug('Removing dependencies', { dependentInstancesKeys, dependenciesToRemove });
|
|
155
146
|
return removeMultiAsync();
|
|
156
|
-
})
|
|
147
|
+
});
|
|
157
148
|
const model = this.getModel(modelOptions.name);
|
|
158
|
-
INVALIDATION_HOOKS.map(hook => model.addHook(hook, (instance, options) => handleTransactionHook(instance, options, instance => invalidateModelInstance(
|
|
149
|
+
INVALIDATION_HOOKS.map(hook => model.addHook(hook, (instance, options) => handleTransactionHook(instance, options, instance => invalidateModelInstance(instance))));
|
|
159
150
|
BULK_HOOKS.map(hook => model.addHook(hook, options => options.individualHook = true));
|
|
160
151
|
modelOptions.associations = this.getModelDependencies(modelOptions.name);
|
|
161
152
|
this.debug(`Adding Invalidations Hooks to ${modelOptions.name}'s associations`, { associations: modelOptions.associations });
|
|
162
153
|
modelOptions.associations.map((associationOptions) => {
|
|
163
154
|
const associationModel = this.getModel(associationOptions.name);
|
|
164
|
-
INVALIDATION_HOOKS.map(hook => associationModel.addHook(hook, (instance, options) => handleTransactionHook(instance, options, associationInstance => invalidateModelInstanceByAssociation(
|
|
155
|
+
INVALIDATION_HOOKS.map(hook => associationModel.addHook(hook, (instance, options) => handleTransactionHook(instance, options, associationInstance => invalidateModelInstanceByAssociation(associationOptions.name, associationInstance[associationOptions.accessKey]))));
|
|
156
|
+
BULK_HOOKS.map(hook => associationModel.addHook(hook, options => options.individualHook = true));
|
|
165
157
|
if (associationOptions.innerAssociation) {
|
|
166
158
|
const innerAssociationModel = this.getModel(associationOptions.innerAssociation.name);
|
|
167
|
-
INVALIDATION_HOOKS.map(hook => innerAssociationModel.addHook(hook, (instance, options) => handleTransactionHook(instance, options, innerAssociationInstance => invalidateModelInstanceByAssociation(
|
|
159
|
+
INVALIDATION_HOOKS.map(hook => innerAssociationModel.addHook(hook, (instance, options) => handleTransactionHook(instance, options, innerAssociationInstance => invalidateModelInstanceByAssociation(associationOptions.innerAssociation.name, innerAssociationInstance[associationOptions.innerAssociation.accessKey]))));
|
|
160
|
+
BULK_HOOKS.map(hook => innerAssociationModel.addHook(hook, options => options.individualHook = true));
|
|
168
161
|
}
|
|
169
162
|
});
|
|
170
163
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/matmon",
|
|
3
|
-
"version": "2.0.4
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"description": "manage cache",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -32,7 +32,6 @@
|
|
|
32
32
|
"bluebird": "^3.7.2",
|
|
33
33
|
"dotenv": "^8.2.0",
|
|
34
34
|
"lru-cache": "^6.0.0",
|
|
35
|
-
"newrelic": "^7.5.2",
|
|
36
35
|
"redis": "^3.0.2",
|
|
37
36
|
"redis-lock": "^0.1.4",
|
|
38
37
|
"sequelize": "^6.6.2",
|