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

fix: stricter requirements for cache #178

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions packages/edge-gateway/src/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// https://developers.cloudflare.com/cache/concepts/default-cache-behavior/#cacheable-size-limits
export const CF_CACHE_MAX_OBJECT_SIZE = 512 * Math.pow(1024, 2) // 512MB to bytes

export const ACCEPTABLE_DENYLIST_STATUS_CODES = [204, 400, 404]
Expand Down
26 changes: 16 additions & 10 deletions packages/edge-gateway/src/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ export async function gatewayGet (request, env, ctx) {
// 2nd layer
const dotstorageRes = await getFromDotstorage(request, env, cid, { pathname, search })
if (dotstorageRes) {
ctx.waitUntil(putToCache(request, dotstorageRes.response, cache))
if (isCachable(request, dotstorageRes.response)) {
ctx.waitUntil(cache.put(request, dotstorageRes.response.clone()))
}

return getResponseWithCustomHeaders(
dotstorageRes.response,
Expand Down Expand Up @@ -151,8 +153,8 @@ export async function gatewayGet (request, env, ctx) {
}

// Cache response
if (winnerGwResponse && winnerGwResponse.ok) {
ctx.waitUntil(putToCache(request, winnerGwResponse, cache))
if (winnerGwResponse && isCachable(request, winnerGwResponse)) {
ctx.waitUntil(cache.put(request, winnerGwResponse.clone()))
}

return getResponseWithCustomHeaders(
Expand Down Expand Up @@ -391,19 +393,23 @@ async function getFromPermaCache (request, env) {
}

/**
* Put receives response to cache.
* Determine if a response is cachable in the Cloudflare cache.
*
* @param {Request} request
* @param {Response} response
* @param {Cache} cache
*/
async function putToCache (request, response, cache) {
const contentLengthMb = Number(response.headers.get('content-length'))
function isCachable (request, response) {
// https://developers.cloudflare.com/workers/runtime-apis/cache/#invalid-parameters
if (!response.ok || request.method !== 'GET' || response.status === 206 || response.headers.get('Vary') === '*') {
return false
}

// Cache request in Cloudflare CDN if smaller than CF_CACHE_MAX_OBJECT_SIZE
if (contentLengthMb <= CF_CACHE_MAX_OBJECT_SIZE) {
await cache.put(request, response.clone())
const contentLength = parseInt(response.headers.get('content-length') ?? '')
if (isNaN(contentLength) || contentLength > CF_CACHE_MAX_OBJECT_SIZE) {
return false
}

return true
}

/**
Expand Down
Loading