diff options
| author | Factiven <[email protected]> | 2023-12-24 13:03:54 +0700 |
|---|---|---|
| committer | Factiven <[email protected]> | 2023-12-24 13:03:54 +0700 |
| commit | 50a0f0240d7fef133eb5acc1bea2b1168b08e9db (patch) | |
| tree | 307e09e505580415a58d64b5fc3580e9235869f1 /utils/getRedisWithPrefix.ts | |
| parent | Update README.md (#104) (diff) | |
| download | moopa-50a0f0240d7fef133eb5acc1bea2b1168b08e9db.tar.xz moopa-50a0f0240d7fef133eb5acc1bea2b1168b08e9db.zip | |
migrate to typescript
Diffstat (limited to 'utils/getRedisWithPrefix.ts')
| -rw-r--r-- | utils/getRedisWithPrefix.ts | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/utils/getRedisWithPrefix.ts b/utils/getRedisWithPrefix.ts new file mode 100644 index 0000000..dacf78e --- /dev/null +++ b/utils/getRedisWithPrefix.ts @@ -0,0 +1,86 @@ +import { redis } from "@/lib/redis"; + +export async function getValuesWithPrefix(prefix: string) { + let cursor = "0"; // Start at the beginning of the keyspace + let values = []; + + do { + const [newCursor, matchingKeys] = await redis.scan( + cursor, + "MATCH", + prefix + "*", + "COUNT", + 100 + ); + + // Retrieve values for matching keys and add them to the array + for (const key of matchingKeys) { + const value = await redis.get(key); + if (value !== null) { + values.push(JSON.parse(value)); + } + } + + // Update the cursor for the next iteration + cursor = newCursor; + } while (cursor !== "0"); // Continue until the cursor is '0' + + return values; +} + +export async function countKeysWithPrefix(prefix: string) { + let cursor = "0"; // Start at the beginning of the keyspace + let count = 0; + + do { + const [newCursor, matchingKeys] = await redis.scan( + cursor, + "MATCH", + prefix + "*", + "COUNT", + 100 + ); + + // Increment the count by the number of matching keys in this iteration + count += matchingKeys.length; + + // Update the cursor for the next iteration + cursor = newCursor; + } while (cursor !== "0"); // Continue until the cursor is '0' + + return count; +} + +export async function getValuesWithNumericKeys() { + const allKeys = await redis.keys("*"); // Fetch all keys in Redis + const numericKeys = allKeys.filter((key) => /^\d+$/.test(key)); // Filter keys that contain only numbers + + const values = []; + + for (const key of numericKeys) { + const value = await redis.get(key); // Retrieve the value for each numeric key + values.push(value); + } + + return values; +} + +export async function getKeysWithNumericKeys() { + const allKeys = await redis.keys("*"); // Fetch all keys in Redis + const numericKeys = allKeys.filter((key) => /^\d+$/.test(key)); // Filter keys that contain only numbers + + const values: any[] = []; + + for (const key of numericKeys) { + await redis.del(key); + } + + return values; +} + +export async function countNumericKeys() { + const allKeys = await redis.keys("*"); // Fetch all keys in Redis + const numericKeys = allKeys.filter((key) => /^\d+$/.test(key)); // Filter keys that contain only numbers + + return numericKeys.length; // Return the count of numeric keys +} |