lesgo 2.1.6 → 2.1.7

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,3 +1,3 @@
1
1
  import { ClientOptions } from '../../types/aws';
2
- declare const deleteRedisCache: (key: string, clientOpts?: ClientOptions) => Promise<number>;
2
+ declare const deleteRedisCache: (keys: string | string[], clientOpts?: ClientOptions) => Promise<number>;
3
3
  export default deleteRedisCache;
@@ -31,20 +31,20 @@ var __awaiter =
31
31
  step((generator = generator.apply(thisArg, _arguments || [])).next());
32
32
  });
33
33
  };
34
- import { logger, validateFields } from '../../utils';
34
+ import { logger } from '../../utils';
35
35
  import { LesgoException } from '../../exceptions';
36
36
  import getElastiCacheRedisClient from './getElastiCacheRedisClient';
37
37
  const FILE = 'lesgo.services.ElastiCacheRedis.deleteRedisCache';
38
- const deleteRedisCache = (key, clientOpts) =>
38
+ const deleteRedisCache = (keys, clientOpts) =>
39
39
  __awaiter(void 0, void 0, void 0, function* () {
40
- const input = validateFields({ key }, [
41
- { key: 'key', type: 'string', required: true },
42
- ]);
43
40
  const client = yield getElastiCacheRedisClient(clientOpts);
44
41
  let resp;
42
+ if (!Array.isArray(keys)) {
43
+ keys = [keys];
44
+ }
45
45
  try {
46
- resp = yield client.del(input.key);
47
- logger.debug(`${FILE}::RECEIVED_RESPONSE`, { resp, input });
46
+ resp = yield client.del(...keys);
47
+ logger.debug(`${FILE}::RECEIVED_RESPONSE`, { resp, keys });
48
48
  return resp;
49
49
  } catch (err) {
50
50
  throw new LesgoException(
@@ -53,7 +53,7 @@ const deleteRedisCache = (key, clientOpts) =>
53
53
  500,
54
54
  {
55
55
  err,
56
- input,
56
+ keys,
57
57
  clientOpts,
58
58
  }
59
59
  );
@@ -2,4 +2,5 @@ export { default as deleteRedisCache } from './deleteRedisCache';
2
2
  export { default as disconnectElastiCacheRedisClient } from './disconnectElastiCacheRedisClient';
3
3
  export { default as getElastiCacheRedisClient } from './getElastiCacheRedisClient';
4
4
  export { default as getRedisCache } from './getRedisCache';
5
+ export { default as scanRedisCache } from './scanRedisCache';
5
6
  export { default as setRedisCache } from './setRedisCache';
@@ -2,4 +2,5 @@ export { default as deleteRedisCache } from './deleteRedisCache';
2
2
  export { default as disconnectElastiCacheRedisClient } from './disconnectElastiCacheRedisClient';
3
3
  export { default as getElastiCacheRedisClient } from './getElastiCacheRedisClient';
4
4
  export { default as getRedisCache } from './getRedisCache';
5
+ export { default as scanRedisCache } from './scanRedisCache';
5
6
  export { default as setRedisCache } from './setRedisCache';
@@ -0,0 +1,12 @@
1
+ import { ClientOptions } from '../../types/aws';
2
+ /**
3
+ * Scans the Redis cache for keys matching a given pattern.
4
+ *
5
+ * @param {string} pattern - The pattern to match keys against.
6
+ * @param {ClientOptions} [clientOpts] - Optional client options for connecting to the Redis instance.
7
+ * @returns {Promise<string[]>} - A promise that resolves to an array of keys matching the pattern.
8
+ *
9
+ * @throws {LesgoException} - Throws an exception if the scan operation fails.
10
+ */
11
+ declare const scanRedisCache: (pattern: string, clientOpts?: ClientOptions) => Promise<string[]>;
12
+ export default scanRedisCache;
@@ -0,0 +1,83 @@
1
+ var __awaiter =
2
+ (this && this.__awaiter) ||
3
+ function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) {
5
+ return value instanceof P
6
+ ? value
7
+ : new P(function (resolve) {
8
+ resolve(value);
9
+ });
10
+ }
11
+ return new (P || (P = Promise))(function (resolve, reject) {
12
+ function fulfilled(value) {
13
+ try {
14
+ step(generator.next(value));
15
+ } catch (e) {
16
+ reject(e);
17
+ }
18
+ }
19
+ function rejected(value) {
20
+ try {
21
+ step(generator['throw'](value));
22
+ } catch (e) {
23
+ reject(e);
24
+ }
25
+ }
26
+ function step(result) {
27
+ result.done
28
+ ? resolve(result.value)
29
+ : adopt(result.value).then(fulfilled, rejected);
30
+ }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ import { logger, validateFields } from '../../utils';
35
+ import { LesgoException } from '../../exceptions';
36
+ import getElastiCacheRedisClient from './getElastiCacheRedisClient';
37
+ const FILE = 'lesgo.services.ElastiCacheRedis.scanRedisCache';
38
+ /**
39
+ * Scans the Redis cache for keys matching a given pattern.
40
+ *
41
+ * @param {string} pattern - The pattern to match keys against.
42
+ * @param {ClientOptions} [clientOpts] - Optional client options for connecting to the Redis instance.
43
+ * @returns {Promise<string[]>} - A promise that resolves to an array of keys matching the pattern.
44
+ *
45
+ * @throws {LesgoException} - Throws an exception if the scan operation fails.
46
+ */
47
+ const scanRedisCache = (pattern, clientOpts) =>
48
+ __awaiter(void 0, void 0, void 0, function* () {
49
+ const input = validateFields({ pattern }, [
50
+ { key: 'pattern', type: 'string', required: true },
51
+ ]);
52
+ const client = yield getElastiCacheRedisClient(clientOpts);
53
+ const keys = [];
54
+ try {
55
+ let cursor = '0'; // Start cursor
56
+ do {
57
+ // Perform SCAN operation
58
+ const [newCursor, matchedKeys] = yield client.scan(
59
+ cursor,
60
+ 'MATCH',
61
+ pattern
62
+ );
63
+ cursor = newCursor;
64
+ if (matchedKeys.length > 0) {
65
+ keys.push(...matchedKeys);
66
+ }
67
+ } while (cursor !== '0'); // Continue until cursor is 0 (scan complete)
68
+ logger.debug(`${FILE}::RECEIVED_RESPONSE`, { keys, input });
69
+ } catch (err) {
70
+ throw new LesgoException(
71
+ 'Failed to scan redis cache',
72
+ `${FILE}::SCAN_ERROR`,
73
+ 500,
74
+ {
75
+ err,
76
+ input,
77
+ clientOpts,
78
+ }
79
+ );
80
+ }
81
+ return keys;
82
+ });
83
+ export default scanRedisCache;
@@ -2,7 +2,7 @@ import { ClientOptions } from '../../../types/aws';
2
2
  /**
3
3
  * Deletes the cache value from the Redis cache.
4
4
  *
5
- * @param {string} key - The key of the cache value to delete.
5
+ * @param {string | string[]} keys - The key(s) of the cache value to delete.
6
6
  * @param {ClientOptions} clientOpts - Optional client options for Redis connection.
7
7
  * @returns A promise that resolves to the deleted cache value.
8
8
  * @throws {LesgoException} If there is an error deleting the cache.
@@ -11,9 +11,10 @@ import { ClientOptions } from '../../../types/aws';
11
11
  * ```typescript
12
12
  * import { deleteCache } from 'lesgo/utils/cache/redis';
13
13
  *
14
- * const key = 'myKey';
14
+ * const keys = 'myKey';
15
+ * // const keys = ['key1', 'key2'];
15
16
  *
16
- * await deleteCache(key);
17
+ * await deleteCache(keys);
17
18
  */
18
- declare const getCache: (key: string, clientOpts?: ClientOptions) => Promise<number>;
19
- export default getCache;
19
+ declare const deleteCache: (keys: string | string[], clientOpts?: ClientOptions) => Promise<number>;
20
+ export default deleteCache;
@@ -2,7 +2,7 @@ import { deleteRedisCache } from '../../../services/ElastiCacheRedisService';
2
2
  /**
3
3
  * Deletes the cache value from the Redis cache.
4
4
  *
5
- * @param {string} key - The key of the cache value to delete.
5
+ * @param {string | string[]} keys - The key(s) of the cache value to delete.
6
6
  * @param {ClientOptions} clientOpts - Optional client options for Redis connection.
7
7
  * @returns A promise that resolves to the deleted cache value.
8
8
  * @throws {LesgoException} If there is an error deleting the cache.
@@ -11,11 +11,12 @@ import { deleteRedisCache } from '../../../services/ElastiCacheRedisService';
11
11
  * ```typescript
12
12
  * import { deleteCache } from 'lesgo/utils/cache/redis';
13
13
  *
14
- * const key = 'myKey';
14
+ * const keys = 'myKey';
15
+ * // const keys = ['key1', 'key2'];
15
16
  *
16
- * await deleteCache(key);
17
+ * await deleteCache(keys);
17
18
  */
18
- const getCache = (key, clientOpts) => {
19
- return deleteRedisCache(key, clientOpts);
19
+ const deleteCache = (keys, clientOpts) => {
20
+ return deleteRedisCache(keys, clientOpts);
20
21
  };
21
- export default getCache;
22
+ export default deleteCache;
@@ -2,4 +2,5 @@ export { default as disconnectCache } from './disconnectCache';
2
2
  export { default as deleteCache } from './deleteCache';
3
3
  export { default as getCache } from './getCache';
4
4
  export { default as getClient } from './getClient';
5
+ export { default as scanCache } from './scanCache';
5
6
  export { default as setCache } from './setCache';
@@ -2,4 +2,5 @@ export { default as disconnectCache } from './disconnectCache';
2
2
  export { default as deleteCache } from './deleteCache';
3
3
  export { default as getCache } from './getCache';
4
4
  export { default as getClient } from './getClient';
5
+ export { default as scanCache } from './scanCache';
5
6
  export { default as setCache } from './setCache';
@@ -0,0 +1,23 @@
1
+ import { ClientOptions } from '../../../types/aws';
2
+ /**
3
+ * Scans the Redis cache for keys matching a given pattern.
4
+ *
5
+ * @param {string} pattern - The pattern to match keys against.
6
+ * @param {ClientOptions} clientOpts - Optional client options for the cache client.
7
+ * @returns A promise that resolves when the value is retrieved from the cache.
8
+ *
9
+ * @throws {LesgoException} If there is an error retrieving the cache.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { scanCache } from 'lesgo/utils/cache/redis';
14
+ *
15
+ * const pattern = 'getUser:byId:*';
16
+ * // const pattern = 'getUser:*';
17
+ *
18
+ * const keys = await scanCache(pattern);
19
+ * console.log(keys); // Array of cache keys returned from the scan operation
20
+ * ```
21
+ */
22
+ declare const scanCache: (pattern: string, clientOpts?: ClientOptions) => Promise<string[]>;
23
+ export default scanCache;
@@ -0,0 +1,25 @@
1
+ import { scanRedisCache } from '../../../services/ElastiCacheRedisService';
2
+ /**
3
+ * Scans the Redis cache for keys matching a given pattern.
4
+ *
5
+ * @param {string} pattern - The pattern to match keys against.
6
+ * @param {ClientOptions} clientOpts - Optional client options for the cache client.
7
+ * @returns A promise that resolves when the value is retrieved from the cache.
8
+ *
9
+ * @throws {LesgoException} If there is an error retrieving the cache.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { scanCache } from 'lesgo/utils/cache/redis';
14
+ *
15
+ * const pattern = 'getUser:byId:*';
16
+ * // const pattern = 'getUser:*';
17
+ *
18
+ * const keys = await scanCache(pattern);
19
+ * console.log(keys); // Array of cache keys returned from the scan operation
20
+ * ```
21
+ */
22
+ const scanCache = (pattern, clientOpts) => {
23
+ return scanRedisCache(pattern, clientOpts);
24
+ };
25
+ export default scanCache;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lesgo",
3
- "version": "2.1.6",
3
+ "version": "2.1.7",
4
4
  "description": "Core framework for lesgo node.js serverless framework.",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",