5a350efb40
* feat: optimize app registry performance with memory-cache - Implement in-memory caching for getInstallCountPerApp with 5-minute TTL - Add memory-cache and @types/memory-cache dependencies to packages/lib - Remove invalidateInstallCountCache functionality as requested - Maintain existing API compatibility for app sorting functionality - Improve performance by avoiding expensive SQL COUNT queries on every request Co-Authored-By: anik@cal.com <adhabal2002@gmail.com> * update * Update getInstallCountPerApp.ts --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
37 lines
1004 B
TypeScript
37 lines
1004 B
TypeScript
import { unstable_cache } from "next/cache";
|
|
import { z } from "zod";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
const computeInstallCountsFromDB = async (): Promise<Record<string, number>> => {
|
|
const mostPopularApps = z.array(z.object({ appId: z.string(), installCount: z.number() })).parse(
|
|
await prisma.$queryRaw`
|
|
SELECT
|
|
c."appId",
|
|
COUNT(*)::integer AS "installCount"
|
|
FROM
|
|
"Credential" c
|
|
WHERE
|
|
c."appId" IS NOT NULL
|
|
GROUP BY
|
|
c."appId"
|
|
ORDER BY
|
|
"installCount" DESC
|
|
`
|
|
);
|
|
return mostPopularApps.reduce((acc, { appId, installCount }) => {
|
|
acc[appId] = installCount;
|
|
return acc;
|
|
}, {} as Record<string, number>);
|
|
};
|
|
|
|
const getInstallCountPerApp = async (): Promise<Record<string, number>> => {
|
|
return unstable_cache(async () => computeInstallCountsFromDB(), ["app-install-counts"], {
|
|
revalidate: 300,
|
|
tags: ["app-install-counts"],
|
|
})();
|
|
};
|
|
|
|
export default getInstallCountPerApp;
|
|
export { computeInstallCountsFromDB };
|