Moved more functions to unstable_cache format

This commit is contained in:
Sam Becker 2023-11-15 09:34:57 -06:00
parent 10fd66591e
commit 9fedfd5149
6 changed files with 30 additions and 57 deletions

View File

@ -33,7 +33,7 @@ import IconGrSync from '@/site/IconGrSync';
const DEBUG_PHOTO_BLOBS = false;
export default async function AdminTagsPage({
export default async function AdminPhotosPage({
searchParams,
}: PaginationParams) {
const { offset, limit } = getPaginationForSearchParams(searchParams);

View File

@ -14,7 +14,7 @@ import { cc } from '@/utility/css';
export const runtime = 'edge';
export default async function AdminPhotosPage() {
export default async function AdminTagsPage() {
const tags = await getUniqueTagsHiddenCached();
return (

View File

@ -1,4 +1,4 @@
import { revalidatePhotosAndBlobKeys, revalidateAdminPaths } from '@/cache';
import { revalidateAdminPaths, revalidatePhotosKey } from '@/cache';
import { ACCEPTED_PHOTO_FILE_TYPES } from '@/photo';
import { isUploadPathnameValid } from '@/services/blob';
import { handleUpload, type HandleUploadBody } from '@vercel/blob/client';
@ -25,11 +25,11 @@ export async function POST(request: Request): Promise<NextResponse> {
},
// This argument is required, but doesn't seem to fire
onUploadCompleted: async () => {
revalidatePhotosAndBlobKeys();
revalidatePhotosKey();
revalidateAdminPaths();
},
});
revalidatePhotosAndBlobKeys();
revalidatePhotosKey();
revalidateAdminPaths();
return NextResponse.json(jsonResponse);
} catch (error) {

View File

@ -2,7 +2,7 @@ import BlobUrls from '@/admin/BlobUrls';
import { getBlobUploadUrlsNoStore } from '@/cache';
import SiteGrid from '@/components/SiteGrid';
export default async function UploadsPage() {
export default async function AdminUploadsPage() {
const blobUrls = await getBlobUploadUrlsNoStore();
return (
<SiteGrid

73
src/cache/index.ts vendored
View File

@ -28,15 +28,16 @@ import { Camera, createCameraKey } from '@/camera';
import { PATHS_ADMIN, PATHS_TO_CACHE } from '@/site/paths';
import { FilmSimulation } from '@/simulation';
// Table key
const KEY_PHOTOS = 'photos';
const KEY_PHOTOS_COUNT = `${KEY_PHOTOS}-count`;
const KEY_PHOTOS_DATE_RANGE = `${KEY_PHOTOS}-date-range`;
// Field keys
const KEY_TAGS = 'tags';
const KEY_CAMERAS = 'cameras';
const KEY_FILM_SIMULATIONS = 'film-simulations';
const KEY_BLOB = 'blob';
// Type keys
const KEY_COUNT = 'count';
const KEY_DATE_RANGE = 'date-range';
// eslint-disable-next-line max-len
const getPhotosCacheKeyForOption = (
options: GetPhotosOptions,
option: keyof GetPhotosOptions,
@ -81,23 +82,20 @@ const getPhotosCacheKeys = (options: GetPhotosOptions = {}) => {
const getPhotoCacheKey = (photoId: string) => `photo-${photoId}`;
const getPhotoTagCountKey = (tag: string) =>
`${KEY_PHOTOS_COUNT}-${KEY_TAGS}-${tag}`;
const getPhotoCameraCountKey = (camera: Camera) =>
`${KEY_PHOTOS_COUNT}-${KEY_CAMERAS}-${createCameraKey(camera)}`;
`${KEY_COUNT}-${KEY_CAMERAS}-${createCameraKey(camera)}`;
const getPhotoFilmSimulationCountKey = (simulation: FilmSimulation) =>
`${KEY_PHOTOS_COUNT}-${KEY_FILM_SIMULATIONS}-${simulation}`;
`${KEY_COUNT}-${KEY_FILM_SIMULATIONS}-${simulation}`;
const getPhotoTagDateRangeKey = (tag: string) =>
`${KEY_PHOTOS_DATE_RANGE}-${KEY_TAGS}-${tag}`;
`${KEY_DATE_RANGE}-${KEY_TAGS}-${tag}`;
const getPhotoCameraDateRangeKey = (camera: Camera) =>
`${KEY_PHOTOS_DATE_RANGE}-${KEY_CAMERAS}-${createCameraKey(camera)}`;
`${KEY_DATE_RANGE}-${KEY_CAMERAS}-${createCameraKey(camera)}`;
const getPhotoFilmSimulationDateRangeKey = (simulation: FilmSimulation) =>
`${KEY_PHOTOS_DATE_RANGE}-${KEY_FILM_SIMULATIONS}-${simulation}`;
`${KEY_DATE_RANGE}-${KEY_FILM_SIMULATIONS}-${simulation}`;
export const revalidatePhotosKey = () =>
revalidateTag(KEY_PHOTOS);
@ -111,16 +109,8 @@ export const revalidateCamerasKey = () =>
export const revalidateFilmSimulationsKey = () =>
revalidateTag(KEY_FILM_SIMULATIONS);
export const revalidateBlobKey = () =>
revalidateTag(KEY_BLOB);
export const revalidatePhotosAndBlobKeys = () => {
revalidatePhotosKey();
revalidateBlobKey();
};
export const revalidateAllKeys = () => {
revalidatePhotosAndBlobKeys();
revalidatePhotosKey();
revalidateTagsKey();
revalidateCamerasKey();
revalidateFilmSimulationsKey();
@ -135,36 +125,37 @@ export const revalidateAdminPaths = () => {
PATHS_ADMIN.forEach(path => revalidatePath(path));
};
// TODO: Test behavior
// Consider a wrapper function where this is executed at runtime
// and then parsed for dates
export const getPhotosCached: typeof getPhotos = (...args) =>
unstable_cache(
() => getPhotos(...args),
getPhotos,
[KEY_PHOTOS, ...getPhotosCacheKeys(...args)], {
tags: [KEY_PHOTOS, ...getPhotosCacheKeys(...args)],
}
)().then(parseCachedPhotosDates);
)(...args).then(parseCachedPhotosDates);
export const getPhotosCountCached =
unstable_cache(
(...args: Parameters<typeof getPhotosCount>) => getPhotosCount(...args),
[KEY_PHOTOS, KEY_PHOTOS_COUNT],
getPhotosCount,
[KEY_PHOTOS, KEY_COUNT],
);
export const getPhotosCountIncludingHiddenCached: typeof getPhotosCount =
(...args) =>
unstable_cache(
() => getPhotosCountIncludingHidden(...args),
[KEY_PHOTOS, KEY_PHOTOS_COUNT], {
tags: [KEY_PHOTOS, KEY_PHOTOS_COUNT],
[KEY_PHOTOS, KEY_COUNT], {
tags: [KEY_PHOTOS, KEY_COUNT],
}
)();
export const getPhotosTagCountCached: typeof getPhotosTagCount = (...args) =>
export const getPhotosTagCountCached =
unstable_cache(
() => getPhotosTagCount(...args),
[KEY_PHOTOS, getPhotoTagCountKey(...args)], {
tags: [KEY_PHOTOS, getPhotoTagCountKey(...args)],
}
)();
getPhotosTagCount,
[KEY_PHOTOS, KEY_TAGS],
);
// eslint-disable-next-line max-len
export const getPhotosCameraCountCached: typeof getPhotosCameraCount = (...args) =>
@ -253,27 +244,11 @@ export const getUniqueFilmSimulationsCached: typeof getUniqueFilmSimulations = (
}
)();
export const getBlobUploadUrlsCached: typeof getBlobUploadUrls = (...args) =>
unstable_cache(
() => getBlobUploadUrls(...args),
[KEY_BLOB, 'uploads'], {
tags: [KEY_BLOB, 'uploads'],
}
)();
export const getBlobUploadUrlsNoStore: typeof getBlobUploadUrls = (...args) => {
unstable_noStore();
return getBlobUploadUrls(...args);
};
export const getBlobPhotoUrlsCached: typeof getBlobPhotoUrls = (...args) =>
unstable_cache(
() => getBlobPhotoUrls(...args),
[KEY_BLOB, 'photos'], {
tags: [KEY_BLOB, 'photos'],
}
)();
export const getBlobPhotoUrlsNoStore: typeof getBlobPhotoUrls = (...args) => {
unstable_noStore();
return getBlobPhotoUrls(...args);

View File

@ -21,7 +21,6 @@ import {
import {
revalidateAdminPaths,
revalidateAllKeysAndPaths,
revalidateBlobKey,
revalidatePhotosKey,
} from '@/cache';
import { PATH_ADMIN_PHOTOS, PATH_ADMIN_TAGS } from '@/site/paths';
@ -84,7 +83,6 @@ export async function renamePhotoTagGloballyAction(formData: FormData) {
export async function deleteBlobPhotoAction(formData: FormData) {
await deleteBlobPhoto(formData.get('url') as string);
revalidateBlobKey();
revalidateAdminPaths();
if (formData.get('redirectToPhotos') === 'true') {