Squashed commit of the following:
commit 603b9bbde4c6efc90c81032e4e765c64d3075e75 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Tue Oct 11 21:47:03 2022 +0200 Basic PDF indexing ok commit 200331bb5c5111493af1e1f6ef8cd4bbfbdbfd4f Author: Simon Cambier <simon.cambier@protonmail.com> Date: Tue Oct 11 20:56:44 2022 +0200 Tweaks and comments commit 434b9662d40c5fea9d8b28d43828b11916db8c94 Author: Simon Cambier <simon.cambier@ores.be> Date: Tue Oct 11 16:22:55 2022 +0200 Refactoring notes & minisearch cache commit 7253c676c8ed161782ba8e33f0c4c162880925ad Author: Simon Cambier <simon.cambier@protonmail.com> Date: Tue Oct 11 09:50:33 2022 +0200 wip commit 77736e6ef6f28ccfddb64fb768732927d43bbd77 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Mon Oct 10 20:49:02 2022 +0200 Small rewrites & deps updates commit 59845fdb89eb6a3ad3f3f9ad75b39e7a3e604c45 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Mon Oct 10 12:22:11 2022 +0200 wasm + worker ok commit 1cf3b506e56147586cd0ebcc003642c5230e04cc Author: Simon Cambier <simon.cambier@protonmail.com> Date: Sun Oct 2 20:04:49 2022 +0200 no disk access, of course commit eb3dd9dd4f616a479a53e10856f6c96c6725e911 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Sun Oct 2 19:08:48 2022 +0200 Rollup build ok commit 54f2b7e615456c0e1b1504691689d1ba2c72d9e8 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Sun Oct 2 16:03:31 2022 +0200 Rollup build + wasm PoC
This commit is contained in:
120
src/cache-manager.ts
Normal file
120
src/cache-manager.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import { throttle } from 'lodash-es'
|
||||
import type MiniSearch from 'minisearch'
|
||||
import type { TFile } from 'obsidian'
|
||||
import { deflate, inflate } from 'pako'
|
||||
import {
|
||||
notesCacheFilePath,
|
||||
minisearchCacheFilePath,
|
||||
type IndexedNote,
|
||||
} from './globals'
|
||||
import { settings } from './settings'
|
||||
|
||||
class CacheManager {
|
||||
notesCache: Record<string, IndexedNote> = {}
|
||||
compress = true
|
||||
writeInterval = 5_000 // In milliseconds
|
||||
|
||||
//#region Minisearch
|
||||
|
||||
/**
|
||||
* Serializes and writes the Minisearch index on the disk
|
||||
*/
|
||||
public writeMinisearchIndex = throttle(
|
||||
this._writeMinisearchIndex,
|
||||
this.writeInterval,
|
||||
{
|
||||
leading: true,
|
||||
trailing: true,
|
||||
}
|
||||
)
|
||||
private async _writeMinisearchIndex(minisearch: MiniSearch): Promise<void> {
|
||||
if (!settings.persistCache) {
|
||||
return
|
||||
}
|
||||
const json = JSON.stringify(minisearch)
|
||||
const data = this.compress ? deflate(json) : json
|
||||
await app.vault.adapter.writeBinary(minisearchCacheFilePath, data as any)
|
||||
console.log('Omnisearch - Minisearch index saved on disk')
|
||||
}
|
||||
|
||||
public async readMinisearchIndex(): Promise<string | null> {
|
||||
if (!settings.persistCache) {
|
||||
return null
|
||||
}
|
||||
if (await app.vault.adapter.exists(minisearchCacheFilePath)) {
|
||||
try {
|
||||
const data = await app.vault.adapter.readBinary(minisearchCacheFilePath)
|
||||
return (
|
||||
this.compress ? new TextDecoder('utf8').decode(inflate(data)) : data
|
||||
) as any
|
||||
} catch (e) {
|
||||
console.trace(
|
||||
'Omnisearch - Could not load MiniSearch index from the file:'
|
||||
)
|
||||
console.warn(e)
|
||||
app.vault.adapter.remove(minisearchCacheFilePath)
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
//#endregion Minisearch
|
||||
|
||||
public async loadNotesCache() {
|
||||
if (!settings.persistCache) {
|
||||
return null
|
||||
}
|
||||
if (await app.vault.adapter.exists(notesCacheFilePath)) {
|
||||
try {
|
||||
const data = await app.vault.adapter.readBinary(notesCacheFilePath)
|
||||
const json = (
|
||||
this.compress ? new TextDecoder('utf8').decode(inflate(data)) : data
|
||||
) as any
|
||||
this.notesCache = JSON.parse(json)
|
||||
} catch (e) {
|
||||
console.trace('Omnisearch - Could not load notes cache:')
|
||||
console.warn(e)
|
||||
app.vault.adapter.remove(notesCacheFilePath)
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
public saveNotesCache = throttle(this._saveNotesCache, this.writeInterval, {
|
||||
leading: true,
|
||||
trailing: true,
|
||||
})
|
||||
private async _saveNotesCache() {
|
||||
if (!settings.persistCache) {
|
||||
return
|
||||
}
|
||||
const json = JSON.stringify(this.notesCache)
|
||||
const data = this.compress ? deflate(json) : json
|
||||
await app.vault.adapter.writeBinary(notesCacheFilePath, data as any)
|
||||
console.log('Omnisearch - Notes cache saved on disk')
|
||||
}
|
||||
|
||||
public addNoteToCache(path: string, note: IndexedNote) {
|
||||
this.notesCache[path] = note
|
||||
this.saveNotesCache()
|
||||
}
|
||||
|
||||
public removeNoteFromCache(key: string): void {
|
||||
delete this.notesCache[key]
|
||||
}
|
||||
|
||||
public getNoteFromCache(key: string): IndexedNote | undefined {
|
||||
return this.notesCache[key]
|
||||
}
|
||||
|
||||
public getNonExistingNotesFromCache(): IndexedNote[] {
|
||||
return Object.values(this.notesCache).filter(note => note.doesNotExist)
|
||||
}
|
||||
|
||||
public isCacheOutdated(file: TFile): boolean {
|
||||
const indexedNote = this.getNoteFromCache(file.path)
|
||||
return !indexedNote || indexedNote.mtime !== file.stat.mtime
|
||||
}
|
||||
}
|
||||
|
||||
export const cacheManager = new CacheManager()
|
||||
Reference in New Issue
Block a user