cache-keys.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // libs/common/src/cache/cache-keys.ts
  2. import type { AdType } from '../ads/ad-types';
  3. /**
  4. * Video sort key type for video listing and pooling.
  5. */
  6. export type VideoSortKey = 'latest' | 'popular' | 'manual';
  7. /**
  8. * Video home section key type for home page sections.
  9. */
  10. export type VideoHomeSectionKey = 'featured' | 'latest' | 'editorPick';
  11. /**
  12. * Centralized Redis logical keys (without REDIS_KEY_PREFIX).
  13. * Actual keys in Redis will be: <REDIS_KEY_PREFIX><logicalKey>
  14. * e.g. "box:" + "app:channel:all" => "box:app:channel:all"
  15. */
  16. export const CacheKeys = {
  17. // ─────────────────────────────────────────────
  18. // CHANNELS (existing)
  19. // ─────────────────────────────────────────────
  20. appChannelAll: 'app:channel:all',
  21. appChannelById: (channelId: string | number): string =>
  22. `app:channel:by-id:${channelId}`,
  23. // NEW: Channel → Categories tree (future use)
  24. appChannelWithCategories: (channelId: string | number): string =>
  25. `app:channel:with-categories:${channelId}`,
  26. // ─────────────────────────────────────────────
  27. // CATEGORIES (existing)
  28. // ─────────────────────────────────────────────
  29. appCategory: (categoryId: string | number): string =>
  30. `app:category:${categoryId}`,
  31. appCategoryAll: 'app:category:all',
  32. appCategoryById: (categoryId: string | number): string =>
  33. `app:category:by-id:${categoryId}`,
  34. // NEW: Category → Tags tree (main for video listing)
  35. appCategoryWithTags: (categoryId: string | number): string =>
  36. `app:category:with-tags:${categoryId}`,
  37. // ─────────────────────────────────────────────
  38. // TAGS (new)
  39. // ─────────────────────────────────────────────
  40. // Global tag suggestion pool
  41. appTagAll: 'app:tag:all',
  42. // (Optional future)
  43. // appTagByCategory: (categoryId: string | number): string =>
  44. // `app:tag:by-category:${categoryId}`,
  45. // appTagByChannel: (channelId: string | number): string =>
  46. // `app:tag:by-channel:${channelId}`,
  47. // ─────────────────────────────────────────────
  48. // ADS (existing)
  49. // ─────────────────────────────────────────────
  50. appAdById: (adId: string | number): string => `app:ad:by-id:${adId}`,
  51. // ─────────────────────────────────────────────
  52. // AD POOLS (AdType-based)
  53. // ─────────────────────────────────────────────
  54. /** Build the canonical ad pool key for a given AdType. */
  55. appAdPoolByType: (adType: AdType | string): string => `app:adpool:${adType}`,
  56. // ─────────────────────────────────────────────
  57. // VIDEO LISTS (existing)
  58. // ─────────────────────────────────────────────
  59. appHomeVideoPage: (page: number): string => `app:videolist:home:page:${page}`,
  60. appChannelVideoPage: (channelId: string | number, page: number): string =>
  61. `app:videolist:channel:${channelId}:page:${page}`,
  62. appTrendingVideoPage: (countryCode: string, page: number): string =>
  63. `app:videolist:trending:${countryCode}:page:${page}`,
  64. // ─────────────────────────────────────────────
  65. // VIDEO DETAILS & METADATA
  66. // ─────────────────────────────────────────────
  67. /** Get cache key for video detail data. */
  68. appVideoDetailKey: (videoId: string): string => `app:video:detail:${videoId}`,
  69. /** Get cache key for video category list by channel. */
  70. appVideoCategoryListKey: (channelId: string): string =>
  71. `app:video:category:list:${channelId}`,
  72. /** Get cache key for video tag list by channel and category. */
  73. appVideoTagListKey: (channelId: string, categoryId: string): string =>
  74. `app:video:tag:list:${channelId}:${categoryId}`,
  75. // ─────────────────────────────────────────────
  76. // VIDEO POOLS (sorted listings)
  77. // ─────────────────────────────────────────────
  78. /** Get cache key for videos in a category with sort order. */
  79. appVideoCategoryPoolKey: (
  80. channelId: string,
  81. categoryId: string,
  82. sort: VideoSortKey,
  83. ): string => `app:video:list:category:${channelId}:${categoryId}:${sort}`,
  84. /** Get cache key for videos with a specific tag with sort order. */
  85. appVideoTagPoolKey: (
  86. channelId: string,
  87. tagId: string,
  88. sort: VideoSortKey,
  89. ): string => `app:video:list:tag:${channelId}:${tagId}:${sort}`,
  90. /** Get cache key for home page video section. */
  91. appVideoHomeSectionKey: (
  92. channelId: string,
  93. section: VideoHomeSectionKey,
  94. ): string => `app:video:list:home:${channelId}:${section}`,
  95. };