@autofleet/matmon 2.0.7 → 2.0.8

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 CHANGED
@@ -18,15 +18,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
18
18
  __setModuleDefault(result, mod);
19
19
  return result;
20
20
  };
21
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
- return new (P || (P = Promise))(function (resolve, reject) {
24
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
- step((generator = generator.apply(thisArg, _arguments || [])).next());
28
- });
29
- };
30
21
  var __importDefault = (this && this.__importDefault) || function (mod) {
31
22
  return (mod && mod.__esModule) ? mod : { "default": mod };
32
23
  };
@@ -58,31 +49,31 @@ exports.getNewLRU = (lifeTimeInSec, size) => new lru_cache_1.default(getOptions(
58
49
  lifeTimeInSec,
59
50
  size,
60
51
  }));
61
- exports.getWithCacheSupport = ({ cacheKey, cacheGet, cacheSet, fetching, skipCache, }) => __awaiter(void 0, void 0, void 0, function* () {
52
+ exports.getWithCacheSupport = async ({ cacheKey, cacheGet, cacheSet, fetching, skipCache, }) => {
62
53
  if (skipCache || process.env.NODE_ENV === 'test') {
63
- const res = yield fetching();
64
- yield cacheSet(res);
54
+ const res = await fetching();
55
+ await cacheSet(res);
65
56
  return res;
66
57
  }
67
58
  let valueToReturn = null;
68
59
  try {
69
- yield locking.wrapWithMutex(getMutexByCacheKey(cacheKey), () => __awaiter(void 0, void 0, void 0, function* () {
70
- valueToReturn = yield cacheGet();
60
+ await locking.wrapWithMutex(getMutexByCacheKey(cacheKey), async () => {
61
+ valueToReturn = await cacheGet();
71
62
  if (!valueToReturn) {
72
- valueToReturn = yield fetching();
73
- yield cacheSet(valueToReturn);
63
+ valueToReturn = await fetching();
64
+ await cacheSet(valueToReturn);
74
65
  }
75
66
  else {
76
67
  // logger.info('get value from cache');
77
68
  }
78
69
  deleteMutexByCacheKey(cacheKey);
79
- }));
70
+ });
80
71
  // retry without locking if failed
81
72
  }
82
73
  catch (e) {
83
- valueToReturn = yield fetching();
84
- yield cacheSet(valueToReturn);
74
+ valueToReturn = await fetching();
75
+ await cacheSet(valueToReturn);
85
76
  deleteMutexByCacheKey(cacheKey);
86
77
  }
87
78
  return valueToReturn;
88
- });
79
+ };
package/lib/locking.js CHANGED
@@ -1,26 +1,17 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.getMutex = exports.wrapWithMutex = void 0;
13
4
  const async_mutex_1 = require("async-mutex");
14
5
  const CACHE_LOCK_TIMEOUT_MILIS = 3000;
15
6
  const LOCK_TIMEOUT_MESSAGE = 'mutex - locking timeout';
16
7
  // eslint-disable-next-line import/prefer-default-export
17
- exports.wrapWithMutex = (mutex, funcToRun) => __awaiter(void 0, void 0, void 0, function* () {
18
- const release = yield mutex.acquire();
8
+ exports.wrapWithMutex = async (mutex, funcToRun) => {
9
+ const release = await mutex.acquire();
19
10
  try {
20
- yield funcToRun();
11
+ await funcToRun();
21
12
  }
22
13
  finally {
23
14
  release();
24
15
  }
25
- });
16
+ };
26
17
  exports.getMutex = () => async_mutex_1.withTimeout(new async_mutex_1.Mutex(), CACHE_LOCK_TIMEOUT_MILIS, new Error(LOCK_TIMEOUT_MESSAGE));
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -87,7 +78,7 @@ class SequelizeAdapter {
87
78
  }
88
79
  }
