@go-mailer/jarvis 10.1.0 → 10.1.2
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/middlewares/cache/manager.js +24 -20
- package/package.json +1 -1
|
@@ -3,7 +3,7 @@ const { RedisClient } = require("../../redis");
|
|
|
3
3
|
const { HTTPCache } = require("./schema");
|
|
4
4
|
|
|
5
5
|
const HTTPCacheManager = () => {
|
|
6
|
-
const clearCache = async (request) => {
|
|
6
|
+
const clearCache = async (request, response, next) => {
|
|
7
7
|
const { tenant_id, cache_resource } = request;
|
|
8
8
|
const repo = await RedisClient.register_schema(HTTPCache);
|
|
9
9
|
const cached_records = await repo
|
|
@@ -16,6 +16,7 @@ const HTTPCacheManager = () => {
|
|
|
16
16
|
for (const cached_record of cached_records) {
|
|
17
17
|
await repo.remove(cached_record[EntityId]);
|
|
18
18
|
}
|
|
19
|
+
next()
|
|
19
20
|
};
|
|
20
21
|
|
|
21
22
|
const handleRequest = async (request, response, next) => {
|
|
@@ -43,28 +44,31 @@ const HTTPCacheManager = () => {
|
|
|
43
44
|
const save = async (request, __, next) => {
|
|
44
45
|
const { tenant_id, cache_key, cache_resource, payload } = request;
|
|
45
46
|
if (isNaN(tenant_id)) return next();
|
|
46
|
-
const repo = await RedisClient.register_schema(HTTPCache);
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
.
|
|
50
|
-
.where("tenant_id")
|
|
51
|
-
.equals(tenant_id)
|
|
52
|
-
.where("key")
|
|
53
|
-
.equals(cache_key)
|
|
54
|
-
.returnFirst();
|
|
48
|
+
if (payload.status_code >= 200 && payload.status_code < 300) {
|
|
49
|
+
const repo = await RedisClient.register_schema(HTTPCache);
|
|
55
50
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
51
|
+
let cached_record = await repo
|
|
52
|
+
.search()
|
|
53
|
+
.where("tenant_id")
|
|
54
|
+
.equals(tenant_id)
|
|
55
|
+
.where("key")
|
|
56
|
+
.equals(cache_key)
|
|
57
|
+
.returnFirst();
|
|
62
58
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
59
|
+
const data_to_cache = {
|
|
60
|
+
tenant_id,
|
|
61
|
+
key: cache_key,
|
|
62
|
+
resource: cache_resource,
|
|
63
|
+
data: JSON.stringify(payload),
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
if (cached_record) {
|
|
67
|
+
await repo.save(cached_record[EntityId], data_to_cache);
|
|
68
|
+
} else {
|
|
69
|
+
cached_record = await repo.save(data_to_cache);
|
|
70
|
+
await repo.expire(cached_record[EntityId], 10 * 60);
|
|
71
|
+
}
|
|
68
72
|
}
|
|
69
73
|
|
|
70
74
|
next();
|