Explorar el Código

refactor: update cache key prefixes to use 'box' namespace and clean up unused code in AdPoolService

Dave hace 1 mes
padre
commit
3b4715391e
Se han modificado 2 ficheros con 37 adiciones y 55 borrados
  1. 29 36
      libs/common/src/cache/cache-keys.ts
  2. 8 19
      libs/core/src/ad/ad-pool.service.ts

+ 29 - 36
libs/common/src/cache/cache-keys.ts

@@ -13,99 +13,92 @@ export type VideoHomeSectionKey = 'featured' | 'latest' | 'editorPick';
 
 export const CacheKeys = {
   // ─────────────────────────────────────────────
-  // CHANNELS (existing)
+  // CHANNELS
   // ─────────────────────────────────────────────
-  appChannelAll: 'app:channel:all',
+  appChannelAll: 'box:app:channel:all',
   appChannelById: (channelId: string | number): string =>
-    `app:channel:by-id:${channelId}`,
+    `box:app:channel:by-id:${channelId}`,
 
-  // NEW: Channel → Categories tree (future use)
   appChannelWithCategories: (channelId: string | number): string =>
-    `app:channel:with-categories:${channelId}`,
+    `box:app:channel:with-categories:${channelId}`,
 
   // ─────────────────────────────────────────────
-  // CATEGORIES (existing)
+  // CATEGORIES
   // ─────────────────────────────────────────────
   appCategory: (categoryId: string | number): string =>
-    `app:category:${categoryId}`,
-  appCategoryAll: 'app:category:all',
+    `box:app:category:${categoryId}`,
+  appCategoryAll: 'box:app:category:all',
   appCategoryById: (categoryId: string | number): string =>
-    `app:category:by-id:${categoryId}`,
+    `box:app:category:by-id:${categoryId}`,
 
-  // NEW: Category → Tags tree (main for video listing)
   appCategoryWithTags: (categoryId: string | number): string =>
-    `app:category:with-tags:${categoryId}`,
+    `box:app:category:with-tags:${categoryId}`,
 
   // ─────────────────────────────────────────────
-  // TAGS (new)
+  // TAGS
   // ─────────────────────────────────────────────
-  appTagAll: 'app:tag:all',
+  appTagAll: 'box:app:tag:all',
 
   appTagByCategoryKey: (categoryId: string | number): string =>
-    `app:tag:list:${categoryId}`,
+    `box:app:tag:list:${categoryId}`,
 
   // ─────────────────────────────────────────────
-  // ADS (existing)
+  // ADS
   // ─────────────────────────────────────────────
-  appAdById: (adId: string | number): string => `app:ad:by-id:${adId}`,
+  appAdById: (adId: string | number): string => `box:app:ad:by-id:${adId}`,
 
   // ─────────────────────────────────────────────
   // AD POOLS (AdType-based)
   // ─────────────────────────────────────────────
-  /** Build the canonical ad pool key for a given AdType. */
   appAdPoolByType: (adType: AdType | string): string =>
     `box:app:adpool:${adType}`,
 
   // ─────────────────────────────────────────────
-  // VIDEO LISTS (existing)
+  // VIDEO LISTS
   // ─────────────────────────────────────────────
-  appHomeVideoPage: (page: number): string => `app:videolist:home:page:${page}`,
+  appHomeVideoPage: (page: number): string =>
+    `box:app:videolist:home:page:${page}`,
 
   appChannelVideoPage: (channelId: string | number, page: number): string =>
-    `app:videolist:channel:${channelId}:page:${page}`,
-
-  // appTrendingVideoPage: (countryCode: string, page: number): string =>
-  //   `app:videolist:trending:${countryCode}:page:${page}`,
+    `box:app:videolist:channel:${channelId}:page:${page}`,
 
   // ─────────────────────────────────────────────
   // VIDEO DETAILS & METADATA
   // ─────────────────────────────────────────────
-  appVideoDetailKey: (videoId: string): string => `app:video:detail:${videoId}`,
+  appVideoDetailKey: (videoId: string): string =>
+    `box:app:video:detail:${videoId}`,
 
   appVideoPayloadKey: (videoId: string): string =>
-    `app:video:payload:${videoId}`,
+    `box:app:video:payload:${videoId}`,
 
   appVideoCategoryListKey: (categoryId: string): string =>
-    `app:video:category:list:${categoryId}`,
+    `box:app:video:category:list:${categoryId}`,
 
   appVideoTagListKey: (categoryId: string, tagId: string): string =>
-    `app:video:tag:list:${categoryId}:${tagId}`,
+    `box:app:video:tag:list:${categoryId}:${tagId}`,
 
   // ─────────────────────────────────────────────
-  // VIDEO POOLS (sorted listings with scores)
+  // VIDEO POOLS
   // ─────────────────────────────────────────────
-
   appVideoCategoryPoolKey: (
     channelId: string,
     categoryId: string,
     sort: VideoSortKey,
-  ): string => `app:video:list:category:${channelId}:${categoryId}:${sort}`,
+  ): string => `box:app:video:list:category:${channelId}:${categoryId}:${sort}`,
 
   appVideoTagPoolKey: (
     channelId: string,
     tagId: string,
     sort: VideoSortKey,
-  ): string => `app:video:list:tag:${channelId}:${tagId}:${sort}`,
+  ): string => `box:app:video:list:tag:${channelId}:${tagId}:${sort}`,
 
   appVideoHomeSectionKey: (
     channelId: string,
     section: VideoHomeSectionKey,
-  ): string => `app:video:list:home:${channelId}:${section}`,
+  ): string => `box:app:video:list:home:${channelId}:${section}`,
 
   // ─────────────────────────────────────────────
