@go-mailer/jarvis 10.0.3 → 10.1.0
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/index.js +2 -0
- package/lib/middlewares/cache/manager.js +84 -0
- package/lib/middlewares/cache/schema.js +12 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -8,6 +8,7 @@ const AutomationConstants = require('./lib/constants/automation')
|
|
|
8
8
|
const APIClient = require('./lib/middlewares/client')
|
|
9
9
|
const { RequestLogger, ProcessLogger } = require('./lib/middlewares/logger')
|
|
10
10
|
const Utility = require('./lib/utilitiy/index')
|
|
11
|
+
const { HTTPCacheManager } = require('./lib/middlewares/cache/manager')
|
|
11
12
|
|
|
12
13
|
module.exports = {
|
|
13
14
|
APIClient,
|
|
@@ -16,6 +17,7 @@ module.exports = {
|
|
|
16
17
|
EnvVar,
|
|
17
18
|
FeatureFlag,
|
|
18
19
|
HTTP,
|
|
20
|
+
HTTPCacheManager,
|
|
19
21
|
ProcessLogger,
|
|
20
22
|
QueryBuilder,
|
|
21
23
|
Redis,
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
const { EntityId } = require("redis-om");
|
|
2
|
+
const { RedisClient } = require("../../redis");
|
|
3
|
+
const { HTTPCache } = require("./schema");
|
|
4
|
+
|
|
5
|
+
const HTTPCacheManager = () => {
|
|
6
|
+
const clearCache = async (request) => {
|
|
7
|
+
const { tenant_id, cache_resource } = request;
|
|
8
|
+
const repo = await RedisClient.register_schema(HTTPCache);
|
|
9
|
+
const cached_records = await repo
|
|
10
|
+
.search()
|
|
11
|
+
.where("tenant_id")
|
|
12
|
+
.equals(tenant_id)
|
|
13
|
+
.where("resource")
|
|
14
|
+
.equals(cache_resource)
|
|
15
|
+
.returnAll();
|
|
16
|
+
for (const cached_record of cached_records) {
|
|
17
|
+
await repo.remove(cached_record[EntityId]);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const handleRequest = async (request, response, next) => {
|
|
22
|
+
const { tenant_id, cache_key } = request;
|
|
23
|
+
const repo = await RedisClient.register_schema(HTTPCache);
|
|
24
|
+
if (isNaN(tenant_id)) return next();
|
|
25
|
+
|
|
26
|
+
let cached_record = await repo
|
|
27
|
+
.search()
|
|
28
|
+
.where("tenant_id")
|
|
29
|
+
.equals(tenant_id)
|
|
30
|
+
.where("key")
|
|
31
|
+
.equals(cache_key)
|
|
32
|
+
.returnFirst();
|
|
33
|
+
|
|
34
|
+
let payload = {};
|
|
35
|
+
if (cached_record) {
|
|
36
|
+
payload = JSON.parse(cached_record.data);
|
|
37
|
+
return response.status(payload.status_code).json(payload);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
next();
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const save = async (request, __, next) => {
|
|
44
|
+
const { tenant_id, cache_key, cache_resource, payload } = request;
|
|
45
|
+
if (isNaN(tenant_id)) return next();
|
|
46
|
+
const repo = await RedisClient.register_schema(HTTPCache);
|
|
47
|
+
|
|
48
|
+
let cached_record = await repo
|
|
49
|
+
.search()
|
|
50
|
+
.where("tenant_id")
|
|
51
|
+
.equals(tenant_id)
|
|
52
|
+
.where("key")
|
|
53
|
+
.equals(cache_key)
|
|
54
|
+
.returnFirst();
|
|
55
|
+
|
|
56
|
+
const data_to_cache = {
|
|
57
|
+
tenant_id,
|
|
58
|
+
key: cache_key,
|
|
59
|
+
resource: cache_resource,
|
|
60
|
+
data: JSON.stringify(payload),
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
if (cached_record) {
|
|
64
|
+
await repo.save(cached_record[EntityId], data_to_cache);
|
|
65
|
+
} else {
|
|
66
|
+
cached_record = await repo.save(data_to_cache);
|
|
67
|
+
await repo.expire(cached_record[EntityId], 10 * 60);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
next();
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const setup = (request, __, next) => {
|
|
74
|
+
const { _parsedUrl } = request;
|
|
75
|
+
const resource = _parsedUrl.pathname.split("/")[1];
|
|
76
|
+
request.cache_key = request.originalUrl;
|
|
77
|
+
request.cache_resource = resource;
|
|
78
|
+
next();
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return { clearCache, handleRequest, save, setup };
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
module.exports = { HTTPCacheManager };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const { Schema } = require("redis-om");
|
|
2
|
+
|
|
3
|
+
const HTTPCache = new Schema("httpcache", {
|
|
4
|
+
data: { type: "string" },
|
|
5
|
+
key: { type: "string" },
|
|
6
|
+
resource: { type: "string" },
|
|
7
|
+
tenant_id: { type: "number" },
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
module.exports = {
|
|
11
|
+
HTTPCache,
|
|
12
|
+
};
|