Skip to content

Commit

Permalink
try to fix cache put
Browse files Browse the repository at this point in the history
  • Loading branch information
Razzmatazzz committed Aug 26, 2024
1 parent b730a26 commit cf110f0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 6 additions & 2 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async function graphqlHandler(request, env, ctx) {
let key;
// Check the cache service for data first - If cached data exists, return it
// we don't check the cache if we're the http server because the worker already did
if (env.SKIP_CACHE !== 'true' && !env.CLOUDFLARE_TOKEN) {
if (env.SKIP_CACHE !== 'true' && env.SKIP_CACHE_CHECK !== 'true' && !env.CLOUDFLARE_TOKEN) {
key = await cacheMachine.createKey(env, query, variables, specialCache);
const cachedResponse = await cacheMachine.get(env, {key});
if (cachedResponse) {
Expand Down Expand Up @@ -174,8 +174,12 @@ async function graphqlHandler(request, env, ctx) {
const response = new Response(body, responseOptions);

if (env.SKIP_CACHE !== 'true' && ttl > 0) {
key = key ?? await cacheMachine.createKey(env, query, variables, specialCache);
ctx.waitUntil(cacheMachine.put(env, body, {key}));
// using waitUntil doesn't hold up returning a response but keeps the worker alive as long as needed
ctx.waitUntil(cacheMachine.put(env, body, {key, query, variables, ttl, specialCache}));
/*const safePut = await cacheMachine.safePut(env, body, {key});
console.log(safePut);
ctx.waitUntil(safePut.fetchRequest);*/
}

return response;
Expand Down
5 changes: 3 additions & 2 deletions utils/cache-machine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ async function fetchWithTimeout(resource, options = {}) {
reject(new Error('The operation was aborted due to timeout'));
}, timeout);
fetch(resource, options).then(response => {
clearTimeout(requestTimeout);
resolve(response);
}).catch(reject);
}).catch(reject).finally(() => {
clearTimeout(requestTimeout);
});
});
}

Expand Down

0 comments on commit cf110f0

Please sign in to comment.