All files / src/common/types cache.ts

0% Statements 0/54
100% Branches 1/1
100% Functions 1/1
0% Lines 0/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194                                                                                                                                                                                                                                                                                                                                                                                                   
import { NewsletterWithRelations } from "./index";
 
// Updated NewsletterFilter to support object-based filtering
export interface NewsletterFilter {
  // Search and text filtering
  search?: string;
 
  // Status filters
  isRead?: boolean;
  isArchived?: boolean;
  isLiked?: boolean;
 
  // Relationship filters
  tagIds?: string[];
  sourceIds?: string[];
  groupIds?: string[];
 
  // Date filtering
  dateFrom?: string;
  dateTo?: string;
 
  // Pagination
  limit?: number;
  offset?: number;
  page?: number;
 
  // Sorting
  orderBy?: string;
  orderDirection?: 'asc' | 'desc';
 
  // Legacy support - can be used for quick filtering
  status?: "all" | "unread" | "liked" | "archived";
}
 
// Legacy type for backward compatibility
export type LegacyNewsletterFilter = "all" | "unread" | "liked" | "archived";
 
// Utility to convert legacy filter to new format
export const convertLegacyFilter = (
  legacyFilter: LegacyNewsletterFilter,
): NewsletterFilter => {
  switch (legacyFilter) {
    case "unread":
      return { isRead: false, isArchived: false };
    case "liked":
      return { isLiked: true, isArchived: false };
    case "archived":
      return { isArchived: true };
    case "all":
    default:
      return { isArchived: false };
  }
};
 
export interface NewsletterQueryKey {
  entity: "newsletters";
  scope: "list" | "detail" | "tags" | "sources";
  params: {
    userId?: string;
    id?: string;
    filters?: NewsletterFilter;
    // Legacy support
    filter?: LegacyNewsletterFilter;
    tagIds?: string[];
    sourceId?: string | null;
    groupSourceIds?: string[];
    timeRange?: string;
  };
}
 
export interface NewsletterCacheUpdate {
  id: string;
  updates: Partial<NewsletterWithRelations>;
}
 
export interface NewsletterBulkUpdate {
  ids: string[];
  updates: Partial<NewsletterWithRelations>;
}
 
// Source-related cache types
export interface NewsletterSourceFilter {
  search?: string;
  isArchived?: boolean;
  excludeArchived?: boolean;
  includeCount?: boolean;
  limit?: number;
  offset?: number;
  orderBy?: string;
  ascending?: boolean;
}
 
export interface NewsletterSourceQueryKey {
  entity: "newsletter_sources";
  scope: "list" | "detail" | "stats";
  params: {
    userId?: string;
    id?: string;
    filters?: NewsletterSourceFilter;
  };
}
 
// Tag-related cache types
export interface TagFilter {
  search?: string;
  includeCount?: boolean;
  limit?: number;
  offset?: number;
  orderBy?: string;
  ascending?: boolean;
}
 
export interface TagQueryKey {
  entity: "tags";
  scope: "list" | "detail" | "stats";
  params: {
    userId?: string;
    id?: string;
    filters?: TagFilter;
  };
}
 
// Cache invalidation patterns
export interface CacheInvalidationPattern {
  entity: string;
  scope?: string;
  operation: string;
  affectedIds?: string[];
}
 
// Performance monitoring
export interface CachePerformanceMetrics {
  operation: string;
  cacheHit: boolean;
  duration: number;
  timestamp: number;
  keyPattern: string;
}
 
// Cache configuration
export const CACHE_DURATION = {
  SHORT: 60 * 1000, // 1 minute
  MEDIUM: 5 * 60 * 1000, // 5 minutes
  LONG: 30 * 60 * 1000, // 30 minutes
  DAY: 24 * 60 * 60 * 1000, // 24 hours
};
 
export const CACHE_CONFIG = {
  // Newsletter-specific cache times
  NEWSLETTER_LIST_STALE_TIME: 2 * 60 * 1000, // 2 minutes
  NEWSLETTER_LIST_CACHE_TIME: 10 * 60 * 1000, // 10 minutes
  NEWSLETTER_DETAIL_STALE_TIME: 5 * 60 * 1000, // 5 minutes
  NEWSLETTER_DETAIL_CACHE_TIME: 15 * 60 * 1000, // 15 minutes
 
  // Source-specific cache times
  SOURCE_LIST_STALE_TIME: 5 * 60 * 1000, // 5 minutes
  SOURCE_LIST_CACHE_TIME: 30 * 60 * 1000, // 30 minutes
 
  // Tag-specific cache times
  TAG_LIST_STALE_TIME: 10 * 60 * 1000, // 10 minutes
  TAG_LIST_CACHE_TIME: 60 * 60 * 1000, // 1 hour
 
  // Error retry configuration
  RETRY_DELAY_BASE: 1000, // 1 second base delay
  MAX_RETRY_DELAY: 30000, // 30 second max delay
  MAX_RETRIES: 3,
};
 
// Query key matchers for cache invalidation
export const CACHE_KEY_MATCHERS = {
  isNewsletterListKey: (key: unknown[]): boolean => {
    return key.length >= 2 && key[0] === "newsletters" && key[1] === "list";
  },
 
  isNewsletterDetailKey: (key: unknown[], id?: string): boolean => {
    if (key.length >= 3 && key[0] === "newsletters" && key[1] === "detail") {
      return id ? key[2] === id : true;
    }
    return false;
  },
 
  isNewsletterSourceKey: (key: unknown[]): boolean => {
    return key.length >= 1 && key[0] === "newsletter_sources";
  },
 
  isTagKey: (key: unknown[]): boolean => {
    return key.length >= 1 && key[0] === "tags";
  },
 
  isReadingQueueKey: (key: unknown[]): boolean => {
    return key.length >= 1 && key[0] === "reading_queue";
  },
};