From 75f825e4f893941768127be25a824e5d4ea0ff5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Thu, 26 Sep 2024 10:23:25 +0200 Subject: [PATCH] Disable KV cache for 5% of the cache tags (#2490) --- .../gitbook/src/lib/cache/cloudflare-kv.ts | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/gitbook/src/lib/cache/cloudflare-kv.ts b/packages/gitbook/src/lib/cache/cloudflare-kv.ts index 5ce5d33812..3f8acbada2 100644 --- a/packages/gitbook/src/lib/cache/cloudflare-kv.ts +++ b/packages/gitbook/src/lib/cache/cloudflare-kv.ts @@ -20,8 +20,23 @@ const noKVTags = new Set([ 'space:NkEGS7hzeqa35sMXQZ4X', ]); -function shouldUseItForTag(tag: string): boolean { - return !noKVTags.has(tag) && !tag.startsWith('change-request:'); +function shouldUseKVForTag(tag: string): boolean { + if (noKVTags.has(tag)) { + return false; + } + if (tag.startsWith('change-request:')) { + return false; + } + + // Hash the tag and return true for 95% of the tags + const hash = tag.split('').reduce((acc, char) => { + return acc + char.charCodeAt(0); + }, 0); + if (hash % 100 <= 95) { + return true; + } + + return false; } /** @@ -32,7 +47,7 @@ export const cloudflareKVCache: CacheBackend = { name: 'cloudflare-kv', replication: 'global', async get({ key, tag }, options) { - if (tag && !shouldUseItForTag(tag)) { + if (tag && !shouldUseKVForTag(tag)) { return null; } @@ -61,7 +76,7 @@ export const cloudflareKVCache: CacheBackend = { ); }, async set(entry) { - if (entry.meta.tag && !shouldUseItForTag(entry.meta.tag)) { + if (entry.meta.tag && !shouldUseKVForTag(entry.meta.tag)) { return; }