perf: cache online API endpoints with Redis-first cache
- Upgrade lib/cache.ts: Redis-first cached() with in-memory fallback (was in-memory only, broken across PM2 instances) - Cache /api/online user list (10s TTL, was uncached per-request) eliminates DB query on every poll request - Add uncached() invalidation helper for write-after-cache patterns - 0 tsc errors, 583 tests passing
This commit is contained in:
1 parent
ef5e706ee1
commit
d1807ca814
2 files changed
+64
-14
No files matched your search
@@ -5,17 +5,23 @@
|
||||
// DB failure.
|
||||
|
||||
import { apiJson } from "@/lib/api";
|
||||
import { cached } from "@/lib/cache";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(_req: Request) {
|
||||
try {
|
||||
const users = await prisma.user.findMany({
|
||||
where: { online: "1" },
|
||||
select: { username: true, look: true },
|
||||
take: 100,
|
||||
});
|
||||
const users = await cached(
|
||||
"online_users",
|
||||
10_000,
|
||||
async () =>
|
||||
prisma.user.findMany({
|
||||
where: { online: "1" },
|
||||
select: { username: true, look: true },
|
||||
take: 100,
|
||||
}),
|
||||
);
|
||||
|
||||
return apiJson({ users });
|
||||
} catch {
|
||||
|
||||
Reference in new issue
Block a user