mirror of
https://github.com/umami-software/umami.git
synced 2026-02-20 12:35:38 +01:00
add cache for website id
This commit is contained in:
parent
f8ac987bfc
commit
2f6e7786c6
3 changed files with 38 additions and 4 deletions
|
|
@ -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({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue