Explorar o código

feat(cache): implement video category and list cache builders with warmup services

Dave hai 2 meses
pai
achega
73dd214e81

+ 1 - 8
apps/box-mgnt-api/src/app.module.ts

@@ -16,9 +16,6 @@ import { MgntBackendModule } from './mgnt-backend/mgnt-backend.module';
 import pinoConfig from '@box/common/config/pino.config';
 import { CacheSyncModule } from './cache-sync/cache-sync.module';
 import { RedisModule } from '@box/db/redis/redis.module';
-import { AdPoolWarmupService } from './cache/adpool-warmup.service';
-import { VideoListWarmupService } from './cache/video-list-warmup.service';
-import { VideoCategoryWarmupService } from './cache/video-category-warmup.service';
 import { CoreModule } from '@box/core/core.module';
 
 @Module({
@@ -56,11 +53,7 @@ import { CoreModule } from '@box/core/core.module';
       http: process.env.NODE_ENV === 'development',
     }),
   ],
-  providers: [
-    AdPoolWarmupService,
-    VideoCategoryWarmupService,
-    VideoListWarmupService,
-  ],
+  providers: [],
 })
 export class AppModule implements OnModuleInit {
   onModuleInit() {

+ 2 - 1
apps/box-mgnt-api/src/cache/adpool-warmup.service.ts → libs/core/src/cache/adpool/adpool-warmup.service.ts

@@ -1,5 +1,5 @@
 import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
-import { AdPoolService } from '@box/core/ad/ad-pool.service';
+import { AdPoolService } from '../../ad/ad-pool.service';
 
 @Injectable()
 export class AdPoolWarmupService implements OnModuleInit {
@@ -9,6 +9,7 @@ export class AdPoolWarmupService implements OnModuleInit {
 
   async onModuleInit(): Promise<void> {
     try {
+      this.logger.log('Ad pool warmup starting...');
       await this.adPoolService.rebuildAllAdPools();
       this.logger.log('Ad pool warmup completed');
     } catch (err) {

+ 2 - 0
libs/core/src/cache/adpool/index.ts

@@ -0,0 +1,2 @@
+export * from '../../ad/ad-pool.builder';
+export * from './adpool-warmup.service';

+ 3 - 3
libs/core/src/cache/cache-manager.module.ts

@@ -12,9 +12,9 @@ import { TagWarmupService } from './tag/tag-warmup.service';
 import { ChannelCacheService } from './channel/channel-cache.service';
 import { ChannelCacheBuilder } from './channel/channel-cache.builder';
 import { ChannelWarmupService } from './channel/channel-warmup.service';
-import { VideoCategoryCacheBuilder } from './video/video-category-cache.builder';
-import { VideoCategoryWarmupService } from './video/video-category-warmup.service';
-import { VideoListCacheBuilder } from './video/video-list-cache.builder';
+import { VideoCategoryCacheBuilder } from './video/category/video-category-cache.builder';
+import { VideoCategoryWarmupService } from './video/category/video-category-warmup.service';
+import { VideoListCacheBuilder } from './video/list/video-list-cache.builder';
 
 @Module({
   providers: [

+ 2 - 0
libs/core/src/cache/video/category/index.ts

@@ -0,0 +1,2 @@
+export * from './video-category-cache.builder';
+export * from './video-category-warmup.service';

+ 0 - 0
libs/core/src/cache/video/video-category-cache.builder.ts → libs/core/src/cache/video/category/video-category-cache.builder.ts


+ 2 - 2
apps/box-mgnt-api/src/cache/video-category-warmup.service.ts → libs/core/src/cache/video/category/video-category-warmup.service.ts

@@ -1,6 +1,5 @@
-// apps/box-mgnt-api/src/cache/video-category-warmup.service.ts
 import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
-import { VideoCategoryCacheBuilder } from '@box/core/cache/video';
+import { VideoCategoryCacheBuilder } from './video-category-cache.builder';
 
 /**
  * Warmup service for video category and tag caches.
@@ -14,6 +13,7 @@ export class VideoCategoryWarmupService implements OnModuleInit {
 
   async onModuleInit(): Promise<void> {
     try {
+      this.logger.log('Video category/tag cache warmup starting...');
       await this.builder.buildAll();
       this.logger.log('Video category/tag cache warmup completed');
     } catch (err) {

+ 4 - 3
libs/core/src/cache/video/index.ts

@@ -2,9 +2,10 @@ export {
   VideoCategoryCacheBuilder,
   VideoCategoryPayload,
   VideoTagPayload,
-} from './video-category-cache.builder';
-export { VideoCategoryWarmupService } from './video-category-warmup.service';
+} from './category/video-category-cache.builder';
 export {
   VideoListCacheBuilder,
   VideoPoolPayload,
-} from './video-list-cache.builder';
+} from './list/video-list-cache.builder';
+export * from './list/video-list-warmup.service';
+export * from './category';

+ 0 - 0
libs/core/src/cache/video/video-list-cache.builder.ts → libs/core/src/cache/video/list/video-list-cache.builder.ts


+ 2 - 2
apps/box-mgnt-api/src/cache/video-list-warmup.service.ts → libs/core/src/cache/video/list/video-list-warmup.service.ts

@@ -1,6 +1,5 @@
-// apps/box-mgnt-api/src/cache/video-list-warmup.service.ts
 import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
-import { VideoListCacheBuilder } from '@box/core/cache/video';
+import { VideoListCacheBuilder } from './video-list-cache.builder';
 
 @Injectable()
 export class VideoListWarmupService implements OnModuleInit {
@@ -10,6 +9,7 @@ export class VideoListWarmupService implements OnModuleInit {
 
   async onModuleInit(): Promise<void> {
     try {
+      this.logger.log('Video list cache warmup starting...');
       await this.builder.buildAll();
       this.logger.log('Video list cache warmup completed');
     } catch (err) {

+ 0 - 19
libs/core/src/cache/video/video-category-warmup.service.ts

@@ -1,19 +0,0 @@
-// libs/core/src/cache/video/video-category-warmup.service.ts
-import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
-import { VideoCategoryCacheBuilder } from './video-category-cache.builder';
-
-@Injectable()
-export class VideoCategoryWarmupService implements OnModuleInit {
-  private readonly logger = new Logger(VideoCategoryWarmupService.name);
-
-  constructor(private readonly builder: VideoCategoryCacheBuilder) {}
-
-  async onModuleInit(): Promise<void> {
-    try {
-      await this.builder.buildAll();
-      this.logger.log('Video category cache warmup completed');
-    } catch (err) {
-      this.logger.error('Video category cache warmup failed', err);
-    }
-  }
-}

+ 27 - 2
libs/core/src/core.module.ts

@@ -1,12 +1,37 @@
+// lib/core/src/core.module.ts
 import { Module } from '@nestjs/common';
 import { MongoPrismaService } from '@box/db/prisma/mongo-prisma.service';
 import { AdPoolService } from './ad/ad-pool.service';
+import { AdPoolBuilder, AdPoolWarmupService } from './cache/adpool';
 import { CategoryCacheService } from './cache/category/category-cache.service';
 import { CacheManagerModule } from './cache/cache-manager.module';
+import {
+  VideoCategoryCacheBuilder,
+  VideoCategoryWarmupService,
+  VideoListCacheBuilder,
+  VideoListWarmupService,
+} from './cache/video';
 
 @Module({
   imports: [CacheManagerModule],
-  providers: [MongoPrismaService, AdPoolService, CategoryCacheService],
-  exports: [AdPoolService, CategoryCacheService, CacheManagerModule],
+  providers: [
+    MongoPrismaService,
+    AdPoolService,
+    CategoryCacheService,
+    VideoCategoryCacheBuilder,
+    VideoCategoryWarmupService,
+    VideoListCacheBuilder,
+    VideoListWarmupService,
+    AdPoolBuilder,
+    AdPoolWarmupService,
+  ],
+  exports: [
+    AdPoolService,
+    CategoryCacheService,
+    CacheManagerModule,
+    VideoCategoryWarmupService,
+    VideoListWarmupService,
+    AdPoolWarmupService,
+  ],
 })
 export class CoreModule {}