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

Don't cache responses with errors #94

Merged
merged 4 commits into from
Aug 20, 2022
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
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async function graphqlHandler(event, graphQLOptions) {

// Check the cache service for data first - If cached data exists, return it
if (!skipCache) {
const cachedResponse = await cacheMachine.get(query);
const cachedResponse = await cacheMachine.get(query, variables);
if (cachedResponse) {
// Construct a new response with the cached data
const newResponse = new Response(cachedResponse, headers);
Expand Down Expand Up @@ -170,9 +170,10 @@ async function graphqlHandler(event, graphQLOptions) {
const body = JSON.stringify(result);

// Update the cache with the results of the query
if (!skipCache) {
// don't update cache if result contained errors
if (!skipCache && (!result.errors || result.errors.length === 0)) {
// using waitUntil doens't hold up returning a response but keeps the worker alive as long as needed
event.waitUntil(cacheMachine.put(query, body));
event.waitUntil(cacheMachine.put(query, variables, body));
}

/* if(!result.errors && !url.hostname.includes('localhost') && !url.hostname.includes('tutorial.cloudflareworkers.com')){
Expand Down
1 change: 1 addition & 0 deletions schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ type HealthEffect {
type HideoutStation {
id: ID!
name: String!
normalizedName: String!
levels: [HideoutStationLevel]!
tarkovDataId: Int
"crafts is only available via the hideoutStations query."
Expand Down
8 changes: 4 additions & 4 deletions utils/cache-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ async function hash(string) {
// :param json: the incoming request in json
// :param body: the body to cache
// :return: true if successful, false if not
async function updateCache(query, body) {
async function updateCache(query, variables, body) {
try {
// Get the cacheKey from the request
query = query.trim();
const cacheKey = await hash(query);
const cacheKey = await hash(query+JSON.stringify(variables));

// headers and POST body
const headersPost = {
Expand Down Expand Up @@ -55,10 +55,10 @@ async function updateCache(query, body) {
// Checks the caching service to see if a request has been cached
// :param json: the json payload of the incoming worker request
// :return: json results of the item found in the cache or false if not found
async function checkCache(query) {
async function checkCache(query, variables) {
try {
query = query.trim();
const cacheKey = await hash(query);
const cacheKey = await hash(query+JSON.stringify(variables));

const response = await fetch(`${cacheUrl}/api/cache?key=${cacheKey}`, {headers: headers});
if (response.status === 200) {
Expand Down