|
@@ -39,6 +39,7 @@ export interface RecommendedVideoItem {
|
|
|
export class RecommendedVideosCacheBuilder extends BaseCacheBuilder {
|
|
export class RecommendedVideosCacheBuilder extends BaseCacheBuilder {
|
|
|
private readonly RECOMMENDED_COUNT = 99;
|
|
private readonly RECOMMENDED_COUNT = 99;
|
|
|
private readonly CACHE_TTL = 3600; // 1 hour
|
|
private readonly CACHE_TTL = 3600; // 1 hour
|
|
|
|
|
+ private readonly LIST_BATCH_SIZE = 5000;
|
|
|
|
|
|
|
|
constructor(redis: RedisService, mongoPrisma: MongoPrismaService) {
|
|
constructor(redis: RedisService, mongoPrisma: MongoPrismaService) {
|
|
|
super(redis, mongoPrisma, RecommendedVideosCacheBuilder.name);
|
|
super(redis, mongoPrisma, RecommendedVideosCacheBuilder.name);
|
|
@@ -74,6 +75,8 @@ export class RecommendedVideosCacheBuilder extends BaseCacheBuilder {
|
|
|
this.CACHE_TTL,
|
|
this.CACHE_TTL,
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
|
|
+ await this.buildVideoListAll();
|
|
|
|
|
+
|
|
|
this.logger.log(
|
|
this.logger.log(
|
|
|
`Recommended videos cache built: ${items.length} videos stored`,
|
|
`Recommended videos cache built: ${items.length} videos stored`,
|
|
|
);
|
|
);
|
|
@@ -123,4 +126,33 @@ export class RecommendedVideosCacheBuilder extends BaseCacheBuilder {
|
|
|
getCacheKey(): string {
|
|
getCacheKey(): string {
|
|
|
return tsCacheKeys.video.recommended();
|
|
return tsCacheKeys.video.recommended();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private async buildVideoListAll(): Promise<void> {
|
|
|
|
|
+ this.logger.log('Building video list cache from all completed videos...');
|
|
|
|
|
+ const items: RecommendedVideoItem[] = [];
|
|
|
|
|
+ let lastId: string | null = null;
|
|
|
|
|
+
|
|
|
|
|
+ while (true) {
|
|
|
|
|
+ const batch = await this.mongoPrisma.videoMedia.findMany({
|
|
|
|
|
+ where: {
|
|
|
|
|
+ status: 'Completed',
|
|
|
|
|
+ ...(lastId ? { id: { gt: lastId } } : {}),
|
|
|
|
|
+ },
|
|
|
|
|
+ orderBy: { id: 'asc' },
|
|
|
|
|
+ take: this.LIST_BATCH_SIZE,
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (batch.length === 0) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ items.push(...batch.map((video) => this.mapVideoToItem(video)));
|
|
|
|
|
+ lastId = batch[batch.length - 1].id;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ await this.redis.setJson(tsCacheKeys.video.list(), items, this.CACHE_TTL);
|
|
|
|
|
+ this.logger.log(
|
|
|
|
|
+ `Video list cache built from ${items.length} completed videos`,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|