import { LatestVideosCacheBuilder } from '@box/core/cache/video/latest/latest-videos-cache.builder'; import { RecommendedVideosCacheBuilder } from '@box/core/cache/video/recommended/recommended-videos-cache.builder'; import { TagCacheBuilder } from '@box/core/cache/tag/tag-cache.builder'; import { CategoryCacheBuilder } from '@box/core/cache/category/category-cache.builder'; import { ChannelCacheBuilder } from '@box/core/cache/channel/channel-cache.builder'; import { AdPoolBuilder } from '@box/core/ad/ad-pool.builder'; export type RedisCacheCode = | 'VIDEO_LATEST' | 'VIDEO_RECOMMENDED' | 'TAG_ALL' | 'CATEGORY_ALL' | 'CATEGORY_BY_ID' | 'CHANNEL_ALL' | 'CHANNEL_BY_ID' | 'ADPOOL'; export interface RedisCacheRebuildHandler { cacheCode: RedisCacheCode; label: string; description?: string; rebuild: () => Promise<{ ok: boolean; message?: string; affected?: number }>; } interface RedisCacheRegistryDeps { latestVideosCacheBuilder: LatestVideosCacheBuilder; recommendedVideosCacheBuilder: RecommendedVideosCacheBuilder; tagCacheBuilder: TagCacheBuilder; categoryCacheBuilder: CategoryCacheBuilder; channelCacheBuilder: ChannelCacheBuilder; adPoolBuilder: AdPoolBuilder; } export function getRedisCacheRegistry( deps: RedisCacheRegistryDeps, ): RedisCacheRebuildHandler[] { return [ { cacheCode: 'VIDEO_LATEST', label: 'Latest Videos Cache', description: 'Rebuilds the latest video feed cache.', rebuild: async () => { await deps.latestVideosCacheBuilder.buildAll(); return { ok: true, message: 'Latest videos cache rebuilt', }; }, }, { cacheCode: 'VIDEO_RECOMMENDED', label: 'Recommended Videos Cache', description: 'Rebuilds the homepage recommended video cache.', rebuild: async () => { await deps.recommendedVideosCacheBuilder.buildAll(); return { ok: true, message: 'Recommended videos cache rebuilt', }; }, }, { cacheCode: 'TAG_ALL', label: 'Tag Cache', description: 'Rebuilds the global tag metadata cache.', rebuild: async () => { await deps.tagCacheBuilder.buildAll(); return { ok: true, message: 'Tag cache rebuilt' }; }, }, { cacheCode: 'CATEGORY_ALL', label: 'Category Cache (all)', description: 'Rebuilds the full category listing cache.', rebuild: async () => { await deps.categoryCacheBuilder.buildAll(); return { ok: true, message: 'Category cache rebuilt' }; }, }, { cacheCode: 'CHANNEL_ALL', label: 'Channel Cache (all)', description: 'Rebuilds the complete channel cache.', rebuild: async () => { await deps.channelCacheBuilder.buildAll(); return { ok: true, message: 'Channel cache rebuilt' }; }, }, { cacheCode: 'ADPOOL', label: 'Ad Pools', description: 'Rebuilds all ad pools.', rebuild: async () => { await deps.adPoolBuilder.buildAll(); return { ok: true, message: 'Ad pools rebuilt' }; }, }, ]; } export interface RedisCacheKeyAllowListEntry { cacheCode: RedisCacheCode; matcher: (key: string) => boolean; description: string; } export const RedisCacheKeyAllowList: RedisCacheKeyAllowListEntry[] = [ { cacheCode: 'VIDEO_LATEST', matcher: (key) => key === 'box:app:video:latest', description: 'Latest videos key', }, { cacheCode: 'VIDEO_RECOMMENDED', matcher: (key) => key === 'box:app:video:recommended', description: 'Recommended videos key', }, { cacheCode: 'TAG_ALL', matcher: (key) => key === 'box:app:tag:all', description: 'Global tag metadata key', }, { cacheCode: 'CATEGORY_ALL', matcher: (key) => key === 'box:app:category:all', description: 'Category all key', }, { cacheCode: 'CATEGORY_BY_ID', matcher: (key) => key.startsWith('box:app:category:by-id:'), description: 'Category detail key', }, { cacheCode: 'CHANNEL_ALL', matcher: (key) => key === 'box:app:channel:all', description: 'Channel all key', }, { cacheCode: 'CHANNEL_BY_ID', matcher: (key) => key.startsWith('box:app:channel:by-id:'), description: 'Channel detail key', }, { cacheCode: 'ADPOOL', matcher: (key) => key.startsWith('box:app:adpool:'), description: 'Ad pool key', }, ];