Skip to content

Commit

Permalink
Merge pull request #200 from sametcodes/develop
Browse files Browse the repository at this point in the history
Extended caching mechanism
  • Loading branch information
sametcodes authored Apr 29, 2023
2 parents 0e66b43 + 18b2978 commit 3ed7177
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions middlewares/api/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import { sendFallbackResponse } from "@/services/api/response";

import { ValidationError } from "yup";
import { shapeDataAPISchema } from "@/services/data/validations";
import { Platform, PlatformQuery } from "@prisma/client";

let cachedPlatformQueries: {
[key: string]: PlatformQuery & {
platform: Platform;
};
} = {};

export const validatePublicRequest = async (
req: NextApiRequest,
Expand All @@ -19,22 +26,35 @@ export const validatePublicRequest = async (
message: "The configuration ID doesn't seem valid.",
});

const query = await prisma.platformQuery.findFirst({
where: { id },
select: {
id: true,
name: true,
cache_time: true,
platformId: true,
platform: { select: { name: true, code: true } },
},
});

if (!query)
return sendFallbackResponse(res, {
title: "Not found",
message: "The configuration does not exist, please check the URL.",
let query: PlatformQuery & {
platform: Platform;
};
if (cachedPlatformQueries[id]) {
query = cachedPlatformQueries[id];
} else {
const platformQuery = await prisma.platformQuery.findFirst({
where: { id },
select: {
id: true,
name: true,
cache_time: true,
platformId: true,
platform: { select: { name: true, code: true } },
},
});
if (!platformQuery)
return sendFallbackResponse(res, {
title: "Not found",
message: "The configuration does not exist, please check the URL.",
});

query = platformQuery as PlatformQuery & {
platform: Platform;
};
cachedPlatformQueries = {
[id]: query,
};
}

const querystring = Object.keys(req.query)
.filter((key) => key !== "id")
Expand Down

1 comment on commit 3ed7177

@vercel
Copy link

@vercel vercel bot commented on 3ed7177 Apr 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

readmerocks – ./

www.readme.rocks
readme.rocks
readmerocks-sametcodes.vercel.app
readmerocks-git-main-sametcodes.vercel.app

Please sign in to comment.