Save/load index to files, improve logging

This commit is contained in:
Michael Naumov
2022-06-06 00:42:29 -06:00
parent f679f3bde9
commit 7cd4f6c502
3 changed files with 101 additions and 15 deletions

View File

@@ -14,9 +14,29 @@ import { stringsToRegex } from './utils'
*/
export let notesCache: Record<string, IndexedNote> = {}
const notesCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/notesCache.json`
export function resetNotesCache(): void {
notesCache = {}
}
export async function loadNotesCache(): Promise<void> {
if (await app.vault.adapter.exists(notesCacheFilePath)) {
try {
const json = await app.vault.adapter.read(notesCacheFilePath)
notesCache = JSON.parse(json)
console.log("Notes cache loaded from the file")
}
catch(e) {
console.trace("Could not load Notes cache from the file")
console.error(e)
}
}
if (!notesCache) {
notesCache = {}
}
}
export function getNoteFromCache(key: string): IndexedNote | undefined {
return notesCache[key]
}
@@ -117,3 +137,14 @@ export function getNonExistingNotes(
export function removeAnchors(name: string): string {
return name.split(/[\^#]+/)[0]
}
export async function saveNotesCacheToFile(): Promise<void> {
const json = JSON.stringify(notesCache)
await app.vault.adapter.write(notesCacheFilePath, json)
console.log("Notes cache saved to the file")
}
export function isCacheOutdated(file: TFile): boolean {
const indexedNote = getNoteFromCache(file.path)
return !indexedNote || indexedNote.mtime !== file.stat.mtime
}