eip-cloud-services 1.0.10 → 1.0.11

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/redis.js +31 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eip-cloud-services",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "description": "Houses a collection of helpers for connecting with Cloud services.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,4 +22,4 @@
22
22
  "mysql": "^2.18.1",
23
23
  "redis": "^4.6.7"
24
24
  }
25
- }
25
+ }
package/src/redis.js CHANGED
@@ -49,6 +49,37 @@ exports.keys = async ( pattern ) => {
49
49
  }
50
50
  };
51
51
 
52
+ /**
53
+ * Iteratively scans through keys in the Redis database.
54
+ * @param {number} cursor - The cursor to start scanning from. Use 0 to start a new scan.
55
+ * @param {string} [matchPattern='*'] - The pattern to match keys against. Defaults to '*' to match all keys.
56
+ * @param {number} [count=100] - The number of keys to return per call. Defaults to 100.
57
+ * @returns {Promise<Array>} - A promise that resolves with an array containing the next cursor and an array of keys.
58
+ *
59
+ * @description
60
+ * This method uses the SCAN command to iteratively scan through the keys in the Redis database.
61
+ * It allows for efficient scanning of large datasets without the risk of blocking the server for a long time.
62
+ * The method returns a promise that resolves with an array, where the first element is the next cursor to be used
63
+ * for subsequent calls, and the second element is an array of keys that matched the pattern.
64
+ */
65
+ exports.scan = async ( cursor, matchPattern = '*', count = 100 ) => {
66
+ try {
67
+ const client = await getClient ();
68
+
69
+ const commandArgs = [
70
+ String ( cursor ),
71
+ 'MATCH', matchPattern.startsWith ( config.redis.prefix ) ? matchPattern : config.redis.prefix + matchPattern,
72
+ 'COUNT', String ( count ),
73
+ ];
74
+
75
+ return await client.sendCommand ( [ 'SCAN', ...commandArgs ] );
76
+ }
77
+ catch ( error ) {
78
+ console.log ( 'REDIS LIB ERROR [scan]', error );
79
+ throw error;
80
+ }
81
+ };
82
+
52
83
  /**
53
84
  * Retrieves the value of a key.
54
85
  * @param {string} key - The key to retrieve.