@autofleet/matmon 1.0.4 → 1.0.5
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/redis/index.js +19 -11
- package/package.json +1 -1
package/lib/redis/index.js
CHANGED
|
@@ -22,9 +22,14 @@ bluebird_1.default.promisifyAll(redis_1.default.Multi.prototype);
|
|
|
22
22
|
const { env } = process;
|
|
23
23
|
const HOST = env.REDIS_HOST_NAME || '127.0.0.1';
|
|
24
24
|
const PORT = env.REDIS_HOST_PORT || 6379;
|
|
25
|
+
const SERVICE_NAME = env.AF_SERVICE_NAME;
|
|
25
26
|
const DEFAULT_LOCK_TIMEOUT = 5000;
|
|
26
27
|
const DEFAULT_LOCK_DURATION = 1000;
|
|
27
28
|
const DEFAULT_BASE_TTL = 3600;
|
|
29
|
+
const KEY_PREFIX = SERVICE_NAME + ':';
|
|
30
|
+
if (!SERVICE_NAME) {
|
|
31
|
+
throw new Error('SERVICE_NAME cannot be null, please check your env.AF_SERVICE_NAME');
|
|
32
|
+
}
|
|
28
33
|
class RedisCache {
|
|
29
34
|
constructor(options) {
|
|
30
35
|
var _a, _b, _c, _d;
|
|
@@ -41,22 +46,23 @@ class RedisCache {
|
|
|
41
46
|
}
|
|
42
47
|
get(key) {
|
|
43
48
|
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
const keyWithPrefix = KEY_PREFIX + key;
|
|
44
50
|
if (this.useLock) {
|
|
45
51
|
let lock;
|
|
46
52
|
try {
|
|
47
53
|
// Try to lock the key.
|
|
48
|
-
lock = yield this.lock(
|
|
54
|
+
lock = yield this.lock(keyWithPrefix);
|
|
49
55
|
}
|
|
50
56
|
catch (err) {
|
|
51
|
-
throw new errors_1.RedisLockError('Failed to lock key', err,
|
|
57
|
+
throw new errors_1.RedisLockError('Failed to lock key', err, keyWithPrefix);
|
|
52
58
|
}
|
|
53
59
|
// If the lock did not fail, add it to a locks dictionary.
|
|
54
|
-
this.locks[
|
|
60
|
+
this.locks[keyWithPrefix] = lock;
|
|
55
61
|
}
|
|
56
62
|
let value;
|
|
57
63
|
try {
|
|
58
64
|
// Try to get the value from redis.
|
|
59
|
-
value = yield this.client.getAsync(
|
|
65
|
+
value = yield this.client.getAsync(keyWithPrefix);
|
|
60
66
|
}
|
|
61
67
|
catch (err) {
|
|
62
68
|
throw new errors_1.RedisCacheError('Failed to get a value', err);
|
|
@@ -66,11 +72,12 @@ class RedisCache {
|
|
|
66
72
|
}
|
|
67
73
|
set(key, value) {
|
|
68
74
|
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
const keyWithPrefix = KEY_PREFIX + key;
|
|
69
76
|
const ttl = parseInt(String(this.baseTTL * (Math.random() + 1)), 10);
|
|
70
77
|
try {
|
|
71
|
-
yield this.client.setAsync(
|
|
72
|
-
if (this.locks[
|
|
73
|
-
yield this.locks[
|
|
78
|
+
yield this.client.setAsync(keyWithPrefix, JSON.stringify(value), 'EX', ttl);
|
|
79
|
+
if (this.locks[keyWithPrefix]) {
|
|
80
|
+
yield this.locks[keyWithPrefix]();
|
|
74
81
|
}
|
|
75
82
|
}
|
|
76
83
|
catch (err) {
|
|
@@ -80,14 +87,15 @@ class RedisCache {
|
|
|
80
87
|
}
|
|
81
88
|
remove(key) {
|
|
82
89
|
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
+
const keyWithPrefix = KEY_PREFIX + key;
|
|
83
91
|
try {
|
|
84
|
-
if (this.locks[
|
|
85
|
-
yield this.locks[
|
|
92
|
+
if (this.locks[keyWithPrefix]) {
|
|
93
|
+
yield this.locks[keyWithPrefix]();
|
|
86
94
|
}
|
|
87
|
-
yield this.client.delAsync(
|
|
95
|
+
yield this.client.delAsync(keyWithPrefix);
|
|
88
96
|
}
|
|
89
97
|
catch (err) {
|
|
90
|
-
throw new errors_1.RedisCacheError(`Failed to invalidate key ${
|
|
98
|
+
throw new errors_1.RedisCacheError(`Failed to invalidate key ${keyWithPrefix}`, err);
|
|
91
99
|
}
|
|
92
100
|
});
|
|
93
101
|
}
|