1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-11-14 19:20:50 -05:00

move database from postgres/prisma to redis

This commit is contained in:
2025-03-27 09:21:22 -04:00
parent e865d9d8e5
commit bbf6e9dc66
16 changed files with 283 additions and 651 deletions

View File

@@ -1,31 +1,43 @@
import { NextResponse } from "next/server";
import { prisma } from "../../../lib/helpers/prisma";
import type { hits as Hits } from "@prisma/client";
import redis from "../../../lib/helpers/redis";
export const revalidate = 900; // 15 mins
export const GET = async (): Promise<
NextResponse<{
total: Pick<Hits, "hits">;
pages: Hits[];
total: {
hits: number;
};
pages: Array<{
slug: string;
hits: number;
}>;
}>
> => {
// fetch all rows from db sorted by most hits
const pages = await prisma.hits.findMany({
orderBy: [
{
hits: "desc",
},
],
const slugs = await redis.scan(0, {
count: 50,
});
const total = { hits: 0 };
// fetch all rows from db sorted by most hits
const data = await Promise.all(
slugs[1].map(async (slug) => {
const hits = (await redis.get(slug)) as number;
return {
slug,
hits,
};
})
);
// sort by hits
data.sort((a, b) => b.hits - a.hits);
// calculate total hits
pages.forEach((page) => {
const total = { hits: 0 };
data.forEach((page) => {
// add these hits to running tally
total.hits += page.hits;
});
return NextResponse.json({ total, pages });
return NextResponse.json({ total, pages: data });
};