Recipe-app main
This commit is contained in:
Generated
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
import { createDefaultCacheHandler } from './default';
|
||||
/**
|
||||
* Used for edge runtime compatibility.
|
||||
*
|
||||
* @deprecated Use createDefaultCacheHandler instead.
|
||||
*/ export default createDefaultCacheHandler(50 * 1024 * 1024);
|
||||
|
||||
//# sourceMappingURL=default.external.js.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../../src/server/lib/cache-handlers/default.external.ts"],"sourcesContent":["import { createDefaultCacheHandler } from './default'\n\n/**\n * Used for edge runtime compatibility.\n *\n * @deprecated Use createDefaultCacheHandler instead.\n */\nexport default createDefaultCacheHandler(50 * 1024 * 1024)\n"],"names":["createDefaultCacheHandler"],"mappings":"AAAA,SAASA,yBAAyB,QAAQ,YAAW;AAErD;;;;CAIC,GACD,eAAeA,0BAA0B,KAAK,OAAO,MAAK","ignoreList":[0]}
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* This is the default "use cache" handler it defaults to an in-memory store.
|
||||
* In-memory caches are fragile and should not use stale-while-revalidate
|
||||
* semantics on the caches because it's not worth warming up an entry that's
|
||||
* likely going to get evicted before we get to use it anyway. However, we also
|
||||
* don't want to reuse a stale entry for too long so stale entries should be
|
||||
* considered expired/missing in such cache handlers.
|
||||
*/ import { LRUCache } from '../lru-cache';
|
||||
import { areTagsExpired, areTagsStale, tagsManifest } from '../incremental-cache/tags-manifest.external';
|
||||
export function createDefaultCacheHandler(maxSize) {
|
||||
// If the max size is 0, return a cache handler that doesn't cache anything,
|
||||
// this avoids an unnecessary LRUCache instance and potential memory
|
||||
// allocation.
|
||||
if (maxSize === 0) {
|
||||
return {
|
||||
get: ()=>Promise.resolve(undefined),
|
||||
set: ()=>Promise.resolve(),
|
||||
refreshTags: ()=>Promise.resolve(),
|
||||
getExpiration: ()=>Promise.resolve(0),
|
||||
updateTags: ()=>Promise.resolve()
|
||||
};
|
||||
}
|
||||
const memoryCache = new LRUCache(maxSize, (entry)=>entry.size);
|
||||
const pendingSets = new Map();
|
||||
const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE ? console.debug.bind(console, 'DefaultCacheHandler:') : undefined;
|
||||
return {
|
||||
async get (cacheKey) {
|
||||
const pendingPromise = pendingSets.get(cacheKey);
|
||||
if (pendingPromise) {
|
||||
debug == null ? void 0 : debug('get', cacheKey, 'pending');
|
||||
await pendingPromise;
|
||||
}
|
||||
const privateEntry = memoryCache.get(cacheKey);
|
||||
if (!privateEntry) {
|
||||
debug == null ? void 0 : debug('get', cacheKey, 'not found');
|
||||
return undefined;
|
||||
}
|
||||
const entry = privateEntry.entry;
|
||||
if (performance.timeOrigin + performance.now() > entry.timestamp + entry.revalidate * 1000) {
|
||||
// In-memory caches should expire after revalidate time because it is
|
||||
// unlikely that a new entry will be able to be used before it is dropped
|
||||
// from the cache.
|
||||
debug == null ? void 0 : debug('get', cacheKey, 'expired');
|
||||
return undefined;
|
||||
}
|
||||
let revalidate = entry.revalidate;
|
||||
if (areTagsExpired(entry.tags, entry.timestamp)) {
|
||||
debug == null ? void 0 : debug('get', cacheKey, 'had expired tag');
|
||||
return undefined;
|
||||
}
|
||||
if (areTagsStale(entry.tags, entry.timestamp)) {
|
||||
debug == null ? void 0 : debug('get', cacheKey, 'had stale tag');
|
||||
revalidate = -1;
|
||||
}
|
||||
const [returnStream, newSaved] = entry.value.tee();
|
||||
entry.value = newSaved;
|
||||
debug == null ? void 0 : debug('get', cacheKey, 'found', {
|
||||
tags: entry.tags,
|
||||
timestamp: entry.timestamp,
|
||||
expire: entry.expire,
|
||||
revalidate
|
||||
});
|
||||
return {
|
||||
...entry,
|
||||
revalidate,
|
||||
value: returnStream
|
||||
};
|
||||
},
|
||||
async set (cacheKey, pendingEntry) {
|
||||
debug == null ? void 0 : debug('set', cacheKey, 'start');
|
||||
let resolvePending = ()=>{};
|
||||
const pendingPromise = new Promise((resolve)=>{
|
||||
resolvePending = resolve;
|
||||
});
|
||||
pendingSets.set(cacheKey, pendingPromise);
|
||||
const entry = await pendingEntry;
|
||||
let size = 0;
|
||||
try {
|
||||
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;
|
||||
}
|
||||
memoryCache.set(cacheKey, {
|
||||
entry,
|
||||
isErrored: false,
|
||||
errorRetryCount: 0,
|
||||
size
|
||||
});
|
||||
debug == null ? void 0 : debug('set', cacheKey, 'done');
|
||||
} catch (err) {
|
||||
// TODO: store partial buffer with error after we retry 3 times
|
||||
debug == null ? void 0 : debug('set', cacheKey, 'failed', err);
|
||||
} finally{
|
||||
resolvePending();
|
||||
pendingSets.delete(cacheKey);
|
||||
}
|
||||
},
|
||||
async refreshTags () {
|
||||
// Nothing to do for an in-memory cache handler.
|
||||
},
|
||||
async getExpiration (tags) {
|
||||
const expirations = tags.map((tag)=>{
|
||||
const entry = tagsManifest.get(tag);
|
||||
if (!entry) return 0;
|
||||
// Return the most recent timestamp (either expired or stale)
|
||||
return entry.expired || 0;
|
||||
});
|
||||
const expiration = Math.max(...expirations, 0);
|
||||
debug == null ? void 0 : debug('getExpiration', {
|
||||
tags,
|
||||
expiration
|
||||
});
|
||||
return expiration;
|
||||
},
|
||||
async updateTags (tags, durations) {
|
||||
const now = Math.round(performance.timeOrigin + performance.now());
|
||||
debug == null ? void 0 : debug('updateTags', {
|
||||
tags,
|
||||
timestamp: now
|
||||
});
|
||||
for (const tag of tags){
|
||||
// TODO: update file-system-cache?
|
||||
const existingEntry = tagsManifest.get(tag) || {};
|
||||
if (durations) {
|
||||
// Use provided durations directly
|
||||
const updates = {
|
||||
...existingEntry
|
||||
};
|
||||
// mark as stale immediately
|
||||
updates.stale = now;
|
||||
if (durations.expire !== undefined) {
|
||||
updates.expired = now + durations.expire * 1000 // Convert seconds to ms
|
||||
;
|
||||
}
|
||||
tagsManifest.set(tag, updates);
|
||||
} else {
|
||||
// Update expired field for immediate expiration (default behavior when no durations provided)
|
||||
tagsManifest.set(tag, {
|
||||
...existingEntry,
|
||||
expired: now
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=default.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+5
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* A timestamp in milliseconds elapsed since the epoch
|
||||
*/ export { };
|
||||
|
||||
//# sourceMappingURL=types.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../../src/server/lib/cache-handlers/types.ts"],"sourcesContent":["/**\n * A timestamp in milliseconds elapsed since the epoch\n */\nexport type Timestamp = number\n\nexport interface CacheEntry {\n /**\n * The ReadableStream can error and only have partial data so any cache\n * handlers need to handle this case and decide to keep the partial cache\n * around or not.\n */\n value: ReadableStream<Uint8Array>\n\n /**\n * The tags configured for the entry excluding soft tags\n */\n tags: string[]\n\n /**\n * This is for the client, not used to calculate cache entry expiration\n * [duration in seconds]\n */\n stale: number\n\n /**\n * When the cache entry was created [timestamp in milliseconds]\n */\n timestamp: Timestamp\n\n /**\n * How long the entry is allowed to be used (should be longer than revalidate)\n * [duration in seconds]\n */\n expire: number\n\n /**\n * How long until the entry should be revalidated [duration in seconds]\n */\n revalidate: number\n}\n\nexport interface CacheHandler {\n /**\n * Retrieve a cache entry for the given cache key, if available. Will return\n * undefined if there's no valid entry, or if the given soft tags are stale.\n */\n get(cacheKey: string, softTags: string[]): Promise<undefined | CacheEntry>\n\n /**\n * Store a cache entry for the given cache key. When this is called, the entry\n * may still be pending, i.e. its value stream may still be written to. So it\n * needs to be awaited first. If a `get` for the same cache key is called,\n * before the pending entry is complete, the cache handler must wait for the\n * `set` operation to finish, before returning the entry, instead of returning\n * undefined.\n */\n set(cacheKey: string, pendingEntry: Promise<CacheEntry>): Promise<void>\n\n /**\n * This function may be called periodically, but always before starting a new\n * request. If applicable, it should communicate with the tags service to\n * refresh the local tags manifest accordingly.\n */\n refreshTags(): Promise<void>\n\n /**\n * This function is called for each set of soft tags that are relevant at the\n * start of a request. The result is the maximum timestamp of a revalidate\n * event for the tags. Returns `0` if none of the tags were ever revalidated.\n * Returns `Infinity` if the soft tags are supposed to be passed into the\n * `get` method instead to be checked for expiration.\n */\n getExpiration(tags: string[]): Promise<Timestamp>\n\n /**\n * This function is called when tags are revalidated/expired. If applicable,\n * it should update the tags manifest accordingly.\n */\n updateTags(tags: string[], durations?: { expire?: number }): Promise<void>\n}\n"],"names":[],"mappings":"AAAA;;CAEC,GAuCD,WAsCC","ignoreList":[0]}
|
||||
Reference in New Issue
Block a user