Skip to content

Commit

Permalink
Merge pull request #206 from sametcodes/205-implemeting-kv-database-t…
Browse files Browse the repository at this point in the history
…o-reduce-response-time

feat(#205) implemeting kv database to reduce response time
  • Loading branch information
sametcodes authored May 4, 2023
2 parents c224697 + 7359d54 commit 34546dd
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 39 deletions.
8 changes: 7 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ WAKATIME_SECRET = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
GITHUB_CLIENT_ID = xxxxxxxxxxxxxxxxxxxx
GITHUB_CLIENT_SECRET = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
GITHUB_CLIENT_INSTALL_URL = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
GITHUB_PUBLIC_CLIENT_PAT = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
GITHUB_PUBLIC_CLIENT_PAT = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# KV database on Vercel
KV_URL = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
KV_REST_API_URL = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
KV_REST_API_TOKEN = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
KV_REST_API_READ_ONLY_TOKEN = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
50 changes: 33 additions & 17 deletions middlewares/api/private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { sendFallbackResponse } from "@/services/api/response";

import { ValidationError } from "yup";
import { shapeDataAPISchema } from "@/services/data/validations";
import kv from "@vercel/kv";
import { PlatformQueryConfig } from "@prisma/client";

type CachedPrivateQuery =
| (PlatformQueryConfig & {
platformQuery: { name: string; cache_time: number };
platform: { name: string; code: string };
})
| null;

export const validatePrivateRequest = async (
req: NextApiRequest,
Expand All @@ -18,31 +27,38 @@ export const validatePrivateRequest = async (
message: "The configuration ID doesn't seem valid.",
});

const config = await prisma.platformQueryConfig.findFirst({
where: { id },
select: {
id: true,
userId: true,
queryConfig: true,
viewConfig: true,
platformQueryId: true,
platformId: true,
platformQuery: { select: { name: true, cache_time: true } },
platform: { select: { name: true, code: true } },
},
});
let queryConfig: CachedPrivateQuery;
const cachedConfig: CachedPrivateQuery = await kv.get(id);
if (cachedConfig) {
queryConfig = cachedConfig;
} else {
queryConfig = await prisma.platformQueryConfig.findFirst({
where: { id },
select: {
id: true,
userId: true,
queryConfig: true,
viewConfig: true,
platformQueryId: true,
platformId: true,
platformQuery: { select: { name: true, cache_time: true } },
platform: { select: { name: true, code: true } },
},
});
await kv.set(id, JSON.stringify(queryConfig));
}

if (!config)
if (!queryConfig)
return sendFallbackResponse(res, {
title: "Not found",
message: "The configuration does not exist, please check the URL.",
});

// @ts-ignore
res.locals = {};
res.locals.config = config;
res.locals.platform = config.platform;
res.locals.query = config.platformQuery;
res.locals.config = queryConfig;
res.locals.platform = queryConfig.platform;
res.locals.query = queryConfig.platformQuery;
return next();
};

Expand Down
38 changes: 17 additions & 21 deletions middlewares/api/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { sendFallbackResponse } from "@/services/api/response";

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

let cachedPlatformQueries: {
[key: string]: PlatformQuery & {
platform: Platform;
};
} = {};
type CachedPublicQuery = {
id: string;
name: string;
cache_time: number;
platformId: string;
platform: { name: string; code: string };
} | null;

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

let query: PlatformQuery & {
platform: Platform;
};
if (cachedPlatformQueries[id]) {
query = cachedPlatformQueries[id];
let query: CachedPublicQuery;
const cachedQuery: CachedPublicQuery = await kv.get(id);
if (cachedQuery) {
query = cachedQuery;
} else {
const platformQuery = await prisma.platformQuery.findFirst({
query = await prisma.platformQuery.findFirst({
where: { id },
select: {
id: true,
Expand All @@ -42,30 +43,25 @@ export const validatePublicRequest = async (
platform: { select: { name: true, code: true } },
},
});
if (!platformQuery)
if (!query)
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,
};
await kv.set(id, JSON.stringify(query));
}

const querystring = Object.keys(req.query)
.filter((key) => key !== "id")
.map((key) => `${key}=${req.query[key]}`)
.join("&");

const config = parseQueryString(querystring);
const paramsConfig = parseQueryString(querystring);

// @ts-ignore
res.locals = {};
res.locals.config = config;
res.locals.config = paramsConfig;
res.locals.query = query;
res.locals.platform = query.platform;
return next();
Expand Down
73 changes: 73 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@radix-ui/react-toast": "^1.1.3",
"@storybook/react": "^7.0.2",
"@vercel/analytics": "^0.1.11",
"@vercel/kv": "^0.1.1",
"class-variance-authority": "^0.5.2",
"clsx": "^1.2.1",
"image-size": "^1.0.2",
Expand Down

0 comments on commit 34546dd

Please sign in to comment.