Merged in fix/delete-fuzzy (pull request #1538)

Support for delete keys fuzzy

* Support for delete keys fuzzy

* Added some logs


Approved-by: Anton Gunnarsson
This commit is contained in:
Linus Flood
2025-03-14 12:50:34 +00:00
parent c0b543f18d
commit f954deaf22

View File

@@ -56,13 +56,14 @@ export const cacheRoutes = new Elysia({ prefix: "/cache" })
"/",
async ({ query: { key, fuzzy }, set }) => {
key = validateKey(key);
console.log("DELETE /cache", key);
console.log("DELETE /cache", key, { fuzzy });
if (fuzzy) {
key = `*${key}*`;
}
await deleteWithPattern(`*${key}*`);
} else {
await redis.del(key);
console.log("Deleted key: ", key);
}
set.status = 204;
return;
@@ -80,9 +81,7 @@ function validateKey(key: string) {
const parsedKey = decodeURIComponent(key);
if (parsedKey.length < MIN_LENGTH) {
throw new ModelValidationError(
"Key has to be atleast 1 character long"
);
throw new ModelValidationError("Key has to be atleast 1 character long");
}
if (parsedKey.includes("*")) {
@@ -91,3 +90,25 @@ function validateKey(key: string) {
return parsedKey;
}
async function deleteWithPattern(pattern: string) {
let cursor = "0";
let keys: string[] = [];
do {
const [newCursor, foundKeys] = await redis.scan(
cursor,
"MATCH",
pattern,
"COUNT",
5000
);
cursor = newCursor;
keys.push(...foundKeys);
} while (cursor !== "0");
if (keys.length > 0) {
await redis.del(...keys);
}
console.log("Deleted number of keys: ", keys.length);
}