89
80
  injectGetWithCacheFunction(cache, modelOptions) {
90
- const addDependencies = (instance) => __awaiter(this, void 0, void 0, function* () {
81
+ const addDependencies = async (instance) => {
91
82
  const dependencyKeys = getInstanceDependencyKeys(modelOptions, instance);
92
83
  const instanceKey = generateInstanceKey(modelOptions.name, instance.id);
93
84
  this.debug('Adding dependencies', { instanceKey, dependencyKeys });
@@ -95,16 +86,16 @@ class SequelizeAdapter {
95
86
  const addDependenciesMultiAsync = util_1.promisify(addDependenciesMulti.exec).bind(addDependenciesMulti);
96
87
  dependencyKeys.reduce((multi, key) => multi.sadd(key, instanceKey), addDependenciesMulti);
97
88
  return addDependenciesMultiAsync();
98
- });
89
+ };
99
90
  const model = this.getModel(modelOptions.name);
100
- model.findByPkCached = (id, scopes, options) => __awaiter(this, void 0, void 0, function* () {
91
+ model.findByPkCached = async (id, scopes, options) => {
101
92
  const cacheKey = generateInstanceKey(modelOptions.name, id);
102
- let value = JSON.parse(yield cache.getClient().getAsync(cacheKey));
93
+ let value = JSON.parse(await cache.getClient().getAsync(cacheKey));
103
94
  if (!value) {
104
95
  this.debug('Value not found in cache, looking in db', { id, cacheKey });
105
- value = yield model.scope(scopes).findByPk(id, options);
96
+ value = await model.scope(scopes).findByPk(id, options);
106
97
  this.debug('Value from DB', { value: value || 'not found', cacheKey });
107
- yield Promise.all([
98
+ await Promise.all([
108
99
  cache.getClient().setAsync(cacheKey, JSON.stringify(value)),
109
100
  value && addDependencies(value),
110
101
  ]);
@@ -114,10 +105,10 @@ class SequelizeAdapter {
114
105
  this.debug('Found cached value', { value, id, cacheKey });
115
106
  }
116
107
  return value;
117
- });
108
+ };
118
109
  }
119
110
  addInvalidationHooks(cache, modelOptions) {
120
- const invalidateModelInstance = (instance) => __awaiter(this, void 0, void 0, function* () {
111
+ const invalidateModelInstance = async (instance) => {
121
112
  const dependencyKeys = getInstanceDependencyKeys(modelOptions, instance);
122
113
  const instanceKey = generateInstanceKey(modelOptions.name, instance.id);
123
114
  this.debug('Removing dependencies', { instance, instanceKey, dependencyKeys });
@@ -126,14 +117,14 @@ class SequelizeAdapter {
126
117
  dependencyKeys.map(key => removeMulti.srem(key, instanceKey));
127
118
  removeMulti.del(instanceKey);
128
119
  return removeMultiAsync();
129
- });
130
- const invalidateModelInstanceByAssociation = (association, associationId) => __awaiter(this, void 0, void 0, function* () {
131
- const dependentInstancesKeys = yield cache.getClient().smembersAsync(generateDependencyKey(modelOptions.name, association, associationId));
120
+ };
121
+ const invalidateModelInstanceByAssociation = async (association, associationId) => {
122
+ const dependentInstancesKeys = await cache.getClient().smembersAsync(generateDependencyKey(modelOptions.name, association, associationId));
132
123
  this.debug('Invalidating dependent instances', { dependentInstancesKeys });
133
124
  const removeMulti = cache.getClient().multi();
134
125
  const removeMultiAsync = util_1.promisify(removeMulti.exec).bind(removeMulti);
135
- const dependenciesToRemove = yield Promise.all(dependentInstancesKeys.map((instanceKey) => __awaiter(this, void 0, void 0, function* () {
136
- const instance = JSON.parse(yield cache.getClient().getAsync(instanceKey));
126
+ const dependenciesToRemove = await Promise.all(dependentInstancesKeys.map(async (instanceKey) => {
127
+ const instance = JSON.parse(await cache.getClient().getAsync(instanceKey));
137
128
  if (!instance) {
138
129
  return [];
139
130
  }
@@ -141,10 +132,10 @@ class SequelizeAdapter {
141
132
  dependencyKeys.reduce((multi, key) => multi.srem(key, instanceKey), removeMulti);
142
133
  removeMulti.del(instanceKey);
143
134
  return dependencyKeys;
144
- })));
135
+ }));
145
136
  this.debug('Removing dependencies', { dependentInstancesKeys, dependenciesToRemove });
146
137
  return removeMultiAsync();
147
- });
138
+ };
148
139
  const model = this.getModel(modelOptions.name);
149
140
  INVALIDATION_HOOKS.map(hook => model.addHook(hook, (instance, options) => handleTransactionHook(instance, options, instance => invalidateModelInstance(instance))));
150
141
  BULK_HOOKS.map(hook => model.addHook(hook, options => options.individualHook = true));
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -32,85 +23,76 @@ if (!SERVICE_NAME) {
32
23
  }
33
24
  class RedisCache {
34
25
  constructor(options) {
35
- var _a, _b, _c, _d;
36
26
  this.client = redis_1.default.createClient({
37
27
  host: options.host || HOST,
38
28
  port: options.port || PORT,
39
29
  });
40
- this.locker = util_1.promisify(redis_lock_1.default(this.client, (_a = options.lockRetries) !== null && _a !== void 0 ? _a : 10));
41
- this.lockTimeout = (_b = options.lockTimeout) !== null && _b !== void 0 ? _b : DEFAULT_LOCK_TIMEOUT;
42
- this.lockDuration = (_c = options.lockDuration) !== null && _c !== void 0 ? _c : DEFAULT_LOCK_DURATION;
43
- this.baseTTL = (_d = options.ttl) !== null && _d !== void 0 ? _d : DEFAULT_BASE_TTL;
30
+ this.locker = util_1.promisify(redis_lock_1.default(this.client, options.lockRetries ?? 10));
31
+ this.lockTimeout = options.lockTimeout ?? DEFAULT_LOCK_TIMEOUT;
32
+ this.lockDuration = options.lockDuration ?? DEFAULT_LOCK_DURATION;
33
+ this.baseTTL = options.ttl ?? DEFAULT_BASE_TTL;
44
34
  this.locks = {};
45
35
  this.useLock = !!options.useLock;
46
36
  }
47
- get(key) {
48
- return __awaiter(this, void 0, void 0, function* () {
49
- const keyWithPrefix = KEY_PREFIX + key;
50
- let value;
37
+ async get(key) {
38
+ const keyWithPrefix = KEY_PREFIX + key;
39
+ let value;
40
+ try {
41
+ // Try to get the value from redis.
42
+ value = await this.client.getAsync(keyWithPrefix);
43
+ }
44
+ catch (err) {
45
+ throw new errors_1.RedisCacheError('Failed to get a value', err);
46
+ }
47
+ if (this.useLock) {
48
+ let lock;
51
49
  try {
52
- // Try to get the value from redis.
53
- value = yield this.client.getAsync(keyWithPrefix);
50
+ // Try to lock the key.
51
+ lock = await this.lock(keyWithPrefix);
54
52
  }
55
53
  catch (err) {
56
- throw new errors_1.RedisCacheError('Failed to get a value', err);
57
- }
58
- if (this.useLock) {
59
- let lock;
60
- try {
61
- // Try to lock the key.
62
- lock = yield this.lock(keyWithPrefix);
63
- }
64
- catch (err) {
65
- throw new errors_1.RedisLockError('Failed to lock key', err, keyWithPrefix);
66
- }
67
- // If the lock did not fail, add it to a locks dictionary.
68
- this.locks[keyWithPrefix] = lock;
54
+ throw new errors_1.RedisLockError('Failed to lock key', err, keyWithPrefix);
69
55
  }
70
- return JSON.parse(value);
71
- });
56
+ // If the lock did not fail, add it to a locks dictionary.
57
+ this.locks[keyWithPrefix] = lock;
58
+ }
59
+ return JSON.parse(value);
72
60
  }
73
- set(key, value) {
74
- return __awaiter(this, void 0, void 0, function* () {
75
- const keyWithPrefix = KEY_PREFIX + key;
76
- const ttl = parseInt(String(this.baseTTL * (Math.random() + 1)), 10);
77
- try {
78
- yield this.client.setAsync(keyWithPrefix, JSON.stringify(value), 'EX', ttl);
79
- if (this.locks[keyWithPrefix]) {
80
- yield this.locks[keyWithPrefix]();
81
- delete this.locks[keyWithPrefix];
82
- }
61
+ async set(key, value) {
62
+ const keyWithPrefix = KEY_PREFIX + key;
63
+ const ttl = parseInt(String(this.baseTTL * (Math.random() + 1)), 10);
64
+ try {
65
+ await this.client.setAsync(keyWithPrefix, JSON.stringify(value), 'EX', ttl);
66
+ if (this.locks[keyWithPrefix]) {
67
+ await this.locks[keyWithPrefix]();
68
+ delete this.locks[keyWithPrefix];
83
69
  }
84
- catch (err) {
85
- throw new errors_1.RedisCacheError('Failed to set a key-value pair', err);
86
- }
87
- });
70
+ }
71
+ catch (err) {
72
+ throw new errors_1.RedisCacheError('Failed to set a key-value pair', err);
73
+ }
88
74
  }
89
- remove(key) {
90
- return __awaiter(this, void 0, void 0, function* () {
91
- const keyWithPrefix = KEY_PREFIX + key;
92
- try {
93
- if (this.locks[keyWithPrefix]) {
94
- yield this.locks[keyWithPrefix]();
95
- delete this.locks[keyWithPrefix];
96
- }
97
- yield this.client.delAsync(keyWithPrefix);
75
+ async remove(key) {
76
+ const keyWithPrefix = KEY_PREFIX + key;
77
+ try {
78
+ if (this.locks[keyWithPrefix]) {
79
+ await this.locks[keyWithPrefix]();
80
+ delete this.locks[keyWithPrefix];
98
81
  }
99
- catch (err) {
100
- throw new errors_1.RedisCacheError(`Failed to delete key ${keyWithPrefix}`, err);
101
- }
102
- });
82
+ await this.client.delAsync(keyWithPrefix);
83
+ }
84
+ catch (err) {
85
+ throw new errors_1.RedisCacheError(`Failed to delete key ${keyWithPrefix}`, err);
86
+ }
103
87
  }
104
- removeMultiple(keys) {
105
- return __awaiter(this, void 0, void 0, function* () {
106
- const keysWithPrefix = keys.map(key => KEY_PREFIX + key);
107
- try {
108
- yield this.client.delAsync(keysWithPrefix);
109
- }
110
- catch (err) {
111
- throw new errors_1.RedisCacheError(`Failed to delete multiple keys ${keysWithPrefix.join('|')}`, err);
112
- }
113
- });
88
+ async removeMultiple(keys) {
89
+ const keysWithPrefix = keys.map(key => KEY_PREFIX + key);
90
+ try {
91
+ await this.client.delAsync(keysWithPrefix);
92
+ }
93
+ catch (err) {
94
+ throw new errors_1.RedisCacheError(`Failed to delete multiple keys ${keysWithPrefix.join('|')}`, err);
95
+ }
114
96
  }
115
97
  getClient() {
116
98
  return this.client;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/matmon",
3
- "version": "2.0.7",
3
+ "version": "2.0.8",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",