Squashed commit of the following:
commit ac82511ddd17d5472ae3cfea9bbad9754f5a4d62 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Sat Oct 22 08:23:42 2022 +0200 Screw that cache, seriously. commit 8ba40d1be73daaaffea09e07bc56c339266db9b6 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Fri Oct 21 22:36:48 2022 +0200 Stuff commit 27b8fd7dc809be9714a109d3a458eb1276a47e2e Author: Simon Cambier <simon.cambier@protonmail.com> Date: Fri Oct 21 22:22:20 2022 +0200 Moved files commit fb1349c914907e586e103ca54fb04b9ddd45ef5d Author: Simon Cambier <simon.cambier@protonmail.com> Date: Thu Oct 20 22:25:29 2022 +0200 Removed duplicate code commit e7371138e60cbe4155cfd4fb44e3ee1d2e3ee088 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Thu Oct 20 21:50:09 2022 +0200 Moved a bunch of files commit 2ee1b2a0e799d4b41ab3a444d8cc44dfff5b5623 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Thu Oct 20 21:32:21 2022 +0200 Removed useless code commit 76c530dfb9adbad1bbe9079de2330fe43a044249 Author: Simon Cambier <simon.cambier@protonmail.com> Date: Thu Oct 20 20:44:11 2022 +0200 Split file reading and indexing
This commit is contained in:
@@ -1,120 +1,50 @@
|
||||
import { throttle } from 'lodash-es'
|
||||
import type MiniSearch from 'minisearch'
|
||||
import type { TFile } from 'obsidian'
|
||||
import { deflate, inflate } from 'pako'
|
||||
import {
|
||||
notesCacheFilePath,
|
||||
minisearchCacheFilePath,
|
||||
type IndexedDocument,
|
||||
} from './globals'
|
||||
import { settings } from './settings'
|
||||
import type { IndexedDocument } from './globals'
|
||||
|
||||
class CacheManager {
|
||||
notesCache: Record<string, IndexedDocument> = {}
|
||||
compress = true
|
||||
writeInterval = 5_000 // In milliseconds
|
||||
private documentsCache: Map<string, IndexedDocument> = new Map()
|
||||
private writeInterval = 10_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 updateDocument(path: string, note: IndexedDocument) {
|
||||
this.documentsCache.set(path, note)
|
||||
}
|
||||
|
||||
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
|
||||
public deleteDocument(key: string): void {
|
||||
this.documentsCache.delete(key)
|
||||
}
|
||||
|
||||
//#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 addNoteToMemCache(path: string, note: IndexedDocument) {
|
||||
this.notesCache[path] = note
|
||||
this.saveNotesCache()
|
||||
}
|
||||
|
||||
public removeNoteFromMemCache(key: string): void {
|
||||
delete this.notesCache[key]
|
||||
}
|
||||
|
||||
public getNoteFromMemCache(key: string): IndexedDocument | undefined {
|
||||
return this.notesCache[key]
|
||||
public getDocument(key: string): IndexedDocument | undefined {
|
||||
return this.documentsCache.get(key)
|
||||
}
|
||||
|
||||
public getNonExistingNotesFromMemCache(): IndexedDocument[] {
|
||||
return Object.values(this.notesCache).filter(note => note.doesNotExist)
|
||||
return Object.values(this.documentsCache).filter(note => note.doesNotExist)
|
||||
}
|
||||
|
||||
public isNoteInMemCacheOutdated(file: TFile): boolean {
|
||||
const indexedNote = this.getNoteFromMemCache(file.path)
|
||||
public isDocumentOutdated(file: TFile): boolean {
|
||||
const indexedNote = this.getDocument(file.path)
|
||||
return !indexedNote || indexedNote.mtime !== file.stat.mtime
|
||||
}
|
||||
|
||||
// private async _writeMinisearchIndex(minisearch: MiniSearch): Promise<void> {
|
||||
// if (!settings.persistCache) {
|
||||
// return
|
||||
// }
|
||||
// const json = JSON.stringify(minisearch)
|
||||
// const data = deflate(json)
|
||||
// await app.vault.adapter.writeBinary(minisearchCacheFilePath, data as any)
|
||||
// console.log('Omnisearch - Minisearch index saved on disk')
|
||||
// }
|
||||
//
|
||||
// private async _saveNotesCache() {
|
||||
// if (!settings.persistCache) {
|
||||
// return
|
||||
// }
|
||||
// const json = JSON.stringify(Array.from(this.documentsCache.entries()))
|
||||
// const data = deflate(json)
|
||||
// await app.vault.adapter.writeBinary(notesCacheFilePath, data as any)
|
||||
// console.log('Omnisearch - Notes cache saved on disk')
|
||||
// }
|
||||
}
|
||||
|
||||
export const cacheManager = new CacheManager()
|
||||
|
||||
Reference in New Issue
Block a user