Gitea Actions Runner Test / test-job (push) Successful in 0s
CI / check (push) Successful in 30s
CI / tests-integration (push) Successful in 1m38s
CI / tests-unit (push) Successful in 1m43s
CI / tests-ui (push) Successful in 2m30s
CI / preflight (push) Skipped
CI / deploy (push) Successful in 2m7s
Three separate things that were each costing more than they needed to on the hot path. - Single-flight avatar renders. The disk cache was checked first and a miss went straight to the upstream, with nothing shared between callers, so a page requesting dozens of avatars at once turned N concurrent requests for one figure into N renders. A render is the most expensive operation this app does, and the duplication happened exactly when the cache had nothing to offer. Eight concurrent requests now cause one render instead of eight. The map lives on globalThis because Next can evaluate the module more than once per process, and two copies would each start their own render. - Let public read-only routes be cached by a shared cache. Every JSON response was `cache-control: no-store`, so a CDN in front of the app could not answer any of it and every request reached the origin. publicCacheControl() opts a route in with s-maxage and stale-while-revalidate, using the same TTL as the server-side cache so the two layers cannot disagree. The default stays no-store: most routes here are personalised, admin-only or auth-dependent. /api/badges/leaderboard is deliberately left alone because it returns per-viewer rank entries to signed-in callers. Note this only takes effect once a cache rule exists for /api/* at the CDN, or the explicit `cache: "no-store"` is dropped from the client fetches (24 files do that today, including the /api/online poll). The headers alone are inert until one of those happens. - Take the homepage row counts from the storage engine estimate instead of COUNT(*), which walks an index and gets slower as the tables grow. A missing or zero estimate falls back to the exact count rather than ever showing a wrong zero. The online count stays exact: it is an indexed read over a small subset and a few seconds of drift reads as broken rather than approximate. The counters move into one module because the homepage and the boot warm-up populate the same cache keys, so two implementations would race to write different values into the same entry. 3223 tests pass.
94 lines
2.7 KiB
TypeScript
94 lines
2.7 KiB
TypeScript
// Public REST: a single rare furni trade value by id (website_rare_values),
|
|
// together with its parent category. AtomCMS JSON API parity. Returns { error }
|
|
// (404) when the id is unknown.
|
|
import { eq } from "drizzle-orm";
|
|
import { apiJson, publicCacheControl } from "@/lib/api";
|
|
import { db, WebsiteRareValueCategories, WebsiteRareValues } from "@/lib/db";
|
|
import { apiCacheKey, cacheSafe, redisCache } from "@/lib/redis-cache";
|
|
|
|
export async function GET(
|
|
_req: Request,
|
|
{ params }: { params: Promise<{ id: string }> },
|
|
) {
|
|
const { id } = await params;
|
|
|
|
// The primary key is a BigInt; reject non-numeric ids up front.
|
|
if (!/^\d+$/.test(id)) {
|
|
return apiJson({ error: "Rare value not found" }, { status: 404 });
|
|
}
|
|
|
|
let valueId: bigint;
|
|
try {
|
|
valueId = BigInt(id);
|
|
} catch {
|
|
return apiJson({ error: "Rare value not found" }, { status: 404 });
|
|
}
|
|
|
|
try {
|
|
const data = await redisCache(
|
|
apiCacheKey(`values:${id}`),
|
|
300,
|
|
async () => {
|
|
const [value] = await db
|
|
.select({
|
|
id: WebsiteRareValues.id,
|
|
categoryId: WebsiteRareValues.categoryId,
|
|
itemId: WebsiteRareValues.itemId,
|
|
name: WebsiteRareValues.name,
|
|
creditValue: WebsiteRareValues.creditValue,
|
|
currencyValue: WebsiteRareValues.currencyValue,
|
|
currencyType: WebsiteRareValues.currencyType,
|
|
furnitureIcon: WebsiteRareValues.furnitureIcon,
|
|
createdAt: WebsiteRareValues.createdAt,
|
|
updatedAt: WebsiteRareValues.updatedAt,
|
|
})
|
|
.from(WebsiteRareValues)
|
|
.where(eq(WebsiteRareValues.id, valueId))
|
|
.limit(1);
|
|
|
|
if (!value) {
|
|
return null;
|
|
}
|
|
|
|
// No relation is modelled between the value and its category, so resolve the
|
|
// category in a second guarded query.
|
|
let category: {
|
|
id: bigint;
|
|
name: string;
|
|
badge: string;
|
|
priority: number;
|
|
} | null = null;
|
|
try {
|
|
const [row] = await db
|
|
.select({
|
|
id: WebsiteRareValueCategories.id,
|
|
name: WebsiteRareValueCategories.name,
|
|
badge: WebsiteRareValueCategories.badge,
|
|
priority: WebsiteRareValueCategories.priority,
|
|
})
|
|
.from(WebsiteRareValueCategories)
|
|
.where(eq(WebsiteRareValueCategories.id, value.categoryId))
|
|
.limit(1);
|
|
category = row ?? null;
|
|
} catch {
|
|
category = null;
|
|
}
|
|
|
|
return cacheSafe({ data: { ...value, category } });
|
|
},
|
|
{ staleMs: 300_000 },
|
|
);
|
|
|
|
if (data === null) {
|
|
return apiJson({ error: "Rare value not found" }, { status: 404 });
|
|
}
|
|
|
|
return apiJson(data, {
|
|
headers: { "cache-control": publicCacheControl(300) },
|
|
});
|
|
} catch {
|
|
// DB unavailable — treat as not found rather than a 500.
|
|
return apiJson({ error: "Rare value not found" }, { status: 200 });
|
|
}
|
|
}
|