Skip to content

Commit

Permalink
Merge pull request #61 from WoWAnalyzer/cache-busting
Browse files Browse the repository at this point in the history
Switch from _ query param to `Cache-Control` header for cache busting
  • Loading branch information
emallson authored May 3, 2024
2 parents 6c61409 + dac8221 commit 6aafa94
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
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")
);
}

0 comments on commit 6aafa94

Please sign in to comment.