website
git clone https://git.pyrossh.dev/website
木 Personal website of pyrossh. Built with astrojs, shiki, vite.
src/_helpers/dataCache.js
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const CACHE_DIR = path.resolve(__dirname, "..", "..", ".cache");
const cachePath = (key) => path.join(CACHE_DIR, `${key}.json`);
/**
* Disk cache for the expensive per-commit/per-file git data (diffs,
* history, blame, syntax highlighting) computed in repoCommits.js/
* repoFiles.js. Keyed by the repo's current HEAD sha rather than by
* content hash — cheap to check (one `git rev-parse` per repo) and
* correct as long as callers only read the cache for a clean working
* tree (see isDirty() in gitRepos.js): if HEAD hasn't moved and there
* are no uncommitted changes, none of this data could have changed.
*/
export const readCache = (key, sha) => {
try {
const { sha: cachedSha, data } = JSON.parse(fs.readFileSync(cachePath(key), "utf8"));
return cachedSha === sha ? data : null;
} catch {
return null;
}
};
export const writeCache = (key, sha, data) => {
try {
fs.mkdirSync(CACHE_DIR, { recursive: true });
fs.writeFileSync(cachePath(key), JSON.stringify({ sha, data }));
} catch {
// best-effort — a failed cache write shouldn't fail the build
}
};