From 558cc295522acb91a4277f02f38b5d57c387a9c4 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 4 May 2023 10:33:54 +0100 Subject: [PATCH 1/2] fix(redis): support `getKeys` and `clear` with base --- src/drivers/redis.ts | 12 ++++++------ src/drivers/utils/index.ts | 4 ++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/drivers/redis.ts b/src/drivers/redis.ts index f7819856..777eea17 100644 --- a/src/drivers/redis.ts +++ b/src/drivers/redis.ts @@ -1,4 +1,4 @@ -import { defineDriver } from "./utils"; +import { defineDriver, joinKeys } from "./utils"; import Redis, { Cluster, ClusterNode, @@ -50,7 +50,7 @@ export default defineDriver((opts: RedisOptions = {}) => { }; const base = (opts.base || "").replace(/:$/, ""); - const p = (key: string) => (base ? `${base}:${key}` : key); // Prefix a key. Uses base for backwards compatibility + const p = (...keys: string[]) => joinKeys(base, ...keys); // Prefix a key. Uses base for backwards compatibility const d = (key: string) => (base ? key.replace(base, "") : key); // Deprefix a key return { @@ -74,12 +74,12 @@ export default defineDriver((opts: RedisOptions = {}) => { async removeItem(key) { await getRedisClient().del(p(key)); }, - async getKeys() { - const keys: string[] = await getRedisClient().keys(p("*")); + async getKeys(base) { + const keys: string[] = await getRedisClient().keys(p(base, "*")); return keys.map((key) => d(key)); }, - async clear() { - const keys = await getRedisClient().keys(p("*")); + async clear(base) { + const keys = await getRedisClient().keys(p(base, "*")); if (keys.length === 0) { return; } diff --git a/src/drivers/utils/index.ts b/src/drivers/utils/index.ts index 9c3ec07c..a9bf3396 100644 --- a/src/drivers/utils/index.ts +++ b/src/drivers/utils/index.ts @@ -14,3 +14,7 @@ export function normalizeKey(key: string | undefined): string { } return key.replace(/[/\\]/g, ":").replace(/^:|:$/g, ""); } + +export function joinKeys(...keys: string[]) { + return normalizeKey(keys.filter(Boolean).join(":")); +} From ef9467e4219146c1c0e30b1ab3ba8c9ddeb5a15c Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 4 May 2023 11:30:15 +0100 Subject: [PATCH 2/2] fix: normalise each key --- src/drivers/utils/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/drivers/utils/index.ts b/src/drivers/utils/index.ts index a9bf3396..1a8b067f 100644 --- a/src/drivers/utils/index.ts +++ b/src/drivers/utils/index.ts @@ -16,5 +16,5 @@ export function normalizeKey(key: string | undefined): string { } export function joinKeys(...keys: string[]) { - return normalizeKey(keys.filter(Boolean).join(":")); + return keys.map(normalizeKey).filter(Boolean).join(":"); }