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' ...@@ -6,7 +6,9 @@ import { LRUCache } from '../lru-cache'
import type { CacheEntry, CacheHandler } from './types' import type { CacheEntry, CacheHandler } from './types'
import { isTagStale, tagsManifest } from '../incremental-cache/tags-manifest' import { isTagStale, tagsManifest } from '../incremental-cache/tags-manifest'
type DefaultCacheEntry = CacheEntry & { type PrivateCacheEntry = {
entry: CacheEntry
// For the default cache we store errored cache // For the default cache we store errored cache
// entries and allow them to be used up to 3 times // entries and allow them to be used up to 3 times
// after that we want to dispose it and try for fresh // after that we want to dispose it and try for fresh
...@@ -16,30 +18,32 @@ type DefaultCacheEntry = CacheEntry & { ...@@ -16,30 +18,32 @@ type DefaultCacheEntry = CacheEntry & {
// and then if it still fails to set after the third we // and then if it still fails to set after the third we
// return the errored content and use expiration of // return the errored content and use expiration of
// Math.min(30, entry.expiration) // Math.min(30, entry.expiration)
isErrored?: boolean isErrored: boolean
errorRetryCount?: number errorRetryCount: number
// compute size on set since we need to read size // compute size on set since we need to read size
// of the ReadableStream for LRU evicting // of the ReadableStream for LRU evicting
size?: number size: number
} }
// LRU cache default to max 50 MB but in future track // 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>>() const pendingSets = new Map<string, Promise<void>>()
export const DefaultCacheHandler: CacheHandler = { const DefaultCacheHandler: CacheHandler = {
get: async function get(cacheKey, softTags) { async get(cacheKey, softTags) {
await pendingSets.get(cacheKey) await pendingSets.get(cacheKey)
if (isTagStale(...softTags)) { if (isTagStale(softTags)) {
return return
} }
const entry = memoryCache.get(cacheKey) const privateEntry = memoryCache.get(cacheKey)
if (!entry) { if (!privateEntry) {
return return undefined
} }
const entry = privateEntry.entry
if ( if (
performance.timeOrigin + performance.now() > performance.timeOrigin + performance.now() >
entry.timestamp + entry.revalidate * 1000 entry.timestamp + entry.revalidate * 1000
...@@ -49,7 +53,7 @@ export const DefaultCacheHandler: CacheHandler = { ...@@ -49,7 +53,7 @@ export const DefaultCacheHandler: CacheHandler = {
return return
} }
if (isTagStale(...(entry.tags || []))) { if (isTagStale(entry.tags || [])) {
return return
} }
const [returnStream, newSaved] = entry.value.tee() const [returnStream, newSaved] = entry.value.tee()
...@@ -61,41 +65,30 @@ export const DefaultCacheHandler: CacheHandler = { ...@@ -61,41 +65,30 @@ export const DefaultCacheHandler: CacheHandler = {
} }
}, },
set: async function set(cacheKey, entry) { async set(cacheKey, pendingEntry) {
let resolvePending: () => void = () => {} let resolvePending: () => void = () => {}
const pendingPromise = new Promise<void>((resolve) => { const pendingPromise = new Promise<void>((resolve) => {
resolvePending = resolve resolvePending = resolve
}) })
pendingSets.set(cacheKey, pendingPromise) pendingSets.set(cacheKey, pendingPromise)
const timestamp = performance.timeOrigin + performance.now() const entry = await pendingEntry
const {
value: originalValue,
revalidate,
tags,
expire,
stale,
} = await entry
let size = 0 let size = 0
let buffer = []
try { try {
const [value, clonedValue] = originalValue.tee() const [value, clonedValue] = entry.value.tee()
entry.value = value
const reader = clonedValue.getReader() const reader = clonedValue.getReader()
for (let chunk; !(chunk = await reader.read()).done; ) { for (let chunk; !(chunk = await reader.read()).done; ) {
size += Buffer.from(chunk.value).byteLength size += Buffer.from(chunk.value).byteLength
buffer.push(chunk.value)
} }
memoryCache.set(cacheKey, { memoryCache.set(cacheKey, {
value, entry,
revalidate, isErrored: false,
tags, errorRetryCount: 0,
expire,
stale,
timestamp,
size, size,
}) })
} catch (err) { } catch (err) {
...@@ -107,7 +100,7 @@ export const DefaultCacheHandler: CacheHandler = { ...@@ -107,7 +100,7 @@ export const DefaultCacheHandler: CacheHandler = {
} }
}, },
expireTags: async function expireTags(...tags) { async expireTags(tags) {
for (const tag of tags) { for (const tag of tags) {
if (!tagsManifest.items[tag]) { if (!tagsManifest.items[tag]) {
tagsManifest.items[tag] = {} tagsManifest.items[tag] = {}
...@@ -117,9 +110,9 @@ export const DefaultCacheHandler: CacheHandler = { ...@@ -117,9 +110,9 @@ export const DefaultCacheHandler: CacheHandler = {
} }
}, },
receiveExpiredTags: async function receiveExpiredTags( async receiveExpiredTags(tags): Promise<void> {
...tags return this.expireTags(tags)
): Promise<void> {
return this.expireTags(...tags)
}, },
} }
export default DefaultCacheHandler
...@@ -8,7 +8,7 @@ export const tagsManifest: TagsManifest = { ...@@ -8,7 +8,7 @@ export const tagsManifest: TagsManifest = {
items: {}, items: {},
} }
export const isTagStale = (...tags: string[]) => { export const isTagStale = (tags: string[]) => {
for (const tag of tags) { for (const tag of tags) {
const tagEntry = tagsManifest.items[tag] const tagEntry = tagsManifest.items[tag]
if ( if (
......
...@@ -32,7 +32,7 @@ import { ...@@ -32,7 +32,7 @@ import {
getServerModuleMap, getServerModuleMap,
} from '../app-render/encryption-utils' } from '../app-render/encryption-utils'
import type { CacheScopeStore } from '../async-storage/cache-scope.external' 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' import type { CacheHandler, CacheEntry } from '../lib/cache-handlers/types'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge' 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