Skip to content

Commit

Permalink
feat: add cache meta collector
Browse files Browse the repository at this point in the history
  • Loading branch information
RyoJerryYu committed May 4, 2024
1 parent 8902908 commit c032496
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/core/indexing/indexing-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export const initCache = async () => {
if (cache) {
return cache;
}
console.log("init cache");
cache = await executePipeline(pipeline);
console.log("cache inited");

return cache;
};
Expand Down
6 changes: 5 additions & 1 deletion src/core/indexing/indexing-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export const defaultStaticResourceChain: MetaCollectorChain<BaseMeta> = {
};

export const defaultChain: MetaCollectorChain<PostMeta> = {
collectors: [new PostRawMetaCollector(), defaultGitMetaCollector()],
collectors: [
// new CacheMetaCollector(".", "cache", ["content"]),
new PostRawMetaCollector(),
defaultGitMetaCollector(),
],
defaultMeta: {
content: "",
title: "<No Title>",
Expand Down
14 changes: 14 additions & 0 deletions src/core/indexing/meta-collecting/cache-meta-collector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fs from "fs";
import git from "isomorphic-git";
describe("test", () => {
it("should work", async () => {
const commits = await git.log({
fs: fs,
dir: ".",
// depth: 1,
filepath: "public/content/articles/2020-01-27-Building-this-blog.md",
});

console.log(commits);
});
});
82 changes: 82 additions & 0 deletions src/core/indexing/meta-collecting/cache-meta-collector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { BaseMeta } from "@/core/types/indexing";
import dayjs from "dayjs";
import fs, { existsSync } from "fs";
import { mkdir, readFile, writeFile } from "fs/promises";
import git from "isomorphic-git";
import { dirname } from "path";
import { dayJsFromCommit } from "../utils/git-utils";
import { MetaCollector } from "./meta-collecting";

export class CacheMetaCollector<Meta extends BaseMeta>
implements MetaCollector<Meta>
{
constructor(
readonly gitDir: string,
readonly cacheDir: string,
readonly exceptFields: (keyof Meta)[]
) {}

filePathToCachePath = (filePath: string) => {
return `${this.cacheDir}/${filePath}.json`;
};

handleAbleKeys = (): "*" => {
return "*";
};

updatedAtFromGit = async (filePath: string): Promise<dayjs.Dayjs> => {
const commits = await git.log({ fs, dir: this.gitDir, filepath: filePath });
return dayJsFromCommit(commits[0]);
};

cacheExpired = async (filePath: string): Promise<boolean> => {
try {
const fileUpdatedAt = await this.updatedAtFromGit(filePath);
const cachePath = this.filePathToCachePath(filePath);
if (!existsSync(cachePath)) {
return true;
}
const cacheUpdatedAt = await this.updatedAtFromGit(cachePath);
return fileUpdatedAt.isAfter(cacheUpdatedAt);
} catch (e) {
// any error occurs, treat as expired
return true;
}
};

collectMeta = async (filePath: string): Promise<Partial<Meta>> => {
const cachePath = this.filePathToCachePath(filePath);
if (await this.cacheExpired(filePath)) {
return {};
}

console.log(`load cache for ${filePath} from ${cachePath}`);
const file = await readFile(cachePath, { encoding: "utf-8" });
console.log(file);
const meta = JSON.parse(file);
if (this.exceptFields) {
for (const field of this.exceptFields) {
delete meta[field];
}
}
return meta;
};
defer = async (filePath: string, meta: Meta): Promise<void> => {
const cachePath = this.filePathToCachePath(filePath);
if (!existsSync(dirname(cachePath))) {
console.log(`create cache dir for ${filePath} to ${cachePath}`);
await mkdir(dirname(cachePath), { recursive: true });
}
console.log(`defer write cache for ${filePath} to ${cachePath}`);
const cacheMeta = { ...meta };
if (this.exceptFields) {
for (const field of this.exceptFields) {
delete cacheMeta[field];
}
}
await writeFile(cachePath, JSON.stringify(cacheMeta), {
flag: "w+",
encoding: "utf-8",
});
};
}
4 changes: 2 additions & 2 deletions src/core/indexing/meta-collecting/meta-collecting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface MetaCollector<Meta extends BaseMeta> {
*/
handleAbleKeys(): Array<keyof Meta> | "*";
collectMeta(filePath: string): Promise<Partial<Meta>>;
defer?(meta: Meta): void;
defer?(filePath: string, meta: Meta): Promise<void>;
}

/**
Expand Down Expand Up @@ -77,7 +77,7 @@ export async function collectMetaForFilePath<Meta extends BaseMeta>(
if (collectorExecuteds[i]) {
// only execute defer if the collector is executed
const collector = collectors[i];
collector.defer?.(fullMeta);
await collector.defer?.(filePath, fullMeta);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/indexing/utils/git-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dayjs from "dayjs";
import { ReadCommitResult } from "isomorphic-git";

function dayJsFromCommit(commit: ReadCommitResult): dayjs.Dayjs {
export function dayJsFromCommit(commit: ReadCommitResult): dayjs.Dayjs {
return dayjs(commit.commit.committer.timestamp * 1000);
}

Expand Down

0 comments on commit c032496

Please sign in to comment.