descript-redis-cache 3.0.0 → 4.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +30 -12
- package/build/index.js +242 -0
- package/package.json +22 -9
- package/.editorconfig +0 -19
- package/.eslintignore +0 -3
- package/.eslintrc.js +0 -23
- package/index.js +0 -280
package/README.md
CHANGED
@@ -1,28 +1,46 @@
|
|
1
1
|
# descript-redis-cache
|
2
|
-
Plugin to use Redis as a cache
|
2
|
+
Plugin to use Redis as a cache in Descript
|
3
3
|
|
4
4
|
## Usage
|
5
5
|
|
6
6
|
```js
|
7
|
-
|
8
|
-
|
7
|
+
import de from 'descript';
|
8
|
+
import deRedisCache from 'descript-redis-cache';
|
9
9
|
|
10
|
-
const
|
11
|
-
|
10
|
+
const redisCache = new deRedisCache(options);
|
11
|
+
|
12
|
+
const myBlock = de.http({
|
13
|
+
block: { /* ... */ },
|
14
|
+
options: {
|
15
|
+
key: ({ params }) => '_some_cache_key_from_params_',
|
16
|
+
cache: deRedisCache,
|
17
|
+
}
|
12
18
|
});
|
13
19
|
```
|
14
20
|
|
15
|
-
##
|
21
|
+
## Options
|
16
22
|
|
17
|
-
```
|
18
|
-
{
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
+
```ts
|
24
|
+
import type { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis';
|
25
|
+
|
26
|
+
export interface Options {
|
27
|
+
// key TTL in seconds (default: 60 * 60 * 24)
|
28
|
+
defaultKeyTTL?: number;
|
29
|
+
// increment generation to invalidate all key across breaking changes releases (default: 1)
|
30
|
+
generation?: number;
|
31
|
+
// read timeout in milliseconds (default: 100)
|
32
|
+
readTimeout?: number;
|
33
|
+
redis: (
|
34
|
+
{ startupNodes: ClusterNode[], options?: ClusterOptions } |
|
35
|
+
{ options: RedisOptions }
|
36
|
+
);
|
23
37
|
}
|
24
38
|
```
|
25
39
|
|
26
40
|
## Logger
|
27
41
|
|
28
42
|
Optionally you can pass logger object in the constructor. It should implement standard `Console` methods.
|
43
|
+
|
44
|
+
```js
|
45
|
+
const redisCache = new deRedisCache(options, logger);
|
46
|
+
```
|
package/build/index.js
ADDED
@@ -0,0 +1,242 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
7
|
+
};
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
12
|
+
};
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
15
|
+
};
|
16
|
+
var _Cache_instances, _Cache_client, _Cache_logger, _Cache_options, _Cache_normalizeKey, _Cache_log;
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
18
|
+
exports.Cache = void 0;
|
19
|
+
const node_crypto_1 = require("node:crypto");
|
20
|
+
const descript_1 = __importDefault(require("descript"));
|
21
|
+
const ioredis_1 = require("ioredis");
|
22
|
+
class Cache {
|
23
|
+
constructor(options, logger) {
|
24
|
+
_Cache_instances.add(this);
|
25
|
+
_Cache_client.set(this, void 0);
|
26
|
+
_Cache_logger.set(this, void 0);
|
27
|
+
_Cache_options.set(this, void 0);
|
28
|
+
__classPrivateFieldSet(this, _Cache_options, {
|
29
|
+
defaultKeyTTL: 60 * 60 * 24,
|
30
|
+
generation: 1,
|
31
|
+
readTimeout: 100,
|
32
|
+
...options,
|
33
|
+
}, "f");
|
34
|
+
__classPrivateFieldSet(this, _Cache_logger, logger, "f");
|
35
|
+
if ('startupNodes' in __classPrivateFieldGet(this, _Cache_options, "f").redis) {
|
36
|
+
__classPrivateFieldSet(this, _Cache_client, new ioredis_1.Cluster(__classPrivateFieldGet(this, _Cache_options, "f").redis.startupNodes, __classPrivateFieldGet(this, _Cache_options, "f").redis.options), "f");
|
37
|
+
}
|
38
|
+
else {
|
39
|
+
__classPrivateFieldSet(this, _Cache_client, new ioredis_1.Redis(__classPrivateFieldGet(this, _Cache_options, "f").redis.options), "f");
|
40
|
+
}
|
41
|
+
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
42
|
+
type: "REDIS_CACHE_INITIALIZED" /* EVENT.REDIS_CACHE_INITIALIZED */,
|
43
|
+
options: { ...__classPrivateFieldGet(this, _Cache_options, "f") },
|
44
|
+
});
|
45
|
+
}
|
46
|
+
get({ key }) {
|
47
|
+
const normalizedKey = __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_normalizeKey).call(this, key);
|
48
|
+
return new Promise((resolve, reject) => {
|
49
|
+
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
50
|
+
type: "REDIS_CACHE_READ_START" /* EVENT.REDIS_CACHE_READ_START */,
|
51
|
+
key,
|
52
|
+
normalizedKey,
|
53
|
+
});
|
54
|
+
const networkTimerStart = process.hrtime();
|
55
|
+
const totalTimerStart = process.hrtime();
|
56
|
+
let isTimeout = false;
|
57
|
+
const timer = setTimeout(() => {
|
58
|
+
isTimeout = true;
|
59
|
+
const networkTimer = process.hrtime(networkTimerStart);
|
60
|
+
const totalTimer = process.hrtime(totalTimerStart);
|
61
|
+
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
62
|
+
type: "REDIS_CACHE_READ_TIMEOUT" /* EVENT.REDIS_CACHE_READ_TIMEOUT */,
|
63
|
+
key,
|
64
|
+
normalizedKey,
|
65
|
+
timers: {
|
66
|
+
network: networkTimer,
|
67
|
+
total: totalTimer,
|
68
|
+
},
|
69
|
+
});
|
70
|
+
reject(descript_1.default.error({
|
71
|
+
id: "REDIS_CACHE_READ_TIMEOUT" /* EVENT.REDIS_CACHE_READ_TIMEOUT */,
|
72
|
+
}));
|
73
|
+
}, __classPrivateFieldGet(this, _Cache_options, "f").readTimeout);
|
74
|
+
__classPrivateFieldGet(this, _Cache_client, "f").get(normalizedKey, (error, data) => {
|
75
|
+
if (isTimeout) {
|
76
|
+
return;
|
77
|
+
}
|
78
|
+
const networkTimer = process.hrtime(networkTimerStart);
|
79
|
+
clearTimeout(timer);
|
80
|
+
if (error) {
|
81
|
+
const totalTimer = process.hrtime(totalTimerStart);
|
82
|
+
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
83
|
+
type: "REDIS_CACHE_READ_ERROR" /* EVENT.REDIS_CACHE_READ_ERROR */,
|
84
|
+
error,
|
85
|
+
key,
|
86
|
+
normalizedKey,
|
87
|
+
timers: {
|
88
|
+
network: networkTimer,
|
89
|
+
total: totalTimer,
|
90
|
+
},
|
91
|
+
});
|
92
|
+
reject(descript_1.default.error({
|
93
|
+
id: "REDIS_CACHE_READ_ERROR" /* EVENT.REDIS_CACHE_READ_ERROR */,
|
94
|
+
}));
|
95
|
+
}
|
96
|
+
else if (!data) {
|
97
|
+
const totalTimer = process.hrtime(totalTimerStart);
|
98
|
+
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
99
|
+
type: "REDIS_CACHE_READ_KEY_NOT_FOUND" /* EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND */,
|
100
|
+
key,
|
101
|
+
normalizedKey,
|
102
|
+
timers: {
|
103
|
+
network: networkTimer,
|
104
|
+
total: totalTimer,
|
105
|
+
},
|
106
|
+
});
|
107
|
+
reject(descript_1.default.error({
|
108
|
+
id: "REDIS_CACHE_READ_KEY_NOT_FOUND" /* EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND */,
|
109
|
+
}));
|
110
|
+
}
|
111
|
+
else {
|
112
|
+
let parsedValue;
|
113
|
+
try {
|
114
|
+
parsedValue = JSON.parse(data);
|
115
|
+
}
|
116
|
+
catch (error) {
|
117
|
+
const totalTimer = process.hrtime(totalTimerStart);
|
118
|
+
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
119
|
+
type: "REDIS_CACHE_JSON_PARSING_FAILED" /* EVENT.REDIS_CACHE_JSON_PARSING_FAILED */,
|
120
|
+
data,
|
121
|
+
error,
|
122
|
+
key,
|
123
|
+
normalizedKey,
|
124
|
+
timers: {
|
125
|
+
network: networkTimer,
|
126
|
+
total: totalTimer,
|
127
|
+
},
|
128
|
+
});
|
129
|
+
reject(descript_1.default.error({
|
130
|
+
id: "REDIS_CACHE_JSON_PARSING_FAILED" /* EVENT.REDIS_CACHE_JSON_PARSING_FAILED */,
|
131
|
+
}));
|
132
|
+
return;
|
133
|
+
}
|
134
|
+
const totalTimer = process.hrtime(totalTimerStart);
|
135
|
+
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
136
|
+
type: "REDIS_CACHE_READ_DONE" /* EVENT.REDIS_CACHE_READ_DONE */,
|
137
|
+
data,
|
138
|
+
key,
|
139
|
+
normalizedKey,
|
140
|
+
timers: {
|
141
|
+
network: networkTimer,
|
142
|
+
total: totalTimer,
|
143
|
+
},
|
144
|
+
});
|
145
|
+
resolve(parsedValue);
|
146
|
+
}
|
147
|
+
});
|
148
|
+
});
|
149
|
+
}
|
150
|
+
set({ key, value, maxage = __classPrivateFieldGet(this, _Cache_options, "f").defaultKeyTTL }) {
|
151
|
+
if (typeof value === 'undefined') {
|
152
|
+
return Promise.resolve();
|
153
|
+
}
|
154
|
+
const totalTimerStart = process.hrtime();
|
155
|
+
const normalizedKey = __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_normalizeKey).call(this, key);
|
156
|
+
return new Promise((resolve, reject) => {
|
157
|
+
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
158
|
+
type: "REDIS_CACHE_WRITE_START" /* EVENT.REDIS_CACHE_WRITE_START */,
|
159
|
+
key,
|
160
|
+
normalizedKey,
|
161
|
+
});
|
162
|
+
let json;
|
163
|
+
try {
|
164
|
+
json = JSON.stringify(value);
|
165
|
+
}
|
166
|
+
catch (error) {
|
167
|
+
const totalTimer = process.hrtime(totalTimerStart);
|
168
|
+
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
169
|
+
type: "REDIS_CACHE_JSON_STRINGIFY_FAILED" /* EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED */,
|
170
|
+
data: value,
|
171
|
+
error,
|
172
|
+
key,
|
173
|
+
normalizedKey,
|
174
|
+
timers: {
|
175
|
+
total: totalTimer,
|
176
|
+
},
|
177
|
+
});
|
178
|
+
reject(descript_1.default.error({
|
179
|
+
id: "REDIS_CACHE_JSON_STRINGIFY_FAILED" /* EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED */,
|
180
|
+
}));
|
181
|
+
return;
|
182
|
+
}
|
183
|
+
const networkTimerStart = process.hrtime();
|
184
|
+
// maxage - seconds
|
185
|
+
__classPrivateFieldGet(this, _Cache_client, "f").set(normalizedKey, json, 'EX', maxage, (error, done) => {
|
186
|
+
const networkTimer = process.hrtime(networkTimerStart);
|
187
|
+
const totalTimer = process.hrtime(totalTimerStart);
|
188
|
+
if (error) {
|
189
|
+
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
190
|
+
type: "REDIS_CACHE_WRITE_ERROR" /* EVENT.REDIS_CACHE_WRITE_ERROR */,
|
191
|
+
error,
|
192
|
+
key,
|
193
|
+
normalizedKey,
|
194
|
+
timers: {
|
195
|
+
network: networkTimer,
|
196
|
+
total: totalTimer,
|
197
|
+
},
|
198
|
+
});
|
199
|
+
reject(descript_1.default.error({
|
200
|
+
id: "REDIS_CACHE_WRITE_ERROR" /* EVENT.REDIS_CACHE_WRITE_ERROR */,
|
201
|
+
}));
|
202
|
+
}
|
203
|
+
else if (!done) {
|
204
|
+
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
205
|
+
type: "REDIS_CACHE_WRITE_FAILED" /* EVENT.REDIS_CACHE_WRITE_FAILED */,
|
206
|
+
key,
|
207
|
+
normalizedKey,
|
208
|
+
timers: {
|
209
|
+
network: networkTimer,
|
210
|
+
total: totalTimer,
|
211
|
+
},
|
212
|
+
});
|
213
|
+
reject(descript_1.default.error({
|
214
|
+
id: "REDIS_CACHE_WRITE_FAILED" /* EVENT.REDIS_CACHE_WRITE_FAILED */,
|
215
|
+
}));
|
216
|
+
}
|
217
|
+
else {
|
218
|
+
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
219
|
+
type: "REDIS_CACHE_WRITE_DONE" /* EVENT.REDIS_CACHE_WRITE_DONE */,
|
220
|
+
data: json,
|
221
|
+
key,
|
222
|
+
normalizedKey,
|
223
|
+
timers: {
|
224
|
+
network: networkTimer,
|
225
|
+
total: totalTimer,
|
226
|
+
},
|
227
|
+
});
|
228
|
+
resolve();
|
229
|
+
}
|
230
|
+
});
|
231
|
+
});
|
232
|
+
}
|
233
|
+
}
|
234
|
+
exports.Cache = Cache;
|
235
|
+
_Cache_client = new WeakMap(), _Cache_logger = new WeakMap(), _Cache_options = new WeakMap(), _Cache_instances = new WeakSet(), _Cache_normalizeKey = function _Cache_normalizeKey(key) {
|
236
|
+
const value = `g${__classPrivateFieldGet(this, _Cache_options, "f").generation}:${key}`;
|
237
|
+
return (0, node_crypto_1.hash)('sha512', value);
|
238
|
+
}, _Cache_log = function _Cache_log(event) {
|
239
|
+
if (__classPrivateFieldGet(this, _Cache_logger, "f")) {
|
240
|
+
__classPrivateFieldGet(this, _Cache_logger, "f").log(event);
|
241
|
+
}
|
242
|
+
};
|
package/package.json
CHANGED
@@ -1,14 +1,17 @@
|
|
1
1
|
{
|
2
2
|
"name": "descript-redis-cache",
|
3
|
-
"version": "
|
3
|
+
"version": "4.0.1",
|
4
4
|
"description": "plugin for descript to use redis as cache",
|
5
|
-
"main": "index.js",
|
5
|
+
"main": "./build/index.js",
|
6
6
|
"scripts": {
|
7
|
-
"
|
7
|
+
"eslint": "npx eslint .",
|
8
|
+
"test": "npx vitest",
|
9
|
+
"ts-compile": "npx tsc --noEmit",
|
10
|
+
"prepack": "rm -rf build && npx tsc -p ./tsconfig-build.json"
|
8
11
|
},
|
9
12
|
"repository": {
|
10
13
|
"type": "git",
|
11
|
-
"url": ""
|
14
|
+
"url": "https://github.com/descript-org/descript-redis-cache"
|
12
15
|
},
|
13
16
|
"keywords": [
|
14
17
|
"descript",
|
@@ -21,16 +24,26 @@
|
|
21
24
|
},
|
22
25
|
"homepage": "",
|
23
26
|
"engines": {
|
24
|
-
"node": ">=
|
27
|
+
"node": ">= 20.12.0"
|
25
28
|
},
|
29
|
+
"files": [
|
30
|
+
"build"
|
31
|
+
],
|
26
32
|
"dependencies": {
|
27
|
-
"
|
28
|
-
"ioredis": "^4.28.2"
|
33
|
+
"ioredis": "^5.4.1"
|
29
34
|
},
|
30
35
|
"peerDependencies": {
|
31
|
-
"descript": ">=
|
36
|
+
"descript": ">=4"
|
32
37
|
},
|
33
38
|
"devDependencies": {
|
34
|
-
"eslint": "^
|
39
|
+
"@eslint/js": "^9.14.0",
|
40
|
+
"@stylistic/eslint-plugin-ts": "^2.11.0",
|
41
|
+
"@types/eslint__js": "^8.42.3",
|
42
|
+
"@types/node": "^22.9.0",
|
43
|
+
"descript": "^4.0.5",
|
44
|
+
"eslint": "^9.15.0",
|
45
|
+
"typescript": "^5.6.3",
|
46
|
+
"typescript-eslint": "^8.15.0",
|
47
|
+
"vitest": "^2.1.5"
|
35
48
|
}
|
36
49
|
}
|
package/.editorconfig
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
# top-most EditorConfig file
|
2
|
-
root = true
|
3
|
-
|
4
|
-
# Unix-style newlines with a newline ending every file
|
5
|
-
[*]
|
6
|
-
charset = utf-8
|
7
|
-
end_of_line = lf
|
8
|
-
insert_final_newline = true
|
9
|
-
trim_trailing_whitespace = true
|
10
|
-
indent_style = space
|
11
|
-
indent_size = 4
|
12
|
-
|
13
|
-
# Data files
|
14
|
-
[*.{json, yml}]
|
15
|
-
indent_size = 2
|
16
|
-
|
17
|
-
# Makefiles
|
18
|
-
[*{M,m}akefile]
|
19
|
-
indent_style = tab
|
package/.eslintignore
DELETED
package/.eslintrc.js
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
module.exports = {
|
2
|
-
'env': {
|
3
|
-
'es6': true,
|
4
|
-
'node': true,
|
5
|
-
},
|
6
|
-
'extends': [
|
7
|
-
'eslint:recommended',
|
8
|
-
],
|
9
|
-
'rules': {
|
10
|
-
'array-bracket-spacing': [ 'error', 'always' ],
|
11
|
-
'comma-dangle': [ 'error', 'always-multiline' ],
|
12
|
-
'eol-last': 'error',
|
13
|
-
'indent': [ 'error', 4 ],
|
14
|
-
'linebreak-style': [ 'error', 'unix' ],
|
15
|
-
'no-empty': [ 'error', { 'allowEmptyCatch': true } ],
|
16
|
-
'no-multiple-empty-lines': [ 'error', { 'max': 1, 'maxBOF': 0 } ],
|
17
|
-
'no-var': [ 'error' ],
|
18
|
-
'object-curly-spacing': [ 'error', 'always' ],
|
19
|
-
'quotes': [ 'error', 'single' ],
|
20
|
-
'semi': [ 'error', 'always' ],
|
21
|
-
'space-infix-ops': 'error',
|
22
|
-
},
|
23
|
-
};
|
package/index.js
DELETED
@@ -1,280 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
const contimer = require('contimer');
|
4
|
-
const crypto = require('crypto');
|
5
|
-
const de = require('descript');
|
6
|
-
const Redis = require('ioredis');
|
7
|
-
|
8
|
-
class DescriptRedisCache {
|
9
|
-
|
10
|
-
constructor(options, logger) {
|
11
|
-
this._options = Object.assign({
|
12
|
-
defaultKeyTTL: 60 * 60 * 24, // key ttl in seconds
|
13
|
-
generation: 1, // increment generation to invalidate all key across breaking changes releases
|
14
|
-
readTimeout: 100, // read timeout in milliseconds,
|
15
|
-
redisOptions: {},
|
16
|
-
}, options);
|
17
|
-
|
18
|
-
this._logger = logger;
|
19
|
-
|
20
|
-
this._client = new Redis(options.redisOptions);
|
21
|
-
|
22
|
-
this._log({
|
23
|
-
type: DescriptRedisCache.EVENT.REDIS_CACHE_INITIALIZED,
|
24
|
-
options: options,
|
25
|
-
});
|
26
|
-
}
|
27
|
-
|
28
|
-
getClient() {
|
29
|
-
return this._client;
|
30
|
-
}
|
31
|
-
|
32
|
-
get({ key, context }) {
|
33
|
-
const normalizedKey = this.normalizeKey(key);
|
34
|
-
|
35
|
-
return new Promise((resolve, reject) => {
|
36
|
-
this._log({
|
37
|
-
type: DescriptRedisCache.EVENT.REDIS_CACHE_READ_START,
|
38
|
-
key,
|
39
|
-
normalizedKey,
|
40
|
-
}, context);
|
41
|
-
|
42
|
-
const networkTimerStop = contimer.start({}, 'descript-redis-cache.get.network');
|
43
|
-
const totalTimerStop = contimer.start({}, 'descript-redis-cache.get.total');
|
44
|
-
|
45
|
-
let isTimeout = false;
|
46
|
-
|
47
|
-
const timer = setTimeout(() => {
|
48
|
-
isTimeout = true;
|
49
|
-
|
50
|
-
const networkTimer = networkTimerStop();
|
51
|
-
const totalTimer = totalTimerStop();
|
52
|
-
|
53
|
-
this._log({
|
54
|
-
type: DescriptRedisCache.EVENT.REDIS_CACHE_READ_TIMEOUT,
|
55
|
-
key,
|
56
|
-
normalizedKey,
|
57
|
-
timers: {
|
58
|
-
network: networkTimer,
|
59
|
-
total: totalTimer,
|
60
|
-
},
|
61
|
-
}, context);
|
62
|
-
|
63
|
-
reject(de.error({
|
64
|
-
id: DescriptRedisCache.EVENT.REDIS_CACHE_READ_TIMEOUT,
|
65
|
-
}));
|
66
|
-
}, this._options.readTimeout);
|
67
|
-
|
68
|
-
this._client.get(normalizedKey, (error, data) => {
|
69
|
-
if (isTimeout) {
|
70
|
-
return;
|
71
|
-
}
|
72
|
-
|
73
|
-
const networkTimer = networkTimerStop();
|
74
|
-
clearTimeout(timer);
|
75
|
-
|
76
|
-
if (error) {
|
77
|
-
const totalTimer = totalTimerStop();
|
78
|
-
this._log({
|
79
|
-
type: DescriptRedisCache.EVENT.REDIS_CACHE_READ_ERROR,
|
80
|
-
error,
|
81
|
-
key,
|
82
|
-
normalizedKey,
|
83
|
-
timers: {
|
84
|
-
network: networkTimer,
|
85
|
-
total: totalTimer,
|
86
|
-
},
|
87
|
-
}, context);
|
88
|
-
|
89
|
-
reject(de.error({
|
90
|
-
id: DescriptRedisCache.EVENT.REDIS_CACHE_READ_ERROR,
|
91
|
-
}));
|
92
|
-
} else if (!data) {
|
93
|
-
const totalTimer = totalTimerStop();
|
94
|
-
this._log({
|
95
|
-
type: DescriptRedisCache.EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND,
|
96
|
-
key,
|
97
|
-
normalizedKey,
|
98
|
-
timers: {
|
99
|
-
network: networkTimer,
|
100
|
-
total: totalTimer,
|
101
|
-
},
|
102
|
-
}, context);
|
103
|
-
|
104
|
-
reject(de.error({
|
105
|
-
id: DescriptRedisCache.EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND,
|
106
|
-
}));
|
107
|
-
} else {
|
108
|
-
let parsedValue;
|
109
|
-
try {
|
110
|
-
parsedValue = JSON.parse(data);
|
111
|
-
} catch (error) {
|
112
|
-
const totalTimer = totalTimerStop();
|
113
|
-
this._log({
|
114
|
-
type: DescriptRedisCache.EVENT.REDIS_CACHE_JSON_PARSING_FAILED,
|
115
|
-
data,
|
116
|
-
error,
|
117
|
-
key,
|
118
|
-
normalizedKey,
|
119
|
-
timers: {
|
120
|
-
network: networkTimer,
|
121
|
-
total: totalTimer,
|
122
|
-
},
|
123
|
-
}, context);
|
124
|
-
|
125
|
-
reject(de.error({
|
126
|
-
id: DescriptRedisCache.EVENT.REDIS_CACHE_JSON_PARSING_FAILED,
|
127
|
-
}));
|
128
|
-
return;
|
129
|
-
}
|
130
|
-
|
131
|
-
const totalTimer = totalTimerStop();
|
132
|
-
this._log({
|
133
|
-
type: DescriptRedisCache.EVENT.REDIS_CACHE_READ_DONE,
|
134
|
-
data,
|
135
|
-
key,
|
136
|
-
normalizedKey,
|
137
|
-
timers: {
|
138
|
-
network: networkTimer,
|
139
|
-
total: totalTimer,
|
140
|
-
},
|
141
|
-
}, context);
|
142
|
-
|
143
|
-
resolve(parsedValue);
|
144
|
-
}
|
145
|
-
});
|
146
|
-
});
|
147
|
-
}
|
148
|
-
|
149
|
-
set({ key, value, maxage = this._options.defaultKeyTTL, context } ) {
|
150
|
-
if (typeof value === 'undefined') {
|
151
|
-
return;
|
152
|
-
}
|
153
|
-
|
154
|
-
const totalTimerStop = contimer.start({}, 'descript-redis-cache.set.total');
|
155
|
-
const normalizedKey = this.normalizeKey(key);
|
156
|
-
|
157
|
-
return new Promise((resolve, reject) => {
|
158
|
-
this._log({
|
159
|
-
type: DescriptRedisCache.EVENT.REDIS_CACHE_WRITE_START,
|
160
|
-
key,
|
161
|
-
normalizedKey,
|
162
|
-
}, context);
|
163
|
-
|
164
|
-
// save to cache only serializable data
|
165
|
-
const safeSerializableValue = {
|
166
|
-
status_code: value.status_code,
|
167
|
-
headers: value.headers,
|
168
|
-
result: value.result,
|
169
|
-
};
|
170
|
-
|
171
|
-
let json;
|
172
|
-
try {
|
173
|
-
json = JSON.stringify(safeSerializableValue);
|
174
|
-
} catch (error) {
|
175
|
-
const totalTimer = totalTimerStop();
|
176
|
-
this._log({
|
177
|
-
type: DescriptRedisCache.EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED,
|
178
|
-
data: value,
|
179
|
-
error,
|
180
|
-
key,
|
181
|
-
normalizedKey,
|
182
|
-
timers: {
|
183
|
-
network: {},
|
184
|
-
total: totalTimer,
|
185
|
-
},
|
186
|
-
}, context);
|
187
|
-
reject(de.error({
|
188
|
-
id: DescriptRedisCache.EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED,
|
189
|
-
}));
|
190
|
-
return;
|
191
|
-
}
|
192
|
-
|
193
|
-
const networkTimerStop = contimer.start({}, 'descript-redis-cache.set.network');
|
194
|
-
// maxage - seconds
|
195
|
-
this._client.set(normalizedKey, json, 'EX', maxage, (error, done) => {
|
196
|
-
const networkTimer = networkTimerStop();
|
197
|
-
const totalTimer = totalTimerStop();
|
198
|
-
if (error) {
|
199
|
-
this._log({
|
200
|
-
type: DescriptRedisCache.EVENT.REDIS_CACHE_WRITE_ERROR,
|
201
|
-
error,
|
202
|
-
key,
|
203
|
-
normalizedKey,
|
204
|
-
timers: {
|
205
|
-
network: networkTimer,
|
206
|
-
total: totalTimer,
|
207
|
-
},
|
208
|
-
}, context);
|
209
|
-
reject(de.error({
|
210
|
-
id: DescriptRedisCache.EVENT.REDIS_CACHE_WRITE_ERROR,
|
211
|
-
}));
|
212
|
-
} else if (!done) {
|
213
|
-
this._log({
|
214
|
-
type: DescriptRedisCache.EVENT.REDIS_CACHE_WRITE_FAILED,
|
215
|
-
key,
|
216
|
-
normalizedKey,
|
217
|
-
timers: {
|
218
|
-
network: networkTimer,
|
219
|
-
total: totalTimer,
|
220
|
-
},
|
221
|
-
}, context);
|
222
|
-
reject(de.error({
|
223
|
-
id: DescriptRedisCache.EVENT.REDIS_CACHE_WRITE_FAILED,
|
224
|
-
}));
|
225
|
-
} else {
|
226
|
-
this._log({
|
227
|
-
type: DescriptRedisCache.EVENT.REDIS_CACHE_WRITE_DONE,
|
228
|
-
data: json,
|
229
|
-
key,
|
230
|
-
normalizedKey,
|
231
|
-
timers: {
|
232
|
-
network: networkTimer,
|
233
|
-
total: totalTimer,
|
234
|
-
},
|
235
|
-
}, context);
|
236
|
-
resolve();
|
237
|
-
}
|
238
|
-
});
|
239
|
-
});
|
240
|
-
}
|
241
|
-
|
242
|
-
/**
|
243
|
-
* Generates normalized SHA-512 key with generation
|
244
|
-
* @param {string} key
|
245
|
-
* @returns {string}
|
246
|
-
*/
|
247
|
-
normalizeKey(key) {
|
248
|
-
const value = `g${ this._options.generation }:${ key }`;
|
249
|
-
return crypto
|
250
|
-
.createHash('sha512')
|
251
|
-
.update(value, 'utf8')
|
252
|
-
.digest('hex');
|
253
|
-
}
|
254
|
-
|
255
|
-
_log(event, context) {
|
256
|
-
if (this._logger) {
|
257
|
-
this._logger.log(event, context);
|
258
|
-
}
|
259
|
-
}
|
260
|
-
}
|
261
|
-
|
262
|
-
DescriptRedisCache.EVENT = {
|
263
|
-
REDIS_CACHE_INITIALIZED: 'REDIS_CACHE_INITIALIZED',
|
264
|
-
|
265
|
-
REDIS_CACHE_JSON_PARSING_FAILED: 'REDIS_CACHE_JSON_PARSING_FAILED',
|
266
|
-
REDIS_CACHE_JSON_STRINGIFY_FAILED: 'REDIS_CACHE_JSON_STRINGIFY_FAILED',
|
267
|
-
|
268
|
-
REDIS_CACHE_READ_DONE: 'REDIS_CACHE_READ_DONE',
|
269
|
-
REDIS_CACHE_READ_ERROR: 'REDIS_CACHE_READ_ERROR',
|
270
|
-
REDIS_CACHE_READ_KEY_NOT_FOUND: 'REDIS_CACHE_READ_KEY_NOT_FOUND',
|
271
|
-
REDIS_CACHE_READ_START: 'REDIS_CACHE_READ_START',
|
272
|
-
REDIS_CACHE_READ_TIMEOUT: 'REDIS_CACHE_READ_TIMEOUT',
|
273
|
-
|
274
|
-
REDIS_CACHE_WRITE_DONE: 'REDIS_CACHE_WRITE_DONE',
|
275
|
-
REDIS_CACHE_WRITE_ERROR: 'REDIS_CACHE_WRITE_ERROR',
|
276
|
-
REDIS_CACHE_WRITE_FAILED: 'REDIS_CACHE_WRITE_FAILED',
|
277
|
-
REDIS_CACHE_WRITE_START: 'REDIS_CACHE_READ_START',
|
278
|
-
};
|
279
|
-
|
280
|
-
module.exports = DescriptRedisCache;
|