@candlerip/shared 0.0.127 → 0.0.129
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CustomErrorWorker
|
|
1
|
+
import { CustomErrorWorker } from '../../../common/index.js';
|
|
2
2
|
import { createClient } from 'redis';
|
|
3
3
|
export const CacheWorker = (environmentMode, url, password, options) => {
|
|
4
4
|
const _isEnabled = Boolean(options?.isEnabled);
|
|
@@ -37,138 +37,55 @@ export const CacheWorker = (environmentMode, url, password, options) => {
|
|
|
37
37
|
data: _client,
|
|
38
38
|
};
|
|
39
39
|
};
|
|
40
|
-
const getCache = async (
|
|
41
|
-
const { composeCustomError } = CustomErrorWorker({ info: {
|
|
42
|
-
const { data: client, customError } = _getClient();
|
|
43
|
-
if (customError) {
|
|
44
|
-
return { customError };
|
|
45
|
-
}
|
|
46
|
-
let data;
|
|
47
|
-
try {
|
|
48
|
-
const cache = await client.get(key);
|
|
49
|
-
if (cache) {
|
|
50
|
-
data = JSON.parse(cache);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
catch (err) {
|
|
54
|
-
return {
|
|
55
|
-
customError: composeCustomError('Error get data from Redis', { err }),
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
if (!data) {
|
|
59
|
-
return {
|
|
60
|
-
customError: composeCustomError('No data in Redis'),
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
return {
|
|
64
|
-
data,
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
const mGetCache = async (keys) => {
|
|
68
|
-
const { composeCustomError } = CustomErrorWorker({ info: { keys } });
|
|
40
|
+
const getCache = async (cacheKey, objKeys) => {
|
|
41
|
+
const { composeCustomError } = CustomErrorWorker({ info: { cacheKey, objKeys } });
|
|
69
42
|
const { data: client, customError } = _getClient();
|
|
70
43
|
if (customError) {
|
|
71
44
|
return { customError };
|
|
72
45
|
}
|
|
73
46
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
-
|
|
47
|
+
let data;
|
|
48
|
+
const pathKeys = objKeys?.map((it) => `$.${it}`);
|
|
75
49
|
try {
|
|
76
|
-
|
|
77
|
-
if (!
|
|
50
|
+
data = await client.json.get(cacheKey, { path: pathKeys });
|
|
51
|
+
if (!data) {
|
|
78
52
|
return {
|
|
79
|
-
customError: composeCustomError('
|
|
53
|
+
customError: composeCustomError('No data in Redis'),
|
|
80
54
|
};
|
|
81
55
|
}
|
|
82
|
-
cache.forEach((it, i) => {
|
|
83
|
-
data[keys[i]] = JSON.parse(it);
|
|
84
|
-
});
|
|
85
56
|
}
|
|
86
57
|
catch (err) {
|
|
87
58
|
return {
|
|
88
|
-
customError: composeCustomError('Error get data from
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
if (Object.keys(data).length !== keys.length) {
|
|
92
|
-
return {
|
|
93
|
-
customError: composeCustomError('Missing data from redis', { data }),
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
return {
|
|
97
|
-
data,
|
|
98
|
-
};
|
|
99
|
-
};
|
|
100
|
-
const setCache = async (key, data) => {
|
|
101
|
-
const { composeCustomError } = CustomErrorWorker({ info: { key } });
|
|
102
|
-
const { data: client, customError } = _getClient();
|
|
103
|
-
if (customError) {
|
|
104
|
-
return { customError };
|
|
105
|
-
}
|
|
106
|
-
try {
|
|
107
|
-
await client.set(key, JSON.stringify(data));
|
|
108
|
-
}
|
|
109
|
-
catch (err) {
|
|
110
|
-
return {
|
|
111
|
-
customError: composeCustomError('Error set data to redis', { err }),
|
|
59
|
+
customError: composeCustomError('Error get data by object keys from Redis', { err }),
|
|
112
60
|
};
|
|
113
61
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const { data: client, customError } = _getClient();
|
|
121
|
-
if (customError) {
|
|
122
|
-
return { customError };
|
|
123
|
-
}
|
|
124
|
-
let data;
|
|
125
|
-
if (!objKeys) {
|
|
126
|
-
try {
|
|
127
|
-
data = await client.hGetAll(cacheKey);
|
|
128
|
-
if (Object.keys(data).length === 0) {
|
|
129
|
-
return {
|
|
130
|
-
customError: composeCustomError('No data in Redis'),
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
return {
|
|
134
|
-
data,
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
catch (err) {
|
|
138
|
-
return {
|
|
139
|
-
customError: composeCustomError('Error get data from Redis', { err }),
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
try {
|
|
144
|
-
data = await client.hmGet(cacheKey, objKeys);
|
|
62
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
63
|
+
let normalizedData = {};
|
|
64
|
+
if (objKeys) {
|
|
65
|
+
objKeys.forEach((objKey) => {
|
|
66
|
+
normalizedData[objKey] = data[`$.${objKey}`][0];
|
|
67
|
+
});
|
|
145
68
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
customError: composeCustomError('Error get data by object keys from Redis', { err }),
|
|
149
|
-
};
|
|
69
|
+
else {
|
|
70
|
+
normalizedData = data;
|
|
150
71
|
}
|
|
151
|
-
if (
|
|
72
|
+
if (Object.keys(normalizedData).some((key) => normalizedData[key] === null)) {
|
|
152
73
|
return {
|
|
153
74
|
customError: composeCustomError('Error get data by object keys from Redis', { data }),
|
|
154
75
|
};
|
|
155
76
|
}
|
|
156
77
|
return {
|
|
157
|
-
data:
|
|
158
|
-
...acc,
|
|
159
|
-
[objKeys[index]]: curr,
|
|
160
|
-
}), {}),
|
|
78
|
+
data: normalizedData,
|
|
161
79
|
};
|
|
162
80
|
};
|
|
163
|
-
const
|
|
81
|
+
const setCache = async (key, data) => {
|
|
164
82
|
const { composeCustomError } = CustomErrorWorker({ info: { key } });
|
|
165
83
|
const { data: client, customError } = _getClient();
|
|
166
84
|
if (customError) {
|
|
167
85
|
return { customError };
|
|
168
86
|
}
|
|
169
87
|
try {
|
|
170
|
-
await client.
|
|
171
|
-
await _client.json.set(key, '$', data);
|
|
88
|
+
await client.json.set(key, '$', data);
|
|
172
89
|
}
|
|
173
90
|
catch (err) {
|
|
174
91
|
return {
|
|
@@ -180,10 +97,7 @@ export const CacheWorker = (environmentMode, url, password, options) => {
|
|
|
180
97
|
};
|
|
181
98
|
};
|
|
182
99
|
return {
|
|
183
|
-
hGetCache,
|
|
184
|
-
hSetCache,
|
|
185
100
|
getCache,
|
|
186
|
-
mGetCache,
|
|
187
101
|
setCache,
|
|
188
102
|
};
|
|
189
103
|
};
|
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
import { EnvironmentMode, TCustomError } from '../../../common/index.js';
|
|
2
2
|
import { Cache } from '../../common/cache/domains.js';
|
|
3
3
|
import { createClient } from 'redis';
|
|
4
|
-
import {
|
|
4
|
+
import { CommonPageDictionaryCacheKey } from '../../common/index.js';
|
|
5
5
|
export type TCacheWorker = (environmentMode: EnvironmentMode, url: string, password: string, options?: {
|
|
6
6
|
isEnabled?: boolean;
|
|
7
7
|
}) => {
|
|
8
8
|
getCache: GetCache;
|
|
9
|
-
hGetCache: HGetCache;
|
|
10
|
-
hSetCache: HSetCache;
|
|
11
|
-
mGetCache: MGetCache;
|
|
12
9
|
setCache: SetCache;
|
|
13
10
|
};
|
|
14
|
-
export type GetCache = <T extends CacheKey = never, D extends Cache[T] = Cache[T]>(key: T) => Promise<TCustomError<D>>;
|
|
15
|
-
export type MGetCache = <T extends CacheKey = never, D extends Pick<Cache, T> = Pick<Cache, T>>(keys: T[]) => Promise<TCustomError<D>>;
|
|
16
|
-
export type SetCache = <T extends CacheKey = never>(key: T, data: Cache[T]) => Promise<TCustomError<null>>;
|
|
17
11
|
export type GetClient = () => TCustomError<ReturnType<typeof createClient>>;
|
|
18
|
-
export type
|
|
19
|
-
export type
|
|
12
|
+
export type GetCache = <T extends CommonPageDictionaryCacheKey, D extends Cache[T]>(cacheKey: T, objKeys?: string[]) => Promise<TCustomError<D>>;
|
|
13
|
+
export type SetCache = <T extends CommonPageDictionaryCacheKey>(key: T, data: Cache[T]) => Promise<TCustomError<null>>;
|