Improve cache-reporting when entry already exists

This commit is contained in:
Daz DeBoer 2022-01-19 12:11:51 -07:00
parent a23ac1d61c
commit 0a5ede19a9
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
4 changed files with 34 additions and 23 deletions

View file

@ -76,6 +76,12 @@ export class CacheEntryListener {
this.savedSize = size
return this
}
markAlreadyExists(key: string): CacheEntryListener {
this.savedKey = key
this.savedSize = 0
return this
}
}
export function logCachingReport(listener: CacheListener): void {
@ -112,12 +118,18 @@ function getSum(
cacheEntries: CacheEntryListener[],
predicate: (value: CacheEntryListener) => number | undefined
): string {
if (cacheEntries.length === 0) {
return '0'
}
return formatSize(cacheEntries.map(e => predicate(e) ?? 0).reduce((p, v) => p + v, 0))
}
function formatSize(bytes: number | undefined): string {
if (bytes === undefined || bytes === 0) {
if (bytes === undefined) {
return ''
}
if (bytes === 0) {
return '0 (Entry already exists)'
}
return `${Math.round(bytes / (1024 * 1024))} MB (${bytes} B)`
}