refactor: add cache layer

This commit is contained in:
bholmesdev 2023-04-27 12:34:30 -04:00
parent 7f77913b9f
commit 1c3bfdc6b3
2 changed files with 13 additions and 4 deletions

View file

@ -202,6 +202,7 @@ export async function createContentTypesGenerator({
fileUrl: event.entry,
contentEntryType,
fs,
invalidateCache: true,
});
if (!(collectionKey in contentTypes)) {
addCollection(contentTypes, collectionKey);
@ -225,6 +226,7 @@ export async function createContentTypesGenerator({
fileUrl: event.entry,
contentEntryType,
fs,
invalidateCache: true,
});
if (contentTypes[collectionKey]?.[entryKey]?.slug !== changedSlug) {
setEntry(contentTypes, collectionKey, entryKey, changedSlug);

View file

@ -442,6 +442,7 @@ export async function getStringifiedLookupMap({
return JSON.stringify(filePathByLookupId);
}
const frontmatterSlugCache = new Map<string, string>();
/**
* Check for slug in content entry frontmatter and validate the type,
* falling back to the `generatedSlug` if none is found.
@ -453,6 +454,7 @@ export async function getEntrySlug({
contentEntryType,
fileUrl,
fs,
invalidateCache = false,
}: {
fs: typeof fsMod;
id: string;
@ -460,11 +462,16 @@ export async function getEntrySlug({
generatedSlug: string;
fileUrl: URL;
contentEntryType: Pick<ContentEntryType, 'getEntryInfo'>;
invalidateCache?: boolean;
}) {
const { slug: frontmatterSlug } = await contentEntryType.getEntryInfo({
fileUrl,
contents: await fs.promises.readFile(fileUrl, 'utf-8'),
});
if (!frontmatterSlugCache.has(id) || invalidateCache) {
const { slug: frontmatterSlug } = await contentEntryType.getEntryInfo({
fileUrl,
contents: await fs.promises.readFile(fileUrl, 'utf-8'),
});
frontmatterSlugCache.set(id, frontmatterSlug);
}
const frontmatterSlug = frontmatterSlugCache.get(id);
return parseEntrySlug({ generatedSlug, frontmatterSlug, id, collection });
}