Skip to content

Commit

Permalink
add cache to sha256
Browse files Browse the repository at this point in the history
  • Loading branch information
wfjsw committed Aug 10, 2024
1 parent f5d4412 commit 6a74e80
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ jobs:
- name: Install dependencies
run: |
npm install
- name: Cache SHA256
id: cache-sha256
uses: actions/cache@v4
with:
path: .script/cache.json
key: sha256-node-${{ hashFiles('**/package-lock.json') }}

- name: Build
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
dist/
.script/cache.json
40 changes: 40 additions & 0 deletions .script/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { readFile, writeFile } from "fs/promises";

const cache: Record<string, string> = {}

export function getCache(key: string): string | undefined {
return cache[key];
}

export function setCache(key: string, value: string): void {
cache[key] = value;
}

export function clearCache(): void {
for (const key in cache) {
delete cache[key];
}
}

export function deleteCache(key: string): void {
delete cache[key];
}

export async function loadCache() {
try {
const data = await readFile(".script/cache.json", "utf-8");
const parsed = JSON.parse(data);
for (const key in parsed) {
cache[key] = parsed[key];
}
} catch (e) {
}
}

export async function saveCache() {
try {
await writeFile(".script/cache.json", JSON.stringify(cache), "utf-8");
} catch (e) {
console.error(e);
}
}
31 changes: 17 additions & 14 deletions .script/hub.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { downloadFile, listFiles } from "@huggingface/hub";
import { HF_TOKEN } from "./env";
import { getSha256FromResponse } from "./sha256";
import { getCache, setCache } from "./cache";

interface HubFileMetadata {
filename: string;
Expand All @@ -20,22 +21,24 @@ export async function getFiles(repoId: string, revision: string) {
let sha256 = file.lfs?.oid;

if (!sha256) {
const response = await downloadFile({
repo: repoId,
revision,
path: file.path,
credentials: { accessToken: HF_TOKEN },
});
const cached = getCache(file.oid);
if (cached) {
sha256 = cached;
} else {
const response = await downloadFile({
repo: repoId,
revision,
path: file.path,
credentials: { accessToken: HF_TOKEN },
});

if (!response) {
throw new Error(
`Failed to download file ${file.path}`
);
}
if (!response) {
throw new Error(`Failed to download file ${file.path}`);
}

sha256 = await getSha256FromResponse(
response
);
sha256 = await getSha256FromResponse(response);
setCache(file.oid, sha256);
}
}

files.push({
Expand Down
5 changes: 5 additions & 0 deletions .script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { readFile, mkdir, writeFile, rm } from "fs/promises";
import { parse } from "yaml";
import type { IProvider, Resolution, ResolutionFile, Rule } from "./interfaces";
import { getFiles } from "./hub";
import { loadCache, saveCache } from "./cache";

const root = resolve(fileURLToPath(import.meta.url), "../..")

async function main() {
console.log(root);

await loadCache();

const providers: Record<string, IProvider> = {};

const providerFiles = globStream("./providers/*.ts", {
Expand Down Expand Up @@ -67,6 +70,8 @@ async function main() {
}

}

await saveCache();
}

main();

0 comments on commit 6a74e80

Please sign in to comment.