-  // RECOMMENDED VIDEOS (Homepage)
+  // RECOMMENDED VIDEOS
   // ─────────────────────────────────────────────
-
-  /** Recommended videos for homepage (7 random completed videos) */
-  appRecommendedVideos: 'app:video:recommended',
+  appRecommendedVideos: 'box:app:video:recommended',
 };

+ 8 - 19
libs/core/src/ad/ad-pool.service.ts

@@ -1,5 +1,5 @@
 // libs/core/src/ad/ad-pool.service.ts
-import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
+import { Injectable, Logger } from '@nestjs/common';
 import { randomUUID } from 'crypto';
 import { tsCacheKeys } from '@box/common/cache/ts-cache-key.provider';
 import type { AdPoolEntry, AdType } from '@box/common/ads/ad-types';
@@ -12,18 +12,7 @@ export type AdPayload = AdPoolEntry & {
 };
 
 @Injectable()
-export class AdPoolService implements OnModuleInit {
-  async onModuleInit(): Promise<void> {
-    // 1) write a fingerprint key
-    await this.redis.set(
-      'debug:redis:fingerprint',
-      JSON.stringify({
-        from: AdPoolService.name,
-        ts: Date.now(),
-      }),
-    );
-  }
-
+export class AdPoolService {
   private readonly logger = new Logger(AdPoolService.name);
 
   constructor(
@@ -87,9 +76,9 @@ export class AdPoolService implements OnModuleInit {
       },
     });
 
-    this.logger.log(
-      `AAAAAA Rebuilding ad pool for adType=${adType}, count=${ads.length}`,
-    );
+    // this.logger.log(
+    //   `Rebuilding ad pool for adType=${adType}, count=${ads.length}`,
+    // );
 
     const payloads: AdPayload[] = ads.map((ad) => ({
       id: ad.id,
@@ -106,9 +95,9 @@ export class AdPoolService implements OnModuleInit {
     }));
 
     const key = tsCacheKeys.ad.poolByType(adType);
-    this.logger.log(
-      `Writing ${payloads.length} ads to Redis key=${key} payloads=${JSON.stringify(payloads).slice(0, 100)}`,
-    );
+    // this.logger.log(
+    //   `Writing ${payloads.length} ads to Redis key=${key} payloads=${JSON.stringify(payloads).slice(0, 100)}`,
+    // );
 
     await this.redis.atomicSwapJson([{ key, value: payloads }]);