@candlerip/shared 0.0.124 → 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, isArray, isString } from '../../../common/index.js';
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,64 +37,45 @@ export const CacheWorker = (environmentMode, url, password, options) => {
37
37
  data: _client,
38
38
  };
39
39
  };
40
- const getCache = async (key) => {
41
- const { composeCustomError } = CustomErrorWorker({ info: { key } });
40
+ const getCache = async (cacheKey, objKeys) => {
41
+ const { composeCustomError } = CustomErrorWorker({ info: { cacheKey, objKeys } });
42
42
  const { data: client, customError } = _getClient();
43
43
  if (customError) {
44
44
  return { customError };
45
45
  }
46
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
47
  let data;
48
+ const pathKeys = objKeys?.map((it) => `$.${it}`);
47
49
  try {
48
- const cache = await client.get(key);
49
- if (cache) {
50
- data = JSON.parse(cache);
50
+ data = await client.json.get(cacheKey, { path: pathKeys });
51
+ if (!data) {
52
+ return {
53
+ customError: composeCustomError('No data in Redis'),
54
+ };
51
55
  }
52
56
  }
53
57
  catch (err) {
54
58
  return {
55
- customError: composeCustomError('Error get data from Redis', { err }),
56
- };
57
- }
58
- if (!data) {
59
- return {
60
- customError: composeCustomError('No data in Redis'),
59
+ customError: composeCustomError('Error get data by object keys from Redis', { err }),
61
60
  };
62
61
  }
63
- return {
64
- data,
65
- };
66
- };
67
- const mGetCache = async (keys) => {
68
- const { composeCustomError } = CustomErrorWorker({ info: { keys } });
69
- const { data: client, customError } = _getClient();
70
- if (customError) {
71
- return { customError };
72
- }
73
62
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
- const data = {};
75
- try {
76
- const cache = await client.mGet(keys);
77
- if (!isArray(cache, isString)) {
78
- return {
79
- customError: composeCustomError('Error get data from redis', { cache }),
80
- };
81
- }
82
- cache.forEach((it, i) => {
83
- data[keys[i]] = JSON.parse(it);
63
+ let normalizedData = {};
64
+ if (objKeys) {
65
+ objKeys.forEach((objKey) => {
66
+ normalizedData[objKey] = data[`$.${objKey}`][0];
84
67
  });
85
68
  }
86
- catch (err) {
87
- return {
88
- customError: composeCustomError('Error get data from redis', { err }),
89
- };
69
+ else {
70
+ normalizedData = data;
90
71
  }
91
- if (Object.keys(data).length !== keys.length) {
72
+ if (Object.keys(normalizedData).some((key) => normalizedData[key] === null)) {
92
73
  return {
93
- customError: composeCustomError('Missing data from redis', { data }),
74
+ customError: composeCustomError('Error get data by object keys from Redis', { data }),
94
75
  };
95
76
  }
96
77
  return {
97
- data,
78
+ data: normalizedData,
98
79
  };
99
80
  };
100
81
  const setCache = async (key, data) => {
@@ -104,65 +85,7 @@ export const CacheWorker = (environmentMode, url, password, options) => {
104
85
  return { customError };
105
86
  }
106
87
  try {
107
- await client.set(key, JSON.stringify(data));
108
- }
109
- catch (err) {
110
- return {
111
- customError: composeCustomError('Error set data to redis', { err }),
112
- };
113
- }
114
- return {
115
- data: null,
116
- };
117
- };
118
- const hGetCache = async (cacheKey, objKeys) => {
119
- const { composeCustomError } = CustomErrorWorker({ info: { cacheKey, objKeys } });
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);
145
- }
146
- catch (err) {
147
- return {
148
- customError: composeCustomError('Error get data by object keys from Redis', { err }),
149
- };
150
- }
151
- return {
152
- data: data.reduce((acc, curr, index) => ({
153
- ...acc,
154
- [objKeys[index]]: curr,
155
- }), {}),
156
- };
157
- };
158
- const hSetCache = async (key, data) => {
159
- const { composeCustomError } = CustomErrorWorker({ info: { key } });
160
- const { data: client, customError } = _getClient();
161
- if (customError) {
162
- return { customError };
163
- }
164
- try {
165
- await client.hSet(key, data);
88
+ await client.json.set(key, '$', data);
166
89
  }
167
90
  catch (err) {
168
91
  return {
@@ -174,10 +97,7 @@ export const CacheWorker = (environmentMode, url, password, options) => {
174
97
  };
175
98
  };
176
99
  return {
177
- hGetCache,
178
- hSetCache,
179
100
  getCache,
180
- mGetCache,
181
101
  setCache,
182
102
  };
183
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 { CacheKey, CommonPageDictionaryCacheKey } from '../../common/index.js';
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 HGetCache = <T extends CommonPageDictionaryCacheKey, D extends Cache[T]>(cacheKey: T, objKeys?: string[]) => Promise<TCustomError<D>>;
19
- export type HSetCache = <T extends CommonPageDictionaryCacheKey>(key: T, data: Cache[T]) => Promise<TCustomError<null>>;
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>>;
@@ -15,5 +15,6 @@ export const filterPageTranslationCodes = (variant, options) => PAGE_TRANSLATION
15
15
  if (isPageMenu(variant) && code === `pageMenu.${variant}`) {
16
16
  return false;
17
17
  }
18
+ return true;
18
19
  }
19
20
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@candlerip/shared",
3
- "version": "0.0.124",
3
+ "version": "0.0.129",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": "=22.19.0"