Skip to content

Commit

Permalink
feat: fix template file cache
Browse files Browse the repository at this point in the history
Signed-off-by: Jason C. Leach <[email protected]>
  • Loading branch information
jleach committed Dec 20, 2024
1 parent 03a9d6d commit 049f693
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/legacy/core/App/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ export const hitSlop = { top: 44, bottom: 44, left: 44, right: 44 }

export const templateBundleStorageDirectory = 'templates'

export const templateCacheDataFileName = 'proof-templates.json'
export const templateCacheDataFileName = 'index.json'

export const templateBundleIndexFileName = 'proof-templates.json'
30 changes: 30 additions & 0 deletions packages/legacy/core/App/utils/fileCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ export class FileCache {
return this._fileEtag || ''
}

protected loadCacheData = async (): Promise<CacheDataFile | undefined> => {
const cacheFileExists = await this.checkFileExists(this.cacheFileName)
if (!cacheFileExists) {
return
}

const data = await this.loadFileFromLocalStorage(this.cacheFileName)
if (!data) {
return
}

const cacheData: CacheDataFile = JSON.parse(data)
return cacheData
}

protected saveCacheData = async (cacheData: CacheDataFile): Promise<boolean> => {
const cacheDataAsString = JSON.stringify(cacheData)

Expand Down Expand Up @@ -119,4 +134,19 @@ export class FileCache {

return false
}

protected stripWeakEtag = (etag: string): string => {
if (etag.startsWith('W/')) {
return etag.slice(2).replace(/"/g, '')
}

return etag.replace(/"/g, '')
}

protected compareWeakEtags = (etag1: string, etag2: string): boolean => {
const cleanEtag1 = this.stripWeakEtag(etag1)
const cleanEtag2 = this.stripWeakEtag(etag2)

return cleanEtag1 === cleanEtag2
}
}
45 changes: 18 additions & 27 deletions packages/legacy/core/App/utils/proofBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class RemoteProofBundleResolver extends FileCache implements IProofBundle
private cacheDataFileName = templateCacheDataFileName

public constructor(indexFileBaseUrl: string, log?: BifoldLogger) {
super(indexFileBaseUrl, templateBundleStorageDirectory, templateBundleIndexFileName, log)
super(indexFileBaseUrl, templateBundleStorageDirectory, templateCacheDataFileName, log)
}

public async resolve(acceptDevRestrictions: boolean): Promise<ProofRequestTemplate[] | undefined> {
Expand Down Expand Up @@ -154,29 +154,23 @@ export class RemoteProofBundleResolver extends FileCache implements IProofBundle
}

this.log?.info('Loading index now')
await this.loadBundleIndex(this.cacheDataFileName)
await this.loadBundleIndex(templateBundleIndexFileName)
}

private loadBundleIndex = async (filePath: string) => {
let remoteFetchSucceeded = false
try {
const response = await this.axiosInstance.get(filePath)
const { status } = response
const { etag } = response.headers

if (status !== 200) {
this.log?.error(`Failed to fetch remote resource at ${filePath}`)
// failed to fetch, use the cached index file
// if available
const data = await this.loadFileFromLocalStorage(filePath)
if (data) {
this.log?.info(`Using index file ${filePath} from cache`)
this.templateData = JSON.parse(data)
}

return
throw new Error('Failed to fetch remote resource')
}

if (etag && etag === this.fileEtag) {
if (etag && this.compareWeakEtags(this.fileEtag, etag)) {
this.log?.info(`Index file ${filePath} has not changed, etag ${etag}`)
// etag is the same, no need to refresh
this.templateData = response.data
Expand All @@ -186,28 +180,25 @@ export class RemoteProofBundleResolver extends FileCache implements IProofBundle

this.fileEtag = etag
this.templateData = response.data
remoteFetchSucceeded = true

this.log?.info(`Saving file ${filePath}, etag ${etag}`)
await this.saveFileToLocalStorage(filePath, JSON.stringify(this.templateData))
} catch (error) {
this.log?.error(`Failed to fetch file index ${filePath}`)
}
}
this.log?.error(`Failed to fetch remote file index ${filePath}`)
} finally {
if (remoteFetchSucceeded) {
return
}

private loadCacheData = async (): Promise<CacheDataFile | undefined> => {
const cacheFileExists = await this.checkFileExists(this.cacheDataFileName)
if (!cacheFileExists) {
return
}
const data = await this.loadFileFromLocalStorage(filePath)
if (!data) {
this.log?.error(`Failed to load index file ${filePath} from cache`)
return
}

const data = await this.loadFileFromLocalStorage(this.cacheDataFileName)
if (!data) {
return
this.log?.info(`Using index file ${filePath} from cache`)
this.templateData = JSON.parse(data)
}

const cacheData: CacheDataFile = JSON.parse(data)

return cacheData
}
}

Expand Down

0 comments on commit 049f693

Please sign in to comment.