Skip to content

Commit

Permalink
add cron to flush objekt stats cache every hour
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekz committed Feb 24, 2025
1 parent afa0ab3 commit d7b7c08
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/app/api/cron/objekt-stats/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { env } from "@/env";
import { revalidateTag } from "next/cache";

/**
* Flush the objekt stats cache.
*/
export function GET(req: Request) {
const authHeader = req.headers.get("authorization");
if (authHeader !== `Bearer ${env.CRON_SECRET}`) {
return new Response("unauthorized", {
status: 401,
});
}

// flush the cache tag
revalidateTag("objekt-stats");

return new Response("ok");
}
4 changes: 2 additions & 2 deletions src/lib/server/objekts/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ async function fetchObjektStats(): Promise<ObjektStats> {

/**
* Get the stats for the fixed 24-hour window.
* Cached for 1 hour.
* Cached for 2 hours, but a cron job flushes the cache every hour.
*/
export const getObjektStats = unstable_cache(
fetchObjektStats,
["objekt-stats"],
{
revalidate: 60 * 60, // 1 hour
revalidate: 60 * 60 * 2, // 2 hours
}
);
4 changes: 4 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
{
"path": "/api/cron/gravity",
"schedule": "*/30 * * * *"
},
{
"path": "/api/cron/objekt-stats",
"schedule": "0 * * * *"
}
]
}

0 comments on commit d7b7c08

Please sign in to comment.