Skip to content

Commit adec08d

Browse files
committed
feat: cache /api/item endpoint
1 parent 6490191 commit adec08d

4 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/lib/server/constants/update-collections.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export async function updateCollectionConstants() {
99

1010
const collections = await MONGO.collection("collections").findOne({});
1111
if (collections?.collections == null) {
12-
console.log(collections);
1312
return;
1413
}
1514

src/lib/server/helper/renderer.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ Modified and Improved by @DuckySoLucky
1010
export const CACHE_PATH = helper.getCacheFolderPath();
1111

1212
import { base } from "$app/paths";
13+
import { REDIS } from "$lib/server/db/redis";
14+
import type { ItemQuery } from "$types/global";
1315
import { createCanvas, loadImage } from "@napi-rs/canvas";
1416
import fs from "fs-extra";
17+
import minecraftData from "minecraft-data";
1518
import sanitize from "mongo-sanitize";
1619
import path from "path";
17-
18-
import type { ItemQuery } from "$types/global";
19-
import minecraftData from "minecraft-data";
2020
import * as helper from "../helper";
2121
import { getItemData } from "./item";
2222
const mcData = minecraftData("1.8.9");
@@ -443,6 +443,12 @@ async function renderPotion(type: string, color: string) {
443443
* @returns Image of an item
444444
*/
445445
export async function renderItem(skyblockId: string | undefined, query: ItemQuery): Promise<RenderItemOutput> {
446+
const cacheId = `ITEM:${skyblockId}:${JSON.stringify(query)}}`;
447+
const cache = await REDIS.get(cacheId);
448+
if (cache) {
449+
return JSON.parse(cache);
450+
}
451+
446452
query = sanitize(query);
447453
let itemQuery = query ?? {};
448454

@@ -501,5 +507,8 @@ export async function renderItem(skyblockId: string | undefined, query: ItemQuer
501507
console.error("Error processing static image:", error);
502508
}
503509
}
510+
511+
REDIS.SETEX(cacheId, 60 * 30, JSON.stringify(outputTexture));
512+
504513
return outputTexture;
505514
}

src/lib/server/stats.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ async function processStats<T>(player: Player, profile: Profile, stats: Array<[s
3131
}
3232

3333
export async function getStats(profile: Profile, player: Player, extra: { museum?: MuseumRawResponse; packs?: string[] } = {}) {
34-
const timeNow = Date.now();
3534
const cacheId = `STATS:${profile.uuid}:${profile.profile_id}:${extra.packs?.join(",") ?? "NONE"}`;
3635
const cache = await REDIS.get(cacheId);
3736
if (cache && !dev) {
38-
console.log(`[CACHE] Found cache for ${profile.uuid}:${profile.profile_id} in ${Date.now() - timeNow}ms`);
3937
return JSON.parse(cache);
4038
}
4139

src/routes/api/item/[id=itemId]/+server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export const GET: RequestHandler = async ({ params, cookies, url }) => {
1515
static: isStatic
1616
});
1717

18-
return new Response(attachment.image, {
18+
const imageBuffer = Buffer.isBuffer(attachment.image) ? attachment.image : Buffer.from(attachment.image.data);
19+
return new Response(imageBuffer, {
1920
headers: {
2021
"Content-Type": "image/png"
2122
}

0 commit comments

Comments
 (0)