Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable KV cache for 5% of the cache tags #2490

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Disable KV cache for 5% of the cache tags
  • Loading branch information
SamyPesse committed Sep 25, 2024
commit b3e27d9c0938903620d7e4146cf9d1dade150a3d
21 changes: 18 additions & 3 deletions packages/gitbook/src/lib/cache/cloudflare-kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,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 @@ -31,7 +46,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
Loading