Remove dedicated count/date queries

This commit is contained in:
Sam Becker 2024-05-20 11:25:17 -05:00
parent 305bf21915
commit 58f52343dd
8 changed files with 28 additions and 61 deletions

View File

@ -1,6 +1,6 @@
import { getStorageUploadUrlsNoStore } from '@/services/storage/cache';
import {
getPhotosCountIncludingHiddenCached,
getPhotosMetaCached,
getPhotosMostRecentUpdateCached,
getUniqueTagsCached,
} from '@/photo/cache';
@ -18,7 +18,9 @@ export default async function AdminNav() {
countTags,
mostRecentPhotoUpdateTime,
] = await Promise.all([
getPhotosCountIncludingHiddenCached().catch(() => 0),
getPhotosMetaCached({ hidden: 'include' })
.then(({ count }) => count)
.catch(() => 0),
getStorageUploadUrlsNoStore()
.then(urls => urls.length)
.catch(e => {

View File

@ -1,7 +1,6 @@
import PhotoUpload from '@/photo/PhotoUpload';
import { clsx } from 'clsx/lite';
import SiteGrid from '@/components/SiteGrid';
import { getPhotosCountIncludingHiddenCached } from '@/photo/cache';
import AdminUploadsTable from '@/admin/AdminUploadsTable';
import { PRO_MODE_ENABLED } from '@/site/config';
import { getStoragePhotoUrlsNoStore } from '@/services/storage/cache';
@ -10,6 +9,7 @@ import { revalidatePath } from 'next/cache';
import AdminPhotosTable from '@/admin/AdminPhotosTable';
import AdminPhotosTableInfinite from
'@/admin/AdminPhotosTableInfinite';
import { getPhotosMetaCached } from '@/photo/cache';
const DEBUG_PHOTO_BLOBS = false;
@ -27,7 +27,9 @@ export default async function AdminPhotosPage() {
sortBy: 'createdAt',
limit: INFINITE_SCROLL_INITIAL_ADMIN_PHOTOS,
}).catch(() => []),
getPhotosCountIncludingHiddenCached().catch(() => 0),
getPhotosMetaCached({ hidden: 'include'})
.then(({ count }) => count)
.catch(() => 0),
DEBUG_PHOTO_BLOBS
? getStoragePhotoUrlsNoStore()
: [],

View File

@ -2,7 +2,8 @@ import {
INFINITE_SCROLL_GRID_PHOTO_INITIAL,
INFINITE_SCROLL_GRID_PHOTO_MULTIPLE,
} from '@/photo';
import { getPhotosCached, getPhotosCountCached } from '@/photo/cache';
import { getPhotosCached } from '@/photo/cache';
import { getPhotosMeta } from '@/photo/db';
import StaggeredOgPhotos from '@/photo/StaggeredOgPhotos';
import StaggeredOgPhotosInfinite from '@/photo/StaggeredOgPhotosInfinite';
@ -12,7 +13,9 @@ export default async function GridPage() {
count,
] = await Promise.all([
getPhotosCached({ limit: INFINITE_SCROLL_GRID_PHOTO_INITIAL }),
getPhotosCountCached(),
getPhotosMeta()
.then(({ count }) => count)
.catch(() => 0),
]);
return (

View File

@ -8,7 +8,7 @@ import { Metadata } from 'next/types';
import { MAX_PHOTOS_TO_SHOW_OG } from '@/image-response';
import PhotosLarge from '@/photo/PhotosLarge';
import { cache } from 'react';
import { getPhotos, getPhotosCount } from '@/photo/db';
import { getPhotos, getPhotosMeta } from '@/photo/db';
import PhotosLargeInfinite from '@/photo/PhotosLargeInfinite';
export const dynamic = 'force-static';
@ -32,7 +32,8 @@ export default async function HomePage() {
limit: INFINITE_SCROLL_LARGE_PHOTO_INITIAL,
})
.catch(() => []),
getPhotosCount()
getPhotosMeta()
.then(({ count }) => count)
.catch(() => 0),
]);

View File

@ -8,13 +8,10 @@ import {
GetPhotosOptions,
getPhoto,
getPhotos,
getPhotosCount,
getPhotosCountIncludingHidden,
getUniqueCameras,
getUniqueTags,
getUniqueTagsHidden,
getUniqueFilmSimulations,
getPhotosDateRange,
getPhotosNearId,
getPhotosMostRecentUpdate,
getPhotosMeta,
@ -163,24 +160,6 @@ export const getPhotosMetaCached = (
[KEY_PHOTOS, KEY_COUNT, KEY_DATE_RANGE, ...getPhotosCacheKeys(...args)],
)(...args);
export const getPhotosDateRangeCached =
unstable_cache(
getPhotosDateRange,
[KEY_PHOTOS, KEY_DATE_RANGE],
);
export const getPhotosCountCached =
unstable_cache(
getPhotosCount,
[KEY_PHOTOS, KEY_COUNT],
);
export const getPhotosCountIncludingHiddenCached =
unstable_cache(
getPhotosCountIncludingHidden,
[KEY_PHOTOS, KEY_COUNT, KEY_HIDDEN],
);
export const getPhotosMostRecentUpdateCached =
unstable_cache(
() => getPhotosMostRecentUpdate(),

View File

@ -1,11 +1,11 @@
import {
getPhotosCountCached,
getPhotosMetaCached,
getUniqueCamerasCached,
getUniqueFilmSimulationsCached,
getUniqueTagsCached,
} from '@/photo/cache';
import {
getPhotosCount,
getPhotosMeta,
getUniqueCameras,
getUniqueFilmSimulations,
getUniqueTags,
@ -14,7 +14,9 @@ import { SHOW_FILM_SIMULATIONS } from '@/site/config';
import { sortTagsObject } from '@/tag';
export const getPhotoSidebarData = () => [
getPhotosCount().catch(() => 0),
getPhotosMeta()
.then(({ count }) => count)
.catch(() => 0),
getUniqueTags().then(sortTagsObject).catch(() => []),
getUniqueCameras().catch(() => []),
SHOW_FILM_SIMULATIONS
@ -23,7 +25,9 @@ export const getPhotoSidebarData = () => [
] as const;
export const getPhotoSidebarDataCached = () => [
getPhotosCountCached(),
getPhotosMetaCached()
.then(({ count }) => count)
.catch(() => 0),
getUniqueTagsCached().then(sortTagsObject),
getUniqueCamerasCached(),
SHOW_FILM_SIMULATIONS ? getUniqueFilmSimulationsCached() : [],

View File

@ -178,27 +178,10 @@ const sqlGetPhoto = (id: string, includeHidden?: boolean) => includeHidden
// eslint-disable-next-line max-len
: sql<PhotoDb>`SELECT * FROM photos WHERE id=${id} AND hidden IS NOT TRUE LIMIT 1`;
const sqlGetPhotosCount = async () => sql`
SELECT COUNT(*) FROM photos
WHERE hidden IS NOT TRUE
`.then(({ rows }) => parseInt(rows[0].count, 10));
const sqlGetPhotosCountIncludingHidden = async () => sql`
SELECT COUNT(*) FROM photos
`.then(({ rows }) => parseInt(rows[0].count, 10));
const sqlGetPhotosMostRecentUpdate = async () => sql`
SELECT updated_at FROM photos ORDER BY updated_at DESC LIMIT 1
`.then(({ rows }) => rows[0] ? rows[0].updated_at as Date : undefined);
const sqlGetPhotosDateRange = async () => sql`
SELECT MIN(taken_at_naive) as start, MAX(taken_at_naive) as end
FROM photos
WHERE hidden IS NOT TRUE
`.then(({ rows }) => rows[0]?.start && rows[0]?.end
? rows[0] as PhotoDateRange
: undefined);
const sqlGetUniqueTags = async () => sql`
SELECT DISTINCT unnest(tags) as tag, COUNT(*)
FROM photos
@ -510,15 +493,6 @@ export const getPhoto = async (
.then(({ rows }) => rows.map(parsePhotoFromDb))
.then(photos => photos.length > 0 ? photos[0] : undefined);
};
export const getPhotosDateRange = () =>
safelyQueryPhotos(sqlGetPhotosDateRange, 'getPhotosDateRange');
export const getPhotosCount = () =>
safelyQueryPhotos(sqlGetPhotosCount, 'getPhotosCount');
export const getPhotosCountIncludingHidden = () =>
safelyQueryPhotos(
sqlGetPhotosCountIncludingHidden,
'getPhotosCountIncludingHidden',
);
export const getPhotosMostRecentUpdate = () =>
safelyQueryPhotos(
sqlGetPhotosMostRecentUpdate,

View File

@ -1,6 +1,6 @@
import CommandKClient, { CommandKSection } from '@/components/CommandKClient';
import {
getPhotosCountCached,
getPhotosMetaCached,
getUniqueCamerasCached,
getUniqueFilmSimulationsCached,
getUniqueTagsCached,
@ -24,7 +24,9 @@ export default async function CommandK() {
cameras,
filmSimulations,
] = await Promise.all([
getPhotosCountCached().catch(() => 0),
getPhotosMetaCached()
.then(({ count }) => count)
.catch(() => 0),
getUniqueTagsCached().catch(() => [] as TagsWithMeta),
getUniqueCamerasCached().catch(() => []),
SHOW_FILM_SIMULATIONS