From 2475a7e7c76945f1bf65506068a6fa1bfec3b9f3 Mon Sep 17 00:00:00 2001 From: Ike Saunders Date: Mon, 15 Apr 2024 17:44:13 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9D=20revert=20image=20caching=20idea?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/model/Image.ts | 57 +++---------------- .../update-image-heights.ts | 9 ++- 2 files changed, 12 insertions(+), 54 deletions(-) diff --git a/db/model/Image.ts b/db/model/Image.ts index f727479ff0e..1077b4498fe 100644 --- a/db/model/Image.ts +++ b/db/model/Image.ts @@ -33,43 +33,11 @@ import { } from "../../settings/serverSettings.js" import { KnexReadWriteTransaction, KnexReadonlyTransaction } from "../db.js" import { at } from "lodash" -import { ENV } from "../../settings/clientSettings.js" class ImageStore { - images: Record | undefined - hasInitialized: boolean = false - isFetchingFromGoogleDrive: boolean = false - lastFetchTime: number | undefined - - async init(): Promise { - // don't prefetch every image in development mode, wait for a specific request first - if (ENV === "development") return - // only one init at a time - while (this.isFetchingFromGoogleDrive) { - await new Promise((resolve) => setTimeout(resolve, 100)) - } - if (!this.hasInitialized) { - console.log("Initializing image store") - await this.fetchImageMetadata([]) - this.hasInitialized = true - } - return - } - async fetchImageMetadata( filenames: string[] ): Promise> { - if ( - this.images && - this.lastFetchTime && - Date.now() - this.lastFetchTime < 60 * 1000 - ) { - console.log( - `Skipping image metadata refresh since last fetch was less than a minute ago` - ) - return this.images - } - this.isFetchingFromGoogleDrive = true try { console.log( `Fetching image metadata from Google Drive ${ @@ -145,30 +113,25 @@ class ImageStore { ) if (duplicateFilenames.length) { - this.isFetchingFromGoogleDrive = false throw new Error( `Multiple images are named ${duplicateFilenames.join(", ")}` ) } - this.images = merge(this.images, keyBy(images, "filename")) console.log( `Fetched ${images.length} images' metadata from Google Drive` ) - return this.images + return keyBy(images, "filename") } catch (error) { console.error(`Error fetching image metadata`, error) throw error - } finally { - this.isFetchingFromGoogleDrive = false - this.lastFetchTime = Date.now() } } async syncImagesToS3( - knex: KnexReadWriteTransaction + knex: KnexReadWriteTransaction, + images: Record ): Promise<(Image | undefined)[]> { - const images = this.images if (!images) return [] return Promise.all( Object.keys(images).map((filename) => @@ -178,12 +141,7 @@ class ImageStore { } } -const _imageStore = new ImageStore() - -export async function getImageStore(): Promise { - await _imageStore.init() - return _imageStore -} +export const imageStore = new ImageStore() export const s3Client = new S3Client({ endpoint: IMAGE_HOSTING_R2_ENDPOINT, @@ -364,9 +322,10 @@ export async function fetchImagesFromDriveAndSyncToS3( ): Promise { if (!filenames.length) return [] - const imageStore = await getImageStore() - await imageStore.fetchImageMetadata(filenames) - const images = at(imageStore.images, filenames).filter(Boolean) + const images = await imageStore + .fetchImageMetadata(filenames) + .then((obj) => Object.values(obj)) + .then(excludeUndefined) return Promise.all(images.map((i) => Image.syncImage(knex, i))) .then(excludeUndefined) diff --git a/devTools/updateImageHeights/update-image-heights.ts b/devTools/updateImageHeights/update-image-heights.ts index 643e3357b4d..d58f89770e2 100644 --- a/devTools/updateImageHeights/update-image-heights.ts +++ b/devTools/updateImageHeights/update-image-heights.ts @@ -1,10 +1,9 @@ -import { getImageStore } from "../../db/model/Image.js" +import { imageStore } from "../../db/model/Image.js" import * as db from "../../db/db.js" import * as lodash from "lodash" import { exit } from "../../db/cleanup.js" async function updateImageHeights() { - const imageStore = await getImageStore() const transaction = await db.knexInstance().transaction() const filenames = await db .knexRaw<{ filename: string }>( @@ -16,10 +15,10 @@ async function updateImageHeights() { .then((rows) => rows.map((row) => row.filename)) console.log("Fetching image metadata...") - await imageStore.fetchImageMetadata([]) + const images = await imageStore.fetchImageMetadata([]) console.log("Fetching image metadata...done") - if (!imageStore.images) { + if (!images) { throw new Error("No images found") } @@ -29,7 +28,7 @@ async function updateImageHeights() { for (const batch of lodash.chunk(filenames, 20)) { const promises = [] for (const filename of batch) { - const image = imageStore.images[filename] + const image = images[filename] if (image && image.originalHeight) { promises.push( db.knexRaw(