|
| 1 | +import type { ComposableCacheEntry, ComposableCacheHandler } from "types/cache"; |
| 2 | +import { fromReadableStream, toReadableStream } from "utils/stream"; |
| 3 | +import { debug } from "./logger"; |
| 4 | + |
| 5 | +export default { |
| 6 | + async get(cacheKey: string) { |
| 7 | + try { |
| 8 | + const result = await globalThis.incrementalCache.get( |
| 9 | + cacheKey, |
| 10 | + "composable", |
| 11 | + ); |
| 12 | + if (!result?.value?.value) { |
| 13 | + return undefined; |
| 14 | + } |
| 15 | + |
| 16 | + debug("composable cache result", result); |
| 17 | + |
| 18 | + // We need to check if the tags associated with this entry has been revalidated |
| 19 | + if ( |
| 20 | + globalThis.tagCache.mode === "nextMode" && |
| 21 | + result.value.tags.length > 0 |
| 22 | + ) { |
| 23 | + const hasBeenRevalidated = await globalThis.tagCache.hasBeenRevalidated( |
| 24 | + result.value.tags, |
| 25 | + result.lastModified, |
| 26 | + ); |
| 27 | + if (hasBeenRevalidated) return undefined; |
| 28 | + } else if ( |
| 29 | + globalThis.tagCache.mode === "original" || |
| 30 | + globalThis.tagCache.mode === undefined |
| 31 | + ) { |
| 32 | + const hasBeenRevalidated = |
| 33 | + (await globalThis.tagCache.getLastModified( |
| 34 | + cacheKey, |
| 35 | + result.lastModified, |
| 36 | + )) === -1; |
| 37 | + if (hasBeenRevalidated) return undefined; |
| 38 | + } |
| 39 | + |
| 40 | + return { |
| 41 | + ...result.value, |
| 42 | + value: toReadableStream(result.value.value), |
| 43 | + }; |
| 44 | + } catch (e) { |
| 45 | + debug("Cannot read composable cache entry"); |
| 46 | + return undefined; |
| 47 | + } |
| 48 | + }, |
| 49 | + |
| 50 | + async set(cacheKey: string, pendingEntry: Promise<ComposableCacheEntry>) { |
| 51 | + const entry = await pendingEntry; |
| 52 | + const valueToStore = await fromReadableStream(entry.value); |
| 53 | + await globalThis.incrementalCache.set( |
| 54 | + cacheKey, |
| 55 | + { |
| 56 | + ...entry, |
| 57 | + value: valueToStore, |
| 58 | + }, |
| 59 | + "composable", |
| 60 | + ); |
| 61 | + if (globalThis.tagCache.mode === "original") { |
| 62 | + const storedTags = await globalThis.tagCache.getByPath(cacheKey); |
| 63 | + const tagsToWrite = entry.tags.filter((tag) => !storedTags.includes(tag)); |
| 64 | + if (tagsToWrite.length > 0) { |
| 65 | + await globalThis.tagCache.writeTags( |
| 66 | + tagsToWrite.map((tag) => ({ tag, path: cacheKey })), |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | + }, |
| 71 | + |
| 72 | + async refreshTags() { |
| 73 | + // We don't do anything for now, do we want to do something here ??? |
| 74 | + return; |
| 75 | + }, |
| 76 | + async getExpiration(...tags: string[]) { |
| 77 | + if (globalThis.tagCache.mode === "nextMode") { |
| 78 | + return globalThis.tagCache.getLastRevalidated(tags); |
| 79 | + } |
| 80 | + // We always return 0 here, original tag cache are handled directly in the get part |
| 81 | + // TODO: We need to test this more, i'm not entirely sure that this is working as expected |
| 82 | + return 0; |
| 83 | + }, |
| 84 | + async expireTags(...tags: string[]) { |
| 85 | + if (globalThis.tagCache.mode === "nextMode") { |
| 86 | + return globalThis.tagCache.writeTags(tags); |
| 87 | + } |
| 88 | + const tagCache = globalThis.tagCache; |
| 89 | + const revalidatedAt = Date.now(); |
| 90 | + // For the original mode, we have more work to do here. |
| 91 | + // We need to find all paths linked to to these tags |
| 92 | + const pathsToUpdate = await Promise.all( |
| 93 | + tags.map(async (tag) => { |
| 94 | + const paths = await tagCache.getByTag(tag); |
| 95 | + return paths.map((path) => ({ |
| 96 | + path, |
| 97 | + tag, |
| 98 | + revalidatedAt, |
| 99 | + })); |
| 100 | + }), |
| 101 | + ); |
| 102 | + // We need to deduplicate paths, we use a set for that |
| 103 | + const setToWrite = new Set<{ path: string; tag: string }>(); |
| 104 | + for (const entry of pathsToUpdate.flat()) { |
| 105 | + setToWrite.add(entry); |
| 106 | + } |
| 107 | + await globalThis.tagCache.writeTags(Array.from(setToWrite)); |
| 108 | + }, |
| 109 | + |
| 110 | + // This one is necessary for older versions of next |
| 111 | + async receiveExpiredTags(...tags: string[]) { |
| 112 | + // This function does absolutely nothing |
| 113 | + return; |
| 114 | + }, |
| 115 | +} satisfies ComposableCacheHandler; |
0 commit comments