Skip to content

Commit

Permalink
Disable KV cache for 5% of the cache tags (#2490)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse authored Sep 26, 2024
1 parent 9b8d519 commit 75f825e
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions packages/gitbook/src/lib/cache/cloudflare-kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 75f825e

Please sign in to comment.