Skip to content

Commit 909a4b8

Browse files
committed
refactor: remove old cache utility and update routes to use new cache implementation
1 parent 592684d commit 909a4b8

File tree

5 files changed

+19
-23
lines changed

5 files changed

+19
-23
lines changed

src/cache.ts

-1
This file was deleted.

src/routes/gateway_github.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import type { HonoContext } from "../types";
22
import { OpenAPIHono } from "@hono/zod-openapi";
3-
import { cache, createError } from "../utils";
3+
import { cache } from "hono/cache";
4+
import { createError } from "../utils";
45
import { GITHUB_EMOJIS_ROUTE } from "./gateway_github.openapi";
56

67
export const GATEWAY_GITHUB_ROUTER = new OpenAPIHono<HonoContext>().basePath("/api/gateway/github");
78

9+
GATEWAY_GITHUB_ROUTER.get("*", cache({
10+
cacheName: "github-emojis",
11+
cacheControl: "max-age=3600, immutable",
12+
}));
813
GATEWAY_GITHUB_ROUTER.openapi(GITHUB_EMOJIS_ROUTE, async (c) => {
914
const response = await fetch("https://api.github.com/emojis", {
1015
headers: {
@@ -24,7 +29,5 @@ GATEWAY_GITHUB_ROUTER.openapi(GITHUB_EMOJIS_ROUTE, async (c) => {
2429

2530
const emojis = await response.json<Record<string, string>>();
2631

27-
cache(c, 3600, true);
28-
2932
return c.json(emojis, 200);
3033
});

src/routes/random-emoji.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import type { HonoContext } from "../types";
22
import { Hono } from "hono";
3-
import { cache } from "../utils";
3+
import { cache } from "hono/cache";
44

55
export const RANDOM_EMOJI_ROUTER = new Hono<HonoContext>();
66

7+
RANDOM_EMOJI_ROUTER.get("/random-emoji.png", cache({
8+
cacheName: "random-emoji",
9+
cacheControl: "max-age=3600, immutable",
10+
}));
11+
712
RANDOM_EMOJI_ROUTER.get("/random-emoji.png", async (c) => {
8-
cache(c, 60 * 60);
913
return c.redirect("https://image.luxass.dev/api/image/emoji");
1014
});

src/routes/v1_categories.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import type { HonoContext } from "../types";
22
import { OpenAPIHono, z } from "@hono/zod-openapi";
3+
import { cache } from "hono/cache";
34
import { HTTPException } from "hono/http-exception";
45
import { versionMiddleware } from "../middlewares/version";
56
import { EmojiCategorySchema } from "../schemas";
6-
import { cache, createError } from "../utils";
7+
import { createError } from "../utils";
78
import { ALL_CATEGORIES_ROUTE, GET_CATEGORY_ROUTE } from "./v1_categories.openapi";
89

910
export const V1_CATEGORIES_ROUTER = new OpenAPIHono<HonoContext>().basePath("/api/v1/categories/:version");
1011

1112
V1_CATEGORIES_ROUTER.use(versionMiddleware);
1213

14+
V1_CATEGORIES_ROUTER.get("*", cache({
15+
cacheName: "v1-categories",
16+
cacheControl: "max-age=3600, immutable",
17+
}));
18+
1319
V1_CATEGORIES_ROUTER.openapi(ALL_CATEGORIES_ROUTE, async (c) => {
1420
const version = c.req.param("version");
1521

@@ -31,8 +37,6 @@ V1_CATEGORIES_ROUTER.openapi(ALL_CATEGORIES_ROUTE, async (c) => {
3137

3238
const categories = result.data;
3339

34-
cache(c, 3600, true);
35-
3640
return c.json(
3741
categories,
3842
200,
@@ -61,8 +65,6 @@ V1_CATEGORIES_ROUTER.openapi(GET_CATEGORY_ROUTE, async (c) => {
6165

6266
const category = result.data.find((c) => c.slug === categorySlug);
6367

64-
cache(c, 3600, true);
65-
6668
return c.json(
6769
category,
6870
200,

src/utils.ts

-12
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,3 @@ export async function getAvailableVersions(): Promise<EmojiLock["versions"]> {
3232

3333
return result.data.versions;
3434
}
35-
36-
export function cache<TCtx extends Context>(ctx: TCtx, age: number, immutable = false) {
37-
if (age === -1) {
38-
ctx.header("Expires", "0");
39-
ctx.header("Pragma", "no-cache");
40-
ctx.header("Cache-Control", "no-cache, no-store, must-revalidate");
41-
return;
42-
}
43-
44-
ctx.header("Expires", new Date(Date.now() + age * 1000).toUTCString());
45-
ctx.header("Cache-Control", ["public", `max-age=${age}`, immutable ? "immutable" : null].filter((x) => !!x).join(", "));
46-
};

0 commit comments

Comments
 (0)