Unverified Commit 29343e26 authored by Sebastian Markbåge's avatar Sebastian Markbåge Committed by GitHub

Follow up to default cache handler (#71504)

Some minor optimizations:

- While spreading tags is convenient and is nice if it's inline. It does
require some cloning and dynamism. So it's faster if we pass the same
array around in internal apis instead of cloning at each level. It also
lets us add new options to those APIs later.
- Export the default cache handler as the "default" export since that's
what it would look like for third parties if they want to be required.
So then it becomes possible to specify the default the same as a third
party.
- Use fixed hidden class for cache entries and other fixes
- No need to buffer the chunks. It can add up memory since we have
multiple copies in memory at any given time with the current naive
stream teeing. We can just GC the copies immediately.
- To avoid hidden class deopts we should avoid optional fields and avoid
adding additional fields to existing objects. It also makes it easier to
follow and avoid clashes when the same name is added both to the public
and private objects.
parent ff40d0a2
......@@ -6,7 +6,9 @@ import { LRUCache } from '../lru-cache'
import type { CacheEntry, CacheHandler } from './types'
import { isTagStale, tagsManifest } from '../incremental-cache/tags-manifest'
type DefaultCacheEntry = CacheEntry & {
type PrivateCacheEntry = {
entry: CacheEntry
// For the default cache we store errored cache
// entries and allow them to be used up to 3 times
// after that we want to dispose it and try for fresh
......@@ -16,30 +18,32 @@ type DefaultCacheEntry = CacheEntry & {
// and then if it still fails to set after the third we
// return the errored content and use expiration of
// Math.min(30, entry.expiration)
isErrored?: boolean
errorRetryCount?: number
isErrored: boolean
errorRetryCount: number
// compute size on set since we need to read size
// of the ReadableStream for LRU evicting
size?: number
size: number
}
// LRU cache default to max 50 MB but in future track
const memoryCache = new LRUCache<DefaultCacheEntry>(50_000_000)
const memoryCache = new LRUCache<PrivateCacheEntry>(50_000_000)
const pendingSets = new Map<string, Promise<void>>()
export const DefaultCacheHandler: CacheHandler = {
get: async function get(cacheKey, softTags) {
const DefaultCacheHandler: CacheHandler = {
async get(cacheKey, softTags) {
await pendingSets.get(cacheKey)
if (isTagStale(...softTags)) {
if (isTagStale(softTags)) {
return
}
const entry = memoryCache.get(cacheKey)
const privateEntry = memoryCache.get(cacheKey)
if (!entry) {
return
if (!privateEntry) {
return undefined
}
const entry = privateEntry.entry
if (
performance.timeOrigin + performance.now() >
entry.timestamp + entry.revalidate * 1000
......@@ -49,7 +53,7 @@ export const DefaultCacheHandler: CacheHandler = {
return
}
if (isTagStale(...(entry.tags || []))) {
if (isTagStale(entry.tags || [])) {
return
}
const [returnStream, newSaved] = entry.value.tee()
......@@ -61,41 +65,30 @@ export const DefaultCacheHandler: CacheHandler = {
}
},
set: async function set(cacheKey, entry) {
async set(cacheKey, pendingEntry) {
let resolvePending: () => void = () => {}
const pendingPromise = new Promise<void>((resolve) => {
resolvePending = resolve
})
pendingSets.set(cacheKey, pendingPromise)
const timestamp = performance.timeOrigin + performance.now()
const {
value: originalValue,
revalidate,
tags,
expire,
stale,
} = await entry
const entry = await pendingEntry
let size = 0
let buffer = []
try {
const [value, clonedValue] = originalValue.tee()
const [value, clonedValue] = entry.value.tee()
entry.value = value
const reader = clonedValue.getReader()
for (let chunk; !(chunk = await reader.read()).done; ) {
size += Buffer.from(chunk.value).byteLength
buffer.push(chunk.value)
}
memoryCache.set(cacheKey, {
value,
revalidate,
tags,
expire,
stale,
timestamp,
entry,
isErrored: false,
errorRetryCount: 0,
size,
})
} catch (err) {
......@@ -107,7 +100,7 @@ export const DefaultCacheHandler: CacheHandler = {
}
},
expireTags: async function expireTags(...tags) {
async expireTags(tags) {
for (const tag of tags) {
if (!tagsManifest.items[tag]) {
tagsManifest.items[tag] = {}
......@@ -117,9 +110,9 @@ export const DefaultCacheHandler: CacheHandler = {
}
},
receiveExpiredTags: async function receiveExpiredTags(
...tags
): Promise<void> {
return this.expireTags(...tags)
async receiveExpiredTags(tags): Promise<void> {
return this.expireTags(tags)
},
}
export default DefaultCacheHandler
......@@ -8,7 +8,7 @@ export const tagsManifest: TagsManifest = {
items: {},
}
export const isTagStale = (...tags: string[]) => {
export const isTagStale = (tags: string[]) => {
for (const tag of tags) {
const tagEntry = tagsManifest.items[tag]
if (
......
......@@ -32,7 +32,7 @@ import {
getServerModuleMap,
} from '../app-render/encryption-utils'
import type { CacheScopeStore } from '../async-storage/cache-scope.external'
import { DefaultCacheHandler } from '../lib/cache-handlers/default'
import DefaultCacheHandler from '../lib/cache-handlers/default'
import type { CacheHandler, CacheEntry } from '../lib/cache-handlers/types'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
......
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