import { Injectable } from '@nestjs/common'; import { BaseCacheBuilder } from '@box/common/cache/cache-builder'; import { tsCacheKeys } from '@box/common/cache/ts-cache-key.provider'; import { RedisService } from '@box/db/redis/redis.service'; import { MongoPrismaService } from '@box/db/prisma/mongo-prisma.service'; export interface ChannelCachePayload { // id: string; channelId: string; // name: string; landingUrl: string; videoCdn?: string; coverCdn?: string; clientName?: string; clientNotice?: string; // remark?: string; } @Injectable() export class ChannelCacheBuilder extends BaseCacheBuilder { constructor(redis: RedisService, mongoPrisma: MongoPrismaService) { super(redis, mongoPrisma, ChannelCacheBuilder.name); } async buildAll(): Promise { const channels = await this.mongoPrisma.channel.findMany({ orderBy: [{ name: 'asc' }], }); const payloads: ChannelCachePayload[] = channels.map((channel) => ({ // id: channel.id, channelId: channel.channelId, // name: channel.name, landingUrl: channel.landingUrl, videoCdn: channel.videoCdn ?? undefined, coverCdn: channel.coverCdn ?? undefined, clientName: channel.clientName ?? undefined, clientNotice: channel.clientNotice ?? undefined, // remark: channel.remark ?? undefined, })); const entries: Array<{ key: string; value: unknown }> = payloads.map( (payload) => ({ key: tsCacheKeys.channel.byId(payload.channelId), value: payload, }), ); entries.push({ key: tsCacheKeys.channel.all(), value: payloads }); await this.redis.pipelineSetJson(entries); this.logger.log(`Built ${payloads.length} channels`); } }