Create top-level cache module

This commit is contained in:
Sam Becker 2025-11-19 09:16:23 -06:00
parent 5482cb4123
commit ab0ddeee94
8 changed files with 100 additions and 73 deletions

View File

@ -1,5 +1,5 @@
import { auth } from '@/auth/server';
import { revalidateAdminPaths, revalidatePhotosKey } from '@/photo/cache';
import { revalidateAdminPaths, revalidatePhotosKey } from '@/cache';
import {
ACCEPTED_PHOTO_FILE_TYPES,
MAX_PHOTO_UPLOAD_SIZE_IN_BYTES,

View File

@ -2,7 +2,7 @@
import { runAuthenticatedAdminServerAction } from '@/auth/server';
import { addPhotoAlbumIds, deleteAlbum, updateAlbum } from './query';
import { revalidateAllKeysAndPaths } from '@/photo/cache';
import { revalidateAllKeysAndPaths } from '@/cache';
import { redirect } from 'next/navigation';
import { PATH_ADMIN_ALBUMS, PATH_ROOT, pathForAlbum } from '@/app/path';
import { convertFormDataToAlbum } from './form';

View File

@ -4,7 +4,7 @@ import {
getAlbumTitlesForPhoto,
getTagsForAlbum,
} from '@/album/query';
import { KEY_ALBUMS, KEY_PHOTOS } from '@/photo/cache';
import { KEY_ALBUMS, KEY_PHOTOS } from '@/cache';
import { unstable_cache } from 'next/cache';
export const getAlbumFromSlugCached =

7
src/cache/actions.ts vendored Normal file
View File

@ -0,0 +1,7 @@
'use server';
import { runAuthenticatedAdminServerAction } from '@/auth/server';
import { revalidateAllKeysAndPaths } from '.';
export const revalidateAllKeysAndPathsAction = async () =>
runAuthenticatedAdminServerAction(revalidateAllKeysAndPaths);

66
src/cache/index.ts vendored Normal file
View File

@ -0,0 +1,66 @@
import { PATHS_ADMIN, PATHS_TO_CACHE } from '@/app/path';
import { revalidatePath, revalidateTag } from 'next/cache';
// Table key
export const KEY_PHOTOS = 'photos';
export const KEY_PHOTO = 'photo';
// Field keys
export const KEY_CAMERAS = 'cameras';
export const KEY_LENSES = 'lenses';
export const KEY_ALBUMS = 'albums';
export const KEY_TAGS = 'tags';
export const KEY_FILMS = 'films';
export const KEY_RECIPES = 'recipes';
export const KEY_FOCAL_LENGTHS = 'focal-lengths';
export const KEY_YEARS = 'years';
// Type keys
export const KEY_COUNT = 'count';
export const KEY_DATE_RANGE = 'date-range';
export const revalidatePhotosKey = () =>
revalidateTag(KEY_PHOTOS, 'max');
export const revalidateAlbumsKey = () =>
revalidateTag(KEY_ALBUMS, 'max');
export const revalidateTagsKey = () =>
revalidateTag(KEY_TAGS, 'max');
export const revalidateRecipesKey = () =>
revalidateTag(KEY_RECIPES, 'max');
export const revalidateCamerasKey = () =>
revalidateTag(KEY_CAMERAS, 'max');
export const revalidateLensesKey = () =>
revalidateTag(KEY_LENSES, 'max');
export const revalidateFilmsKey = () =>
revalidateTag(KEY_FILMS, 'max');
export const revalidateFocalLengthsKey = () =>
revalidateTag(KEY_FOCAL_LENGTHS, 'max');
export const revalidateYearsKey = () =>
revalidateTag(KEY_YEARS, 'max');
export const revalidateAllKeys = () => {
revalidatePhotosKey();
revalidateAlbumsKey();
revalidateTagsKey();
revalidateCamerasKey();
revalidateLensesKey();
revalidateFilmsKey();
revalidateRecipesKey();
revalidateFocalLengthsKey();
revalidateYearsKey();
};
export const revalidateAdminPaths = () => {
PATHS_ADMIN.forEach(path => revalidatePath(path));
};
export const revalidateAllKeysAndPaths = () => {
revalidateAllKeys();
PATHS_TO_CACHE.forEach(path => revalidatePath(path, 'layout'));
};

View File

@ -1,6 +1,6 @@
import { unstable_cache } from 'next/cache';
import { getCountsForCategories, getDataForCategories } from './data';
import { KEY_PHOTOS } from '@/photo/cache';
import { KEY_PHOTOS } from '@/cache';
export const getDataForCategoriesCached = unstable_cache(
getDataForCategories,

View File

@ -24,14 +24,13 @@ import {
import { redirect } from 'next/navigation';
import { deleteFile } from '@/platforms/storage';
import {
getPhotosCached,
revalidateAdminPaths,
revalidateAllKeysAndPaths,
revalidatePhoto,
revalidatePhotosKey,
revalidateRecipesKey,
revalidateTagsKey,
} from '@/photo/cache';
} from '@/cache';
import { revalidatePhoto, getPhotosCached } from './cache';
import {
PATH_ADMIN_PHOTOS,
PATH_ADMIN_RECIPES,

View File

@ -23,8 +23,6 @@ import { PhotoQueryOptions } from '@/db';
import { parseCachedPhotoDates, parseCachedPhotosDates } from '@/photo';
import { createCameraKey } from '@/camera';
import {
PATHS_ADMIN,
PATHS_TO_CACHE,
PATH_ADMIN,
PATH_FULL,
PATH_GRID,
@ -40,22 +38,27 @@ import {
PREFIX_ALBUM,
} from '@/app/path';
import { createLensKey } from '@/lens';
// Table key
export const KEY_PHOTOS = 'photos';
export const KEY_PHOTO = 'photo';
// Field keys
export const KEY_CAMERAS = 'cameras';
export const KEY_LENSES = 'lenses';
export const KEY_ALBUMS = 'albums';
export const KEY_TAGS = 'tags';
export const KEY_FILMS = 'films';
export const KEY_RECIPES = 'recipes';
export const KEY_FOCAL_LENGTHS = 'focal-lengths';
export const KEY_YEARS = 'years';
// Type keys
export const KEY_COUNT = 'count';
export const KEY_DATE_RANGE = 'date-range';
import {
KEY_PHOTOS,
KEY_PHOTO,
KEY_CAMERAS,
KEY_LENSES,
KEY_TAGS,
KEY_FILMS,
KEY_RECIPES,
KEY_FOCAL_LENGTHS,
KEY_YEARS,
KEY_COUNT,
KEY_DATE_RANGE,
revalidateYearsKey,
revalidateCamerasKey,
revalidateLensesKey,
revalidateAlbumsKey,
revalidateTagsKey,
revalidateFilmsKey,
revalidateRecipesKey,
revalidateFocalLengthsKey,
} from '@/cache';
const getCacheKeyForPhotoQueryOptions = (
options: PhotoQueryOptions,
@ -102,54 +105,6 @@ const getPhotosCacheKeys = (options: PhotoQueryOptions = {}) => {
return tags;
};
export const revalidatePhotosKey = () =>
revalidateTag(KEY_PHOTOS, 'max');
export const revalidateAlbumsKey = () =>
revalidateTag(KEY_ALBUMS, 'max');
export const revalidateTagsKey = () =>
revalidateTag(KEY_TAGS, 'max');
export const revalidateRecipesKey = () =>
revalidateTag(KEY_RECIPES, 'max');
export const revalidateCamerasKey = () =>
revalidateTag(KEY_CAMERAS, 'max');
export const revalidateLensesKey = () =>
revalidateTag(KEY_LENSES, 'max');
export const revalidateFilmsKey = () =>
revalidateTag(KEY_FILMS, 'max');
export const revalidateFocalLengthsKey = () =>
revalidateTag(KEY_FOCAL_LENGTHS, 'max');
export const revalidateYearsKey = () =>
revalidateTag(KEY_YEARS, 'max');
export const revalidateAllKeys = () => {
revalidatePhotosKey();
revalidateAlbumsKey();
revalidateTagsKey();
revalidateCamerasKey();
revalidateLensesKey();
revalidateFilmsKey();
revalidateRecipesKey();
revalidateFocalLengthsKey();
revalidateYearsKey();
};
export const revalidateAdminPaths = () => {
PATHS_ADMIN.forEach(path => revalidatePath(path));
};
export const revalidateAllKeysAndPaths = () => {
revalidateAllKeys();
PATHS_TO_CACHE.forEach(path => revalidatePath(path, 'layout'));
};
export const revalidatePhoto = (photoId: string) => {
// Tags
revalidateTag(photoId, 'max');