add cache for website id

This commit is contained in:
formica2 2021-03-24 14:48:20 +03:00
parent f8ac987bfc
commit 2f6e7786c6
3 changed files with 38 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import {
MYSQL_DATE_FORMATS,
POSTGRESQL_DATE_FORMATS,
URL_LENGTH,
WEBSITE_ID_CACHE_TIME,
} from 'lib/constants';
export function getDatabase() {
@ -92,6 +93,38 @@ export async function getWebsiteByUuid(website_uuid) {
);
}
/**
* Caching website ids for WEBSITE_ID_CACHE_TIME ms
*/
const cacheWebsiteIDByUuid = {};
export async function getWebsiteIDByUuidCached(website_uuid) {
if (cacheWebsiteIDByUuid[website_uuid] &&
(Date.now() - cacheWebsiteIDByUuid[website_uuid].timeout) <= WEBSITE_ID_CACHE_TIME)
return cacheWebsiteIDByUuid[website_uuid].website_id;
const result = await runQuery(
prisma.website.findUnique({
where: {
website_uuid,
},
select: {
website_id: true
}
}),
);
if (!result) return result;
const { website_id } = result;
if (website_id) cacheWebsiteIDByUuid[website_uuid] = {
website_id,
timeout: Date.now()
}
return website_id;
}
export async function getWebsiteByShareId(share_id) {
return runQuery(
prisma.website.findUnique({