Iterate over all keys in KV when purging cache instead of just first page (#312)

* Iterate over all keys in KV when purging cache instead of just first key

* Call getTagPrefix once
This commit is contained in:
Samy Pessé
2024-03-20 17:48:17 +00:00
committed by GitHub
parent 10fc27ba4e
commit dc2d50e455
2 changed files with 28 additions and 23 deletions
+27 -22
View File
@@ -7,7 +7,6 @@ import { trace } from '../tracing';
const cacheVersion = 1;
interface KVTagMetadata {
key: string;
meta: CacheEntryMeta;
}
@@ -69,7 +68,6 @@ export const cloudflareKVCache: CacheBackend = {
if (entry.meta.tags.length > 0) {
const metadata: KVTagMetadata = {
key, // TODO: Remove this key from the metadata in 1 day
meta: entry.meta,
};
const jsonMetadata = JSON.stringify(metadata);
@@ -115,28 +113,35 @@ export const cloudflareKVCache: CacheBackend = {
const pendingDeletions: Array<Promise<unknown>> = [];
const iterateKVPage = async (prefix: string, cursor: string | null, max: number = 3) => {
const entries = await kv.list<KVTagMetadata>({
prefix,
cursor,
limit: 100,
});
for (const entry of entries.keys) {
if (entry.metadata) {
const metadata = entry.metadata;
const key = metadata.meta.key;
result.metas.push(metadata.meta);
result.keys.push(key);
// Delete the tag key and the value key
pendingDeletions.push(kv.delete(getValueKey(key)));
pendingDeletions.push(kv.delete(entry.name));
}
}
if (!entries.list_complete && max > 0) {
await iterateKVPage(prefix, entries.cursor, max - 1);
}
};
await Promise.all(
tags.map(async (tag) => {
const entries = await kv.list({
prefix: getTagPrefix(tag),
// We don't paginate at the moment and only get the first 100 keys.
limit: 100,
});
for (const entry of entries.keys) {
if (entry.metadata) {
const metadata = entry.metadata as KVTagMetadata;
const key = metadata.meta.key ?? metadata.key;
result.metas.push(metadata.meta);
result.keys.push(key);
// Delete the tag key and the value key
pendingDeletions.push(kv.delete(getValueKey(key)));
pendingDeletions.push(kv.delete(entry.name));
}
}
await iterateKVPage(getTagPrefix(tag), null);
}),
);
+1 -1
View File
@@ -1,5 +1,5 @@
export interface CacheEntryMeta {
key?: string;
key: string;
/**
* Timestamp when the entry was created.