channel-cache.builder.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { Injectable } from '@nestjs/common';
  2. import { BaseCacheBuilder } from '@box/common/cache/cache-builder';
  3. import { tsCacheKeys } from '@box/common/cache/ts-cache-key.provider';
  4. import { RedisService } from '@box/db/redis/redis.service';
  5. import { MongoPrismaService } from '@box/db/prisma/mongo-prisma.service';
  6. export interface ChannelCachePayload {
  7. // id: string;
  8. channelId: string;
  9. // name: string;
  10. landingUrl: string;
  11. videoCdn?: string;
  12. coverCdn?: string;
  13. clientName?: string;
  14. clientNotice?: string;
  15. // remark?: string;
  16. }
  17. @Injectable()
  18. export class ChannelCacheBuilder extends BaseCacheBuilder {
  19. constructor(redis: RedisService, mongoPrisma: MongoPrismaService) {
  20. super(redis, mongoPrisma, ChannelCacheBuilder.name);
  21. }
  22. async buildAll(): Promise<void> {
  23. const channels = await this.mongoPrisma.channel.findMany({
  24. orderBy: [{ name: 'asc' }],
  25. });
  26. const payloads: ChannelCachePayload[] = channels.map((channel) => ({
  27. // id: channel.id,
  28. channelId: channel.channelId,
  29. // name: channel.name,
  30. landingUrl: channel.landingUrl,
  31. videoCdn: channel.videoCdn ?? undefined,
  32. coverCdn: channel.coverCdn ?? undefined,
  33. clientName: channel.clientName ?? undefined,
  34. clientNotice: channel.clientNotice ?? undefined,
  35. // remark: channel.remark ?? undefined,
  36. }));
  37. const entries: Array<{ key: string; value: unknown }> = payloads.map(
  38. (payload) => ({
  39. key: tsCacheKeys.channel.byId(payload.channelId),
  40. value: payload,
  41. }),
  42. );
  43. entries.push({ key: tsCacheKeys.channel.all(), value: payloads });
  44. await this.redis.pipelineSetJson(entries);
  45. this.logger.log(`Built ${payloads.length} channels`);
  46. }
  47. }