diff --git a/app/admin/photos/[photoId]/edit/page.tsx b/app/admin/photos/[photoId]/edit/page.tsx index cee0d533..d7a79b7c 100644 --- a/app/admin/photos/[photoId]/edit/page.tsx +++ b/app/admin/photos/[photoId]/edit/page.tsx @@ -1,12 +1,14 @@ import { redirect } from 'next/navigation'; import { - getAlbumsWithMetaCached, - getAlbumTitlesForPhotoCached, getPhotoNoStore, getUniqueFilmsCached, getUniqueRecipesCached, getUniqueTagsCached, } from '@/photo/cache'; +import { + getAlbumTitlesForPhotoCached, + getAlbumsWithMetaCached, +} from '@/album/cache'; import { PATH_ADMIN } from '@/app/path'; import PhotoEditPageClient from '@/photo/PhotoEditPageClient'; import { diff --git a/app/admin/uploads/page.tsx b/app/admin/uploads/page.tsx index 0f5d08b3..d3b7c302 100644 --- a/app/admin/uploads/page.tsx +++ b/app/admin/uploads/page.tsx @@ -1,6 +1,7 @@ import { getStorageUploadUrlsNoStore } from '@/platforms/storage/cache'; import AppGrid from '@/components/AppGrid'; -import { getUniqueTagsCached, getAlbumsWithMetaCached } from '@/photo/cache'; +import { getUniqueTagsCached } from '@/photo/cache'; +import { getAlbumsWithMetaCached } from '@/album/cache'; import AdminUploadsClient from '@/admin/AdminUploadsClient'; import { redirect } from 'next/navigation'; import { PATH_ADMIN_PHOTOS } from '@/app/path'; diff --git a/app/album/[album]/page.tsx b/app/album/[album]/page.tsx index f4a9747d..2e50b80a 100644 --- a/app/album/[album]/page.tsx +++ b/app/album/[album]/page.tsx @@ -1,5 +1,4 @@ import { INFINITE_SCROLL_GRID_INITIAL } from '@/photo'; -import { getPhotos } from '@/photo/query'; import { PATH_ROOT } from '@/app/path'; import type { Metadata } from 'next'; import { redirect } from 'next/navigation'; @@ -7,13 +6,14 @@ import { cache } from 'react'; import { staticallyGenerateCategoryIfConfigured } from '@/app/static'; import { getAppText } from '@/i18n/state/server'; import AlbumOverview from '@/album/AlbumOverview'; -import { - getAlbumFromSlug, - getAlbumsWithMeta, - getTagsForAlbum, -} from '@/album/query'; import { Album, generateMetaForAlbum } from '@/album'; import { getPhotosAlbumDataCached } from '@/album/data'; +import { + getAlbumFromSlugCached, + getAlbumsWithMetaCached, + getTagsForAlbumCached, +} from '@/album/cache'; +import { getPhotosCached } from '@/photo/cache'; const getPhotosAlbumDataCachedCached = cache((album: Album) => getPhotosAlbumDataCached({ album, limit: INFINITE_SCROLL_GRID_INITIAL})); @@ -21,7 +21,7 @@ const getPhotosAlbumDataCachedCached = cache((album: Album) => export const generateStaticParams = staticallyGenerateCategoryIfConfigured( 'albums', 'page', - getAlbumsWithMeta, + getAlbumsWithMetaCached, albums => albums.map(({ album }) => ({ album: album.slug })), ); @@ -36,7 +36,7 @@ export async function generateMetadata({ const albumSlug = decodeURIComponent(albumFromParams); - const album = await getAlbumFromSlug(albumSlug); + const album = await getAlbumFromSlugCached(albumSlug); if (!album) { return {}; } @@ -80,13 +80,13 @@ export default async function AlbumPage({ const albumSlug = decodeURIComponent(albumFromParams); - const album = await getAlbumFromSlug(albumSlug); + const album = await getAlbumFromSlugCached(albumSlug); if (!album) { redirect(PATH_ROOT); } - const photos = await getPhotos({ album }); + const photos = await getPhotosCached({ album }); - const tags = await getTagsForAlbum(album.id); + const tags = await getTagsForAlbumCached(album.id); return ( >; @@ -34,13 +34,13 @@ export const getAdminDataAction = async () => tagsCount, recipesCount, ] = await Promise.all([ - getPhotosMeta() + getPhotosMetaCached() .then(({ count }) => count) .catch(() => 0), - getPhotosMeta({ hidden: 'only' }) + getPhotosMetaCached({ hidden: 'only' }) .then(({ count }) => count) .catch(() => 0), - getPhotosInNeedOfUpdateCount(), + getPhotosInNeedOfUpdateCountCached(), getGitHubMetaForCurrentApp(), getStorageUploadUrlsNoStore() .then(urls => urls.length) @@ -48,13 +48,13 @@ export const getAdminDataAction = async () => console.error(`Error getting blob upload urls: ${e}`); return 0; }), - getAlbumsWithMeta() + getAlbumsWithMetaCached() .then(albums => albums.length) .catch(() => 0), - getUniqueTags() + getUniqueTagsCached() .then(tags => tags.length) .catch(() => 0), - getUniqueRecipes() + getUniqueRecipesCached() .then(recipes => recipes.length) .catch(() => 0), ]); diff --git a/src/admin/select/AdminBatchEditPanel.tsx b/src/admin/select/AdminBatchEditPanel.tsx index d6e2001d..20b6cb1f 100644 --- a/src/admin/select/AdminBatchEditPanel.tsx +++ b/src/admin/select/AdminBatchEditPanel.tsx @@ -1,4 +1,5 @@ -import { getAlbumsWithMetaCached, getUniqueTagsCached } from '@/photo/cache'; +import { getUniqueTagsCached } from '@/photo/cache'; +import { getAlbumsWithMetaCached } from '@/album/cache'; import AdminBatchEditPanelClient from './AdminBatchEditPanelClient'; export default async function AdminBatchEditPanel({ diff --git a/src/album/cache.ts b/src/album/cache.ts new file mode 100644 index 00000000..61af20d8 --- /dev/null +++ b/src/album/cache.ts @@ -0,0 +1,32 @@ +import { + getAlbumFromSlug, + getAlbumsWithMeta, + getAlbumTitlesForPhoto, + getTagsForAlbum, +} from '@/album/query'; +import { KEY_ALBUMS, KEY_PHOTOS } from '@/photo/cache'; +import { unstable_cache } from 'next/cache'; + +export const getAlbumFromSlugCached = + unstable_cache( + getAlbumFromSlug, + [KEY_PHOTOS, KEY_ALBUMS], + ); + +export const getAlbumTitlesForPhotoCached = + unstable_cache( + getAlbumTitlesForPhoto, + [KEY_PHOTOS, KEY_ALBUMS], + ); + +export const getAlbumsWithMetaCached = + unstable_cache( + getAlbumsWithMeta, + [KEY_PHOTOS, KEY_ALBUMS], + ); + +export const getTagsForAlbumCached = + unstable_cache( + getTagsForAlbum, + [KEY_PHOTOS, KEY_ALBUMS], + ); diff --git a/src/category/data.ts b/src/category/data.ts index 38a2a03e..d885f0fb 100644 --- a/src/category/data.ts +++ b/src/category/data.ts @@ -1,13 +1,3 @@ -import { - getPhotosMeta, - getUniqueCameras, - getUniqueFilms, - getUniqueFocalLengths, - getUniqueLenses, - getUniqueRecipes, - getUniqueTags, - getUniqueYears, -} from '@/photo/query'; import { SHOW_FILMS, SHOW_FOCAL_LENGTHS, @@ -23,7 +13,17 @@ import { createLensKey } from '@/lens'; import { sortTagsByCount } from '@/tag'; import { sortCategoriesByCount } from '@/category'; import { sortFocalLengths } from '@/focal'; -import { getAlbumsWithMeta } from '@/album/query'; +import { + getPhotosMetaCached, + getUniqueCamerasCached, + getUniqueFilmsCached, + getUniqueFocalLengthsCached, + getUniqueLensesCached, + getUniqueRecipesCached, + getUniqueTagsCached, + getUniqueYearsCached, +} from '@/photo/cache'; +import { getAlbumsWithMetaCached } from '@/album/cache'; type CategoryData = Awaited>; @@ -41,7 +41,7 @@ export const NULL_CATEGORY_DATA: CategoryData = { export const getDataForCategories = () => Promise.all([ SHOW_RECENTS - ? getPhotosMeta({ recent: true }) + ? getPhotosMetaCached({ recent: true }) .then(({ count, dateRange }) => count && dateRange ? [{ count, @@ -50,41 +50,41 @@ export const getDataForCategories = () => Promise.all([ .catch(() => []) : undefined, SHOW_YEARS - ? getUniqueYears() + ? getUniqueYearsCached() .catch(() => []) : undefined, SHOW_CAMERAS - ? getUniqueCameras() + ? getUniqueCamerasCached() .then(sortCategoriesByCount) .catch(() => []) : undefined, SHOW_LENSES - ? getUniqueLenses() + ? getUniqueLensesCached() .then(sortCategoriesByCount) .catch(() => []) : undefined, SHOW_TAGS - ? getUniqueTags() + ? getUniqueTagsCached() .then(sortTagsByCount) .catch(() => []) : undefined, SHOW_RECIPES - ? getUniqueRecipes() + ? getUniqueRecipesCached() .then(sortCategoriesByCount) .catch(() => []) : undefined, SHOW_FILMS - ? getUniqueFilms() + ? getUniqueFilmsCached() .then(sortCategoriesByCount) .catch(() => []) : undefined, SHOW_FOCAL_LENGTHS - ? getUniqueFocalLengths() + ? getUniqueFocalLengthsCached() .then(sortFocalLengths) .catch(() => []) : undefined, SHOW_ALBUMS - ? getAlbumsWithMeta() + ? getAlbumsWithMetaCached() .catch(() => []) : undefined, ]).then(([ diff --git a/src/photo/cache.ts b/src/photo/cache.ts index c6afd4bd..6d6e4684 100644 --- a/src/photo/cache.ts +++ b/src/photo/cache.ts @@ -17,6 +17,7 @@ import { getUniqueLenses, getUniqueRecipes, getUniqueYears, + getPhotosInNeedOfUpdateCount, } from '@/photo/query'; import { PhotoQueryOptions } from '@/db'; import { parseCachedPhotoDates, parseCachedPhotosDates } from '@/photo'; @@ -39,23 +40,22 @@ import { PREFIX_ALBUM, } from '@/app/path'; import { createLensKey } from '@/lens'; -import { getAlbumsWithMeta, getAlbumTitlesForPhoto } from '@/album/query'; // Table key -export const KEY_PHOTOS = 'photos'; -const KEY_PHOTO = 'photo'; +export const KEY_PHOTOS = 'photos'; +export const KEY_PHOTO = 'photo'; // Field keys -const KEY_CAMERAS = 'cameras'; -const KEY_LENSES = 'lenses'; -const KEY_ALBUMS = 'albums'; -const KEY_TAGS = 'tags'; -const KEY_FILMS = 'films'; -const KEY_RECIPES = 'recipes'; -const KEY_FOCAL_LENGTHS = 'focal-lengths'; -const KEY_YEARS = 'years'; +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 -const KEY_COUNT = 'count'; -const KEY_DATE_RANGE = 'date-range'; +export const KEY_COUNT = 'count'; +export const KEY_DATE_RANGE = 'date-range'; const getCacheKeyForPhotoQueryOptions = ( options: PhotoQueryOptions, @@ -73,7 +73,7 @@ const getCacheKeyForPhotoQueryOptions = ( } case 'album': { const album = options[option]; - return album ? album.slug : null; + return album?.id ? `${option}-${album.id}` : null; } case 'takenBefore': case 'takenAfterInclusive': @@ -225,16 +225,10 @@ export const getPhotoCached = (...args: Parameters) => [KEY_PHOTOS, KEY_PHOTO], )(...args).then(photo => photo ? parseCachedPhotoDates(photo) : undefined); -export const getAlbumTitlesForPhotoCached = +export const getPhotosInNeedOfUpdateCountCached = unstable_cache( - getAlbumTitlesForPhoto, - [KEY_PHOTOS, KEY_ALBUMS], - ); - -export const getAlbumsWithMetaCached = - unstable_cache( - getAlbumsWithMeta, - [KEY_PHOTOS, KEY_ALBUMS], + getPhotosInNeedOfUpdateCount, + [KEY_PHOTOS, KEY_COUNT], ); export const getUniqueTagsCached =