Improve video data fetching by prioritizing cached information

Refactor server-side video retrieval logic to first check local cache via `videoSyncService.getVideos` and `videoSyncService.getVideo`, falling back to `bunnyService.getVideo` only if cache misses. This change removes database merging logic from `videoSyncService` and integrates it into `BunnyStorage` when retrieving individual videos, optimizing data access.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 2cd2c0bc-434c-4bc9-ad3f-b99d3897a0d1
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/8cc42625-c1f5-4e43-99bd-77f2c4dedee2/2cd2c0bc-434c-4bc9-ad3f-b99d3897a0d1/DVlzRoR
This commit is contained in:
sebastjanartic
2025-09-04 06:35:54 +00:00
parent 3c7aef0fa5
commit fcb28e8c74
3 changed files with 35 additions and 53 deletions
+5 -41
View File
@@ -220,13 +220,13 @@ class VideoSyncService {
console.log('📅 Scheduled video sync every 5 minutes for optimal performance');
}
async getVideos(limit: number = 20, offset: number = 0, search?: string) {
let filteredVideos = await this.mergeWithDatabaseEdits(this.cache.videos);
getVideos(limit: number = 20, offset: number = 0, search?: string) {
let filteredVideos = this.cache.videos;
// Fast client-side search
if (search && search.length >= 2) {
const searchLower = search.toLowerCase();
filteredVideos = filteredVideos.filter(video =>
filteredVideos = this.cache.videos.filter(video =>
video.title.toLowerCase().includes(searchLower) ||
video.description?.toLowerCase().includes(searchLower)
);
@@ -242,52 +242,16 @@ class VideoSyncService {
};
}
private async mergeWithDatabaseEdits(bunnyVideos: any[]) {
try {
// Fetch all edited videos from database
const dbVideos = await db.select().from(videos);
const dbVideoMap = new Map(dbVideos.map(v => [v.id, v]));
// Merge Bunny.net data with database edits
return bunnyVideos.map(bunnyVideo => {
const dbVideo = dbVideoMap.get(bunnyVideo.id);
if (dbVideo) {
// Database has edited version - use edited metadata but keep Bunny.net URLs
return {
...bunnyVideo,
title: dbVideo.title,
description: dbVideo.description,
category: dbVideo.category,
tags: dbVideo.tags || [],
customThumbnailUrl: dbVideo.customThumbnailUrl,
isPublic: dbVideo.isPublic,
updatedAt: dbVideo.updatedAt
};
}
return bunnyVideo; // No database edits - use original Bunny.net data
});
} catch (error) {
console.warn('Failed to merge database edits, using Bunny.net data only:', error);
return bunnyVideos;
}
}
async getVideo(id: string) {
// First try cache
const cachedVideo = this.cache.videos.find(v => v.id === id);
if (cachedVideo) {
// Merge with database edits
const mergedVideos = await this.mergeWithDatabaseEdits([cachedVideo]);
return mergedVideos[0];
return cachedVideo;
}
// Fallback to direct API call
const bunnyVideo = await this.bunnyService.getVideo(id);
if (bunnyVideo) {
const mergedVideos = await this.mergeWithDatabaseEdits([bunnyVideo]);
return mergedVideos[0];
}
return bunnyVideo;
return await this.bunnyService.getVideo(id);
}
forceVideoRefresh(videoId: string) {