Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved cache use to avoid download images if the content was not changed. #22

Merged
merged 1 commit into from
Oct 28, 2024
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
21 changes: 15 additions & 6 deletions feishu-pages/src/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { printMemoryUsage, writeTemplfile } from './utils';
* @param document_id doc.obj_token
* @returns
*/
export const fetchDocBody = async (fileDoc: Doc) => {
export const fetchDocBody = async (fileDoc: Doc): Promise<{ contentFile: string; meta: Record<string, any>; fileTokens: Record<string, any>; hasCache: boolean }> => {
let document_id = fileDoc.obj_token;

const doc = {
Expand All @@ -21,15 +21,20 @@ export const fetchDocBody = async (fileDoc: Doc) => {
blocks: [],
};

const fetchDocBlocks = async (document_id: string) => {
const fetchDocBlocks = async (document_id: string): Promise<{ hasCache: boolean; blocks: any[] }> => {
// Check cache in .cache/docs/${document_id}.json
let hasCache = false;
let cacheBlocks = path.join(CACHE_DIR, 'blocks', document_id + '.json');
fs.mkdirSync(path.dirname(cacheBlocks), { recursive: true });
if (fs.existsSync(cacheBlocks)) {
const doc = JSON.parse(fs.readFileSync(cacheBlocks, 'utf-8'));
if (doc?.obj_edit_time === fileDoc.obj_edit_time) {
hasCache = true;
console.info('Cache hit doc: ', document_id, '...');
return doc.blocks;
return {
hasCache,
blocks: doc.blocks,
};
}
}

Expand All @@ -49,10 +54,13 @@ export const fetchDocBody = async (fileDoc: Doc) => {
blocks,
})
);
return blocks;
return { hasCache, blocks };
};

doc.blocks = await fetchDocBlocks(document_id);

let { blocks, hasCache } = await fetchDocBlocks(document_id);

doc.blocks = blocks;
printMemoryUsage('loaded doc blocks');

const render = new MarkdownRenderer(doc as any);
Expand All @@ -64,9 +72,10 @@ export const fetchDocBody = async (fileDoc: Doc) => {
let tmp_filename = writeTemplfile(content);

return {
cotnent_file: tmp_filename,
contentFile: tmp_filename,
meta,
fileTokens,
hasCache,
};
};

Expand Down
13 changes: 8 additions & 5 deletions feishu-pages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ import { fetchAllDocs } from './wiki';
const fetchDocBodies = async (docs: FileDoc[]) => {
for (let idx = 0; idx < docs.length; idx++) {
const doc = docs[idx];
const { cotnent_file, fileTokens, meta } = await fetchDocBody(doc);
const { contentFile, fileTokens, meta, hasCache } = await fetchDocBody(doc);

doc.cotnent_file = cotnent_file;
doc.contentFile = contentFile;
doc.meta = meta;
doc.fileTokens = fileTokens;
doc.hasCache = hasCache;

await fetchDocBodies(doc.children);
}
Expand All @@ -97,9 +98,9 @@ const fetchDocAndWriteFile = async (
const folder = path.dirname(filename);
fs.mkdirSync(folder, { recursive: true });

let { cotnent_file, fileTokens } = doc;
let { contentFile, fileTokens } = doc;

let content = fs.readFileSync(cotnent_file, 'utf-8');
let content = fs.readFileSync(contentFile, 'utf-8');

// Replace node_token to slug

Expand All @@ -118,7 +119,9 @@ const fetchDocAndWriteFile = async (
let out = '';
out += metaInfo + '\n\n';

content = await downloadFiles(content, fileTokens, folder);
if (!doc.hasCache) {
content = await downloadFiles(content, fileTokens, folder);
}

out += content;

Expand Down
3 changes: 2 additions & 1 deletion feishu-pages/src/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ export interface FileDoc extends Doc {
position: number;
filename: string;
/// The content file path (in Tmp path)
cotnent_file?: string;
contentFile?: string;
meta?: Record<string, any>;
fileTokens?: any;
children: FileDoc[];
hasCache?: boolean;
}

/**
Expand Down
Loading