Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/content/onedrive-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { isOneDriveConfigured, readText } from "@/lib/onedrive/graph";
/**
* 사이트가 읽는 OneDrive 텍스트(site-data, 원페이저, Ask Hansol 문맥)를 Next 데이터 캐시에 얹는다.
* Graph 조회는 콜드 스타트에 1~3초 걸려서, 인스턴스 사이에서 공유되는 캐시(5분 재검증, stale-while-revalidate)
* 가 페이지 응답 시간을 지킨다. 파일을 고치면 늦어도 약 5분 뒤 반영된다.
* 가 페이지 응답 시간을 지킨다. 파일을 고치면 보통 5분 안팎, 늦어도 10분 안에 반영된다(재검증은 요청이 올 때 뒤에서 돈다).
*/
const REVALIDATE_SECONDS = Number(process.env.ONEDRIVE_TEXT_REVALIDATE_SECONDS ?? 300);

const cachedRead = unstable_cache(async (relPath: string) => readText(relPath), ["onedrive-text-v1"], {
const cachedRead = unstable_cache(async (relPath: string) => readText(relPath, { fresh: true }), ["onedrive-text-v2"], {
revalidate: REVALIDATE_SECONDS,
tags: ["onedrive-text"],
});
Expand Down
9 changes: 6 additions & 3 deletions src/lib/onedrive/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,13 @@ export async function readBytes(relPath: string): Promise<ArrayBuffer | null> {
return res.arrayBuffer();
}

/** 텍스트 파일 읽기. 인스턴스 메모리에 5분 캐시(오류는 캐시하지 않는다). 없으면 null. */
export async function readText(relPath: string): Promise<string | null> {
/**
* 텍스트 파일 읽기. 없으면 null. 기본은 인스턴스 메모리에 5분 캐시(오류는 캐시하지 않는다).
* `fresh: true` 면 메모리 캐시를 건너뛴다(바깥에 다른 캐시가 있을 때 두 캐시가 겹쳐 반영이 늦어지지 않게).
*/
export async function readText(relPath: string, opts: { fresh?: boolean } = {}): Promise<string | null> {
const now = Date.now();
const hit = textCache.get(relPath);
const hit = opts.fresh ? undefined : textCache.get(relPath);
if (hit && hit.expiresAt > now) return hit.text;
const buf = await readBytes(relPath);
const text = buf ? new TextDecoder("utf-8").decode(buf) : null;
Expand Down
Loading