Skip to content
Draft
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
37 changes: 37 additions & 0 deletions packages/backend/src/middleware/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { styleText } from "node:util";
import type { NextFunction, Request, RequestHandler, Response } from "express";
import Cache from "@/app/Cache";

export const middlewareCache = new Cache();

function log(hit: "hit" | "miss", cacheKey: string) {
console.debug(
"Cache",
styleText(["bold", "green"], hit.toUpperCase()),
"for",
styleText(["italic"], cacheKey),
);
}

export default function cacheGets(staleTimeMs?: number): RequestHandler {
return (req: Request, res: Response, next: NextFunction) => {
if (req.method.toUpperCase() !== "GET") next();

const key = req.originalUrl || req.url;

if (middlewareCache.has(key)) {
log("hit", key);
return res.json(middlewareCache.get(key));
}

log("miss", key);

const originalJson = res.json.bind(res);
res.json = (data) => {
middlewareCache.set(key, data, staleTimeMs);
return originalJson(data);
};

next();
};
}
2 changes: 2 additions & 0 deletions packages/backend/src/routes/api/v1/history.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Point } from "@blurple-canvas-web/types";
import { Router } from "express";
import cacheGets from "@/middleware/cache";
import {
assertLoggedIn,
requireCanvasModerator,
Expand All @@ -23,6 +24,7 @@ export const historyRouter = typedRouter(Router({ mergeParams: true }));
historyRouter.get(
"/",
validate({ params: CanvasIdParamModel, query: PixelHistoryParamModel }),
cacheGets(),
async (req, res) => {
const startedAt = performance.now();
const pixelHistory = await getPixelHistorySummary(
Expand Down
9 changes: 5 additions & 4 deletions packages/backend/src/routes/api/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Router } from "express";
import cacheGets from "@/middleware/cache";
import { blocklistRouter } from "./blocklist";
import { canvasRouter } from "./canvas";
import { discordRouter } from "./discord";
Expand All @@ -14,7 +15,7 @@ apiV1Router.use("/blocklist", blocklistRouter);
apiV1Router.use("/canvas", canvasRouter);
apiV1Router.use("/discord", discordRouter);
apiV1Router.use("/event", eventRouter);
apiV1Router.use("/frame", frameRouter);
apiV1Router.use("/notice", noticeRouter);
apiV1Router.use("/palette", paletteRouter);
apiV1Router.use("/statistics", statisticsRouter);
apiV1Router.use("/frame", frameRouter, cacheGets(300_000));
apiV1Router.use("/notice", noticeRouter, cacheGets(300_000));
apiV1Router.use("/palette", paletteRouter, cacheGets(600_000));
apiV1Router.use("/statistics", statisticsRouter, cacheGets(300_000));