Rename cache 'tags' to 'keys' to reduce confusion

This commit is contained in:
Sam Becker 2023-10-05 11:43:03 -05:00
parent 3c913c273f
commit d30c8a14de
3 changed files with 82 additions and 79 deletions

View File

@ -1,4 +1,4 @@
import { revalidatePhotosAndBlobTag } from '@/cache'; import { revalidatePhotosAndBlobKeys } from '@/cache';
import { import {
ACCEPTED_PHOTO_FILE_TYPES, ACCEPTED_PHOTO_FILE_TYPES,
isUploadPathnameValid, isUploadPathnameValid,
@ -27,13 +27,13 @@ export async function POST(request: Request): Promise<NextResponse> {
}, },
// This argument is required, but doesn't seem to fire // This argument is required, but doesn't seem to fire
onUploadCompleted: async () => { onUploadCompleted: async () => {
revalidatePhotosAndBlobTag(); revalidatePhotosAndBlobKeys();
}, },
}); });
revalidatePhotosAndBlobTag(); revalidatePhotosAndBlobKeys();
return NextResponse.json(jsonResponse); return NextResponse.json(jsonResponse);
} catch (error) { } catch (error) {
revalidatePhotosAndBlobTag(); revalidatePhotosAndBlobKeys();
return NextResponse.json( return NextResponse.json(
{ error: (error as Error).message }, { error: (error as Error).message },
{ status: 400 }, { status: 400 },

135
src/cache/index.ts vendored
View File

@ -17,104 +17,107 @@ import { getBlobPhotoUrls, getBlobUploadUrls } from '@/services/blob';
import { AuthSession } from 'next-auth'; import { AuthSession } from 'next-auth';
import { Camera, createCameraKey } from '@/camera'; import { Camera, createCameraKey } from '@/camera';
const TAG_PHOTOS = 'photos'; const KEY_PHOTOS = 'photos';
const TAG_PHOTOS_COUNT = `${TAG_PHOTOS}-count`; const KEY_PHOTOS_COUNT = `${KEY_PHOTOS}-count`;
const TAG_PHOTOS_DATE_RANGE = `${TAG_PHOTOS}-date-range`; const KEY_PHOTOS_DATE_RANGE = `${KEY_PHOTOS}-date-range`;
const TAG_TAGS = 'tags'; const KEY_TAGS = 'tags';
const TAG_CAMERAS = 'cameras'; const KEY_CAMERAS = 'cameras';
const TAG_BLOB = 'blob'; const KEY_BLOB = 'blob';
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
const getPhotosCacheTagForKey = ( const getPhotosCacheKeyForOption = (
options: GetPhotosOptions, options: GetPhotosOptions,
key: keyof GetPhotosOptions, option: keyof GetPhotosOptions,
): string | null => { ): string | null => {
switch (key) { switch (option) {
// Primitive keys // Primitive keys
case 'sortBy': case 'sortBy':
case 'limit': case 'limit':
case 'offset': case 'offset':
case 'tag': case 'tag':
case 'includeHidden': { case 'includeHidden': {
const value = options[key]; const value = options[option];
return value ? `${key}-${value}` : null; return value ? `${option}-${value}` : null;
} }
// Date keys // Date keys
case 'takenBefore': case 'takenBefore':
case 'takenAfterInclusive': { case 'takenAfterInclusive': {
const value = options[key]; const value = options[option];
return value ? `${key}-${value.toISOString()}` : null; return value ? `${option}-${value.toISOString()}` : null;
} }
// Complex keys // Complex keys
case 'camera': { case 'camera': {
const value = options[key]; const value = options[option];
return value ? `${key}-${value.make}-${value.model}` : null; return value ? `${option}-${value.make}-${value.model}` : null;
} }
} }
}; };
const getPhotosCacheTags = (options: GetPhotosOptions = {}) => { const getPhotosCacheKeys = (options: GetPhotosOptions = {}) => {
const tags: string[] = []; const tags: string[] = [];
Object.keys(options).forEach(key => { Object.keys(options).forEach(key => {
const tag = getPhotosCacheTagForKey(options, key as keyof GetPhotosOptions); const tag = getPhotosCacheKeyForOption(
options,
key as keyof GetPhotosOptions
);
if (tag) { tags.push(tag); } if (tag) { tags.push(tag); }
}); });
return tags; return tags;
}; };
const getPhotoCacheTag = (photoId: string) => `photo-${photoId}`; const getPhotoCacheKey = (photoId: string) => `photo-${photoId}`;
const getPhotoTagCountTag = (tag: string) => const getPhotoTagCountKey = (tag: string) =>
`${TAG_PHOTOS_COUNT}-${TAG_TAGS}-${tag}`; `${KEY_PHOTOS_COUNT}-${KEY_TAGS}-${tag}`;
const getPhotoCameraCountTag = (camera: Camera) => const getPhotoCameraCountKey = (camera: Camera) =>
`${TAG_PHOTOS_COUNT}-${TAG_CAMERAS}-${createCameraKey(camera)}`; `${KEY_PHOTOS_COUNT}-${KEY_CAMERAS}-${createCameraKey(camera)}`;
const getPhotoTagDateRangeTag = (tag: string) => const getPhotoTagDateRangeKey = (tag: string) =>
`${TAG_PHOTOS_DATE_RANGE}-${TAG_TAGS}-${tag}`; `${KEY_PHOTOS_DATE_RANGE}-${KEY_TAGS}-${tag}`;
const getPhotoCameraDateRangeTag = (camera: Camera) => const getPhotoCameraDateRangeKey = (camera: Camera) =>
`${TAG_PHOTOS_DATE_RANGE}-${TAG_CAMERAS}-${createCameraKey(camera)}`; `${KEY_PHOTOS_DATE_RANGE}-${KEY_CAMERAS}-${createCameraKey(camera)}`;
export const revalidatePhotosTag = () => export const revalidatePhotosKey = () =>
revalidateTag(TAG_PHOTOS); revalidateTag(KEY_PHOTOS);
export const revalidateTagsTag = () => export const revalidateTagsKey = () =>
revalidateTag(TAG_TAGS); revalidateTag(KEY_TAGS);
export const revalidateCamerasTag = () => export const revalidateCamerasKey = () =>
revalidateTag(TAG_CAMERAS); revalidateTag(KEY_CAMERAS);
export const revalidateBlobTag = () => export const revalidateBlobKey = () =>
revalidateTag(TAG_BLOB); revalidateTag(KEY_BLOB);
export const revalidatePhotosAndBlobTag = () => { export const revalidatePhotosAndBlobKeys = () => {
revalidateTag(TAG_PHOTOS); revalidateTag(KEY_PHOTOS);
revalidateTag(TAG_BLOB); revalidateTag(KEY_BLOB);
}; };
export const revalidateAllTags = () => { export const revalidateAllKeys = () => {
revalidatePhotosTag(); revalidatePhotosKey();
revalidateTagsTag(); revalidateTagsKey();
revalidateCamerasTag(); revalidateCamerasKey();
revalidateBlobTag(); revalidateBlobKey();
}; };
export const getPhotosCached: typeof getPhotos = (...args) => export const getPhotosCached: typeof getPhotos = (...args) =>
unstable_cache( unstable_cache(
() => getPhotos(...args), () => getPhotos(...args),
[TAG_PHOTOS, ...getPhotosCacheTags(...args)], { [KEY_PHOTOS, ...getPhotosCacheKeys(...args)], {
tags: [TAG_PHOTOS, ...getPhotosCacheTags(...args)], tags: [KEY_PHOTOS, ...getPhotosCacheKeys(...args)],
} }
)().then(parseCachedPhotosDates); )().then(parseCachedPhotosDates);
export const getPhotosCountCached: typeof getPhotosCount = (...args) => export const getPhotosCountCached: typeof getPhotosCount = (...args) =>
unstable_cache( unstable_cache(
() => getPhotosCount(...args), () => getPhotosCount(...args),
[TAG_PHOTOS, TAG_PHOTOS_COUNT], { [KEY_PHOTOS, KEY_PHOTOS_COUNT], {
tags: [TAG_PHOTOS, TAG_PHOTOS_COUNT], tags: [KEY_PHOTOS, KEY_PHOTOS_COUNT],
} }
)(); )();
@ -122,16 +125,16 @@ export const getPhotosCountIncludingHiddenCached: typeof getPhotosCount =
(...args) => (...args) =>
unstable_cache( unstable_cache(
() => getPhotosCountIncludingHidden(...args), () => getPhotosCountIncludingHidden(...args),
[TAG_PHOTOS, TAG_PHOTOS_COUNT], { [KEY_PHOTOS, KEY_PHOTOS_COUNT], {
tags: [TAG_PHOTOS, TAG_PHOTOS_COUNT], tags: [KEY_PHOTOS, KEY_PHOTOS_COUNT],
} }
)(); )();
export const getPhotosTagCountCached: typeof getPhotosTagCount = (...args) => export const getPhotosTagCountCached: typeof getPhotosTagCount = (...args) =>
unstable_cache( unstable_cache(
() => getPhotosTagCount(...args), () => getPhotosTagCount(...args),
[TAG_PHOTOS, getPhotoTagCountTag(...args)], { [KEY_PHOTOS, getPhotoTagCountKey(...args)], {
tags: [TAG_PHOTOS, getPhotoTagCountTag(...args)], tags: [KEY_PHOTOS, getPhotoTagCountKey(...args)],
} }
)(); )();
@ -139,8 +142,8 @@ export const getPhotosTagCountCached: typeof getPhotosTagCount = (...args) =>
export const getPhotosCameraCountCached: typeof getPhotosCameraCount = (...args) => export const getPhotosCameraCountCached: typeof getPhotosCameraCount = (...args) =>
unstable_cache( unstable_cache(
() => getPhotosCameraCount(...args), () => getPhotosCameraCount(...args),
[TAG_PHOTOS, getPhotoCameraCountTag(...args)], { [KEY_PHOTOS, getPhotoCameraCountKey(...args)], {
tags: [TAG_PHOTOS, getPhotoCameraCountTag(...args)], tags: [KEY_PHOTOS, getPhotoCameraCountKey(...args)],
} }
)(); )();
@ -148,8 +151,8 @@ export const getPhotosCameraCountCached: typeof getPhotosCameraCount = (...args)
export const getPhotosTagDateRangeCached: typeof getPhotosTagDateRange = (...args) => export const getPhotosTagDateRangeCached: typeof getPhotosTagDateRange = (...args) =>
unstable_cache( unstable_cache(
() => getPhotosTagDateRange(...args), () => getPhotosTagDateRange(...args),
[TAG_PHOTOS, getPhotoTagDateRangeTag(...args)], { [KEY_PHOTOS, getPhotoTagDateRangeKey(...args)], {
tags: [TAG_PHOTOS, getPhotoTagDateRangeTag(...args)], tags: [KEY_PHOTOS, getPhotoTagDateRangeKey(...args)],
} }
)(); )();
@ -157,48 +160,48 @@ export const getPhotosTagDateRangeCached: typeof getPhotosTagDateRange = (...arg
export const getPhotosCameraDateRangeCached: typeof getPhotosCameraDateRange = (...args) => export const getPhotosCameraDateRangeCached: typeof getPhotosCameraDateRange = (...args) =>
unstable_cache( unstable_cache(
() => getPhotosCameraDateRange(...args), () => getPhotosCameraDateRange(...args),
[TAG_PHOTOS, getPhotoCameraDateRangeTag(...args)], { [KEY_PHOTOS, getPhotoCameraDateRangeKey(...args)], {
tags: [TAG_PHOTOS, getPhotoCameraDateRangeTag(...args)], tags: [KEY_PHOTOS, getPhotoCameraDateRangeKey(...args)],
} }
)(); )();
export const getPhotoCached: typeof getPhoto = (...args) => export const getPhotoCached: typeof getPhoto = (...args) =>
unstable_cache( unstable_cache(
() => getPhoto(...args), () => getPhoto(...args),
[TAG_PHOTOS, getPhotoCacheTag(...args)], { [KEY_PHOTOS, getPhotoCacheKey(...args)], {
tags: [TAG_PHOTOS, getPhotoCacheTag(...args)], tags: [KEY_PHOTOS, getPhotoCacheKey(...args)],
} }
)().then(photo => photo ? parseCachedPhotoDates(photo) : undefined); )().then(photo => photo ? parseCachedPhotoDates(photo) : undefined);
export const getUniqueTagsCached: typeof getUniqueTags = (...args) => export const getUniqueTagsCached: typeof getUniqueTags = (...args) =>
unstable_cache( unstable_cache(
() => getUniqueTags(...args), () => getUniqueTags(...args),
[TAG_PHOTOS, TAG_TAGS], { [KEY_PHOTOS, KEY_TAGS], {
tags: [TAG_PHOTOS, TAG_TAGS], tags: [KEY_PHOTOS, KEY_TAGS],
} }
)(); )();
export const getUniqueCamerasCached: typeof getUniqueCameras = (...args) => export const getUniqueCamerasCached: typeof getUniqueCameras = (...args) =>
unstable_cache( unstable_cache(
() => getUniqueCameras(...args), () => getUniqueCameras(...args),
[TAG_PHOTOS, TAG_CAMERAS], { [KEY_PHOTOS, KEY_CAMERAS], {
tags: [TAG_PHOTOS, TAG_CAMERAS], tags: [KEY_PHOTOS, KEY_CAMERAS],
} }
)(); )();
export const getBlobUploadUrlsCached: typeof getBlobUploadUrls = (...args) => export const getBlobUploadUrlsCached: typeof getBlobUploadUrls = (...args) =>
unstable_cache( unstable_cache(
() => getBlobUploadUrls(...args), () => getBlobUploadUrls(...args),
[TAG_BLOB], { [KEY_BLOB], {
tags: [TAG_BLOB], tags: [KEY_BLOB],
} }
)(); )();
export const getBlobPhotoUrlsCached: typeof getBlobPhotoUrls = (...args) => export const getBlobPhotoUrlsCached: typeof getBlobPhotoUrls = (...args) =>
unstable_cache( unstable_cache(
() => getBlobPhotoUrls(...args), () => getBlobPhotoUrls(...args),
[TAG_BLOB], { [KEY_BLOB], {
tags: [TAG_BLOB], tags: [KEY_BLOB],
} }
)(); )();

View File

@ -13,9 +13,9 @@ import {
deleteBlobPhoto, deleteBlobPhoto,
} from '@/services/blob'; } from '@/services/blob';
import { import {
revalidateAllTags, revalidateAllKeys,
revalidateBlobTag, revalidateBlobKey,
revalidatePhotosTag, revalidatePhotosKey,
} from '@/cache'; } from '@/cache';
import { IS_PRO_MODE } from '@/site/config'; import { IS_PRO_MODE } from '@/site/config';
import { getNextImageUrlForRequest } from '@/utility/image'; import { getNextImageUrlForRequest } from '@/utility/image';
@ -39,7 +39,7 @@ export async function createPhotoAction(formData: FormData) {
await sqlInsertPhoto(photo); await sqlInsertPhoto(photo);
revalidateAllTags(); revalidateAllKeys();
redirect('/admin/photos'); redirect('/admin/photos');
} }
@ -49,7 +49,7 @@ export async function updatePhotoAction(formData: FormData) {
await sqlUpdatePhoto(photo); await sqlUpdatePhoto(photo);
revalidatePhotosTag(); revalidatePhotosKey();
redirect('/admin/photos'); redirect('/admin/photos');
} }
@ -60,7 +60,7 @@ export async function deletePhotoAction(formData: FormData) {
sqlDeletePhoto(formData.get('id') as string), sqlDeletePhoto(formData.get('id') as string),
]); ]);
revalidatePhotosTag(); revalidatePhotosKey();
}; };
export async function deletePhotoTagGloballyAction(formData: FormData) { export async function deletePhotoTagGloballyAction(formData: FormData) {
@ -68,15 +68,15 @@ export async function deletePhotoTagGloballyAction(formData: FormData) {
await sqlDeletePhotoTagGlobally(tag); await sqlDeletePhotoTagGlobally(tag);
revalidatePhotosTag(); revalidatePhotosKey();
} }
export async function deleteBlobPhotoAction(formData: FormData) { export async function deleteBlobPhotoAction(formData: FormData) {
await deleteBlobPhoto(formData.get('url') as string); await deleteBlobPhoto(formData.get('url') as string);
revalidateBlobTag(); revalidateBlobKey();
}; };
export async function syncCacheAction() { export async function syncCacheAction() {
revalidateAllTags(); revalidateAllKeys();
} }