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

Switch from _ query param to Cache-Control header for cache busting #61

Merged
merged 2 commits into from
May 3, 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
5 changes: 3 additions & 2 deletions src/route/wcl/character-parses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FastifyInstance } from "fastify";
import axios from "axios";
import * as cache from "../../cache";
import * as Sentry from "@sentry/node";
import { shouldSkipCache } from "./common";

// FIXME we cheat on the character parses due to significant changes to the
// response from WCL. specifically: it is not currently possible to get all
Expand All @@ -14,11 +15,11 @@ import * as Sentry from "@sentry/node";
// change, we just use the v1 API for this call.

type Query = {
_?: string;
includeCombatantInfo?: string;
metric: string;
zone: string;
timeframe?: string;
partition?: string;
game?: "classic" | "retail";
};

Expand Down Expand Up @@ -49,7 +50,7 @@ const characterParses = (app: FastifyInstance) => {
};
let data;
const cacheKey = `character-parses-${req.params.region}-${req.params.server}-${req.params.name}`;
if (!req.query._) {
if (!shouldSkipCache(req)) {
data = await cache.remember(cacheKey, thunk);
} else {
data = await thunk();
Expand Down
15 changes: 13 additions & 2 deletions src/route/wcl/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function decompress(data: string): Promise<unknown> {
* Wrap a request to WCL in a caching & compression layer.
*/
export function wrapEndpoint<
Q extends { translate?: string; _?: string },
Q extends { translate?: string },
P = ReportParams,
>(
url: string,
Expand All @@ -69,7 +69,7 @@ export function wrapEndpoint<
)}-${await queryKey(req.query as Q)}`;

try {
if ((req.query as Q)._) {
if (shouldSkipCache(req)) {
const data = await thunk(req);
if (data) {
cache.set(cacheKey, data, timeout).catch(Sentry.captureException);
Expand Down Expand Up @@ -103,3 +103,14 @@ export function wrapEndpoint<
}
});
}

export function shouldSkipCache(req: FastifyRequest): boolean {
const cacheControl = req.headers["cache-control"];
if (!cacheControl) {
return false;
}

return (
cacheControl.includes("no-cache") || cacheControl.includes("max-age=0")
);
}
Loading