add a more resilient cache system with a global cache for each note from path

This commit is contained in:
Julien Calixte
2024-07-16 21:55:54 +02:00
parent 100d593050
commit 8802ffb5b2
3 changed files with 27 additions and 4 deletions

View File

@@ -18,7 +18,10 @@ export const useFile = (sha: Ref<string> | string, retrieveContent = true) => {
renderFromUTF8, renderFromUTF8,
getRawContent: getRawContentFromFile getRawContent: getRawContentFromFile
} = useMarkdown(toValue(sha)) } = useMarkdown(toValue(sha))
const { getCachedNote, saveCacheNote } = prepareNoteCache(toValue(sha)) const { getCachedNote, saveCacheNote } = prepareNoteCache(
toValue(sha),
toValue(path)
)
const fromCache = ref(false) const fromCache = ref(false)
const rawContent = ref('') const rawContent = ref('')

View File

@@ -3,11 +3,24 @@ import { DataType } from '@/data/DataType.enum'
import { Note } from '@/modules/note/models/Note' import { Note } from '@/modules/note/models/Note'
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store' import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
export const prepareNoteCache = (sha: string) => { export const prepareNoteCache = (sha: string, path?: string) => {
const store = useUserRepoStore() const store = useUserRepoStore()
const noteId = data.generateId(DataType.Note, sha) const noteId = data.generateId(DataType.Note, sha)
const getCachedNote = async () => data.get<DataType.Note, Note>(noteId) const notePath = path ? data.generateId(DataType.Note, path) : null
const getCachedNote = async () => {
const note = await data.get<DataType.Note, Note>(noteId)
if (note) {
return note
}
if (notePath) {
return data.get<DataType.Note, Note>(notePath)
}
return null
}
const saveCacheNote = async ( const saveCacheNote = async (
content: string, content: string,
@@ -26,6 +39,13 @@ export const prepareNoteCache = (sha: string) => {
}) })
await data.update(newNote) await data.update(newNote)
if (notePath) {
await data.update({
...newNote,
_id: notePath
})
}
} }
return { return {

View File

@@ -49,7 +49,7 @@ watch(mode, async (newMode) => {
} }
newContent.value = initialContent newContent.value = initialContent
const { saveCacheNote } = prepareNoteCache(newSha) const { saveCacheNote } = prepareNoteCache(newSha, newContentPath)
await saveCacheNote(encodeUTF8ToBase64(content), { await saveCacheNote(encodeUTF8ToBase64(content), {
editedSha: newSha, editedSha: newSha,
path: newContentPath path: newContentPath