Unverified Commit d6ca86c1 authored by vercel[bot]'s avatar vercel[bot] Committed by GitHub

perf: avoid reading image bodies during cache init

Co-Authored-By: 's avatarMarcos Hernanz <96699542+marcoshernanz@users.noreply.github.com>
parent 0950e8ae
......@@ -68,12 +68,12 @@ async function initCacheEntries(
for (const cacheKey of cacheKeys) {
try {
const { expireAt, buffer } = await readFromCacheDir(cacheDir, cacheKey)
entries.push({
key: cacheKey,
size: buffer.byteLength,
expireAt,
})
const { expireAt, filePath } = await readCacheDirMetadata(
cacheDir,
cacheKey
)
const { size } = await promises.stat(filePath)
entries.push({ key: cacheKey, size, expireAt })
} catch {
// Skip entries that can't be read from disk
}
......@@ -189,7 +189,7 @@ async function writeToCacheDir(
await promises.writeFile(filename, buffer)
}
async function readFromCacheDir(cacheDir: string, cacheKey: string) {
async function readCacheDirMetadata(cacheDir: string, cacheKey: string) {
const dir = join(/* turbopackIgnore: true */ cacheDir, cacheKey)
const files = await promises.readdir(dir)
const file = files[0]
......@@ -202,11 +202,23 @@ async function readFromCacheDir(cacheDir: string, cacheKey: string) {
'.',
5
)
const filePath = join(/* turbopackIgnore: true */ dir, file)
return {
maxAge: Number(maxAgeSt),
expireAt: Number(expireAtSt),
etag,
upstreamEtag,
extension,
filePath: join(/* turbopackIgnore: true */ dir, file),
}
}
async function readFromCacheDir(cacheDir: string, cacheKey: string) {
const { filePath, ...metadata } = await readCacheDirMetadata(
cacheDir,
cacheKey
)
const buffer = await promises.readFile(/* turbopackIgnore: true */ filePath)
const expireAt = Number(expireAtSt)
const maxAge = Number(maxAgeSt)
return { maxAge, expireAt, etag, upstreamEtag, buffer, extension }
return { ...metadata, buffer }
}
async function deleteFromCacheDir(cacheDir: string, cacheKey: string) {
......
/* eslint-env jest */
import { promises } from 'fs'
import { join } from 'path'
import { tmpdir } from 'os'
import { ImageOptimizerCache } from 'next/dist/server/image-optimizer'
import { resetDiskLRU } from 'next/dist/server/lib/disk-lru-cache.external'
import { imageConfigDefault } from 'next/dist/shared/lib/image-config'
import type { NextConfigRuntime } from 'next/dist/server/config-shared'
async function writeEntry(
cacheDir: string,
key: string,
size: number,
expireAt: number
) {
const dir = join(cacheDir, key)
await promises.mkdir(dir, { recursive: true })
await promises.writeFile(
join(dir, `60.${expireAt}.etag.upstream.webp`),
Buffer.alloc(size, 0x42)
)
}
describe('image cache LRU initialization', () => {
let distDir: string
beforeEach(async () => {
resetDiskLRU()
distDir = await promises.mkdtemp(join(tmpdir(), 'next-image-cache-init-'))
})
afterEach(async () => {
jest.restoreAllMocks()
resetDiskLRU()
await promises.rm(distDir, { recursive: true, force: true })
})
it('uses file metadata to size existing entries without reading their bodies', async () => {
const cacheDir = join(distDir, 'cache', 'images')
const expireAt = Date.now() + 60_000
await writeEntry(cacheDir, 'old', 400, expireAt)
await writeEntry(cacheDir, 'new', 400, expireAt + 1)
const readFile = jest.spyOn(promises, 'readFile')
const cache = new ImageOptimizerCache({
distDir,
nextConfig: {
images: {
...imageConfigDefault,
maximumDiskCacheSize: 500,
},
experimental: {
isrFlushToDisk: true,
},
} as NextConfigRuntime,
})
const lru = await (
cache as unknown as {
cacheDiskLRU: Promise<{
has(key: string): boolean
}>
}
).cacheDiskLRU
expect(readFile).not.toHaveBeenCalled()
expect(lru.has('old')).toBe(false)
expect(lru.has('new')).toBe(true)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment