fix: distinct multiple layer of cache

This commit is contained in:
Julien Calixte
2024-10-27 12:33:26 +01:00
parent ffa7c42021
commit 84789393ad
4 changed files with 46 additions and 15 deletions

View File

@@ -18,19 +18,20 @@ 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( const { getCachedNote, saveCacheNote } = prepareNoteCache(
toValue(sha), toValue(sha),
toValue(path) toValue(path)
) )
const fromCache = ref(false)
const fromCache = ref(false)
const rawContent = ref('') const rawContent = ref('')
const content = computed(() => const content = computed(() =>
rawContent.value ? renderFromUTF8(rawContent.value) : '' rawContent.value ? renderFromUTF8(rawContent.value) : ''
) )
const getEditedSha = async () => { const getEditedSha = async () => {
const note = await getCachedNote() const { note } = await getCachedNote()
if (!note) { if (!note) {
return null return null
@@ -40,26 +41,38 @@ export const useFile = (sha: Ref<string> | string, retrieveContent = true) => {
} }
const getCachedFileContent = async (): Promise<string | null> => { const getCachedFileContent = async (): Promise<string | null> => {
const cachedNote = await getCachedNote() const { note: cachedNote, from } = await getCachedNote()
fromCache.value = !!cachedNote fromCache.value = !!cachedNote
if (cachedNote) { if (cachedNote) {
if (from === 'path') {
queryFileContent(store.user, store.repo, toValue(sha)).then(
(fileContent) => {
if (!fileContent) {
return
}
saveCacheNote(fileContent)
rawContent.value = getRawContentFromFile(fileContent)
}
)
}
return cachedNote.content return cachedNote.content
} }
const contentFile = await queryFileContent( const fileContent = await queryFileContent(
store.user, store.user,
store.repo, store.repo,
toValue(sha) toValue(sha)
) )
if (!contentFile) { if (!fileContent) {
return null return null
} }
saveCacheNote(contentFile) saveCacheNote(fileContent)
return contentFile return fileContent
} }
const getRawContent = async () => { const getRawContent = async () => {

View File

@@ -32,9 +32,9 @@ export const useOfflineNote = () => {
file.path file.path
) )
const isNoteCached = (await getCachedNote()) !== null const { from } = await getCachedNote()
if (isNoteCached) { if (from === 'sha') {
continue continue
} }

View File

@@ -3,23 +3,41 @@ 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'
type NoteCacheResult =
| {
note: Note
from: 'sha'
}
| { note: Note; from: 'path' }
| { note: null; from: null }
export const prepareNoteCache = (sha: string, path?: 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 notePath = path ? data.generateId(DataType.Note, path) : null const notePath = path ? data.generateId(DataType.Note, path) : null
const getCachedNote = async () => { const getCachedNote = async (): Promise<NoteCacheResult> => {
const note = await data.get<DataType.Note, Note>(noteId) const note = await data.get<DataType.Note, Note>(noteId)
if (note) { if (note) {
return note return { note, from: 'sha' }
} }
if (notePath) { if (notePath) {
return data.get<DataType.Note, Note>(notePath) const note = await data.get<DataType.Note, Note>(notePath)
if (!note) {
return {
note: null,
from: null
}
}
return {
note,
from: 'path'
}
} }
return null return { note: null, from: null }
} }
const saveCacheNote = async ( const saveCacheNote = async (

View File

@@ -44,7 +44,7 @@ export const getCachedMainReadme = async (owner: string, repo: string) => {
const { render } = useMarkdown() const { render } = useMarkdown()
const { getCachedNote } = prepareNoteCache(`${owner}-${repo}-README`) const { getCachedNote } = prepareNoteCache(`${owner}-${repo}-README`)
const cachedReadme = await getCachedNote() const { note: cachedReadme } = await getCachedNote()
if (!cachedReadme) { if (!cachedReadme) {
return null return null
@@ -77,7 +77,7 @@ export const getMainReadme = async (owner: string, repo: string) => {
} }
} catch (error) { } catch (error) {
console.warn(error) console.warn(error)
const cachedReadme = await getCachedNote() const { note: cachedReadme } = await getCachedNote()
if (cachedReadme) { if (cachedReadme) {
return render(cachedReadme.content) return render(cachedReadme.content)