|
| 1 | +import { ApiClient, ScanResult, ResourceKeyDetails, ValidationResult, KeyUsage, ResourceKey } from './apiClient'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Shared cache service for the LRM extension. |
| 5 | + * Provides centralized caching for scan results, key details, and validation data. |
| 6 | + * Used by CodeLens provider, Resource Editor, and diagnostics. |
| 7 | + */ |
| 8 | +export class CacheService { |
| 9 | + private scanResultsCache: ScanResult | null = null; |
| 10 | + private keyDetailsCache: Map<string, ResourceKeyDetails> = new Map(); |
| 11 | + private keysCache: ResourceKey[] | null = null; |
| 12 | + private validationCache: ValidationResult | null = null; |
| 13 | + private keyReferencesCache: Map<string, KeyUsage> = new Map(); |
| 14 | + |
| 15 | + private scanResultsTimestamp: number = 0; |
| 16 | + private validationTimestamp: number = 0; |
| 17 | + private keysTimestamp: number = 0; |
| 18 | + |
| 19 | + private readonly TTL = 30000; // 30 seconds cache TTL |
| 20 | + |
| 21 | + constructor(private apiClient: ApiClient) {} |
| 22 | + |
| 23 | + /** |
| 24 | + * Check if a timestamp has expired based on TTL |
| 25 | + */ |
| 26 | + private isExpired(timestamp: number): boolean { |
| 27 | + return Date.now() - timestamp > this.TTL; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Get scan results (cached or fresh) |
| 32 | + */ |
| 33 | + async getScanResults(forceRefresh = false): Promise<ScanResult> { |
| 34 | + if (!forceRefresh && this.scanResultsCache && !this.isExpired(this.scanResultsTimestamp)) { |
| 35 | + return this.scanResultsCache; |
| 36 | + } |
| 37 | + |
| 38 | + this.scanResultsCache = await this.apiClient.scanCode(); |
| 39 | + this.scanResultsTimestamp = Date.now(); |
| 40 | + |
| 41 | + // Also populate key references cache from scan results |
| 42 | + this.keyReferencesCache.clear(); |
| 43 | + for (const ref of this.scanResultsCache.references) { |
| 44 | + this.keyReferencesCache.set(ref.key, ref); |
| 45 | + } |
| 46 | + |
| 47 | + return this.scanResultsCache; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get all resource keys (cached or fresh) |
| 52 | + */ |
| 53 | + async getKeys(forceRefresh = false): Promise<ResourceKey[]> { |
| 54 | + if (!forceRefresh && this.keysCache && !this.isExpired(this.keysTimestamp)) { |
| 55 | + return this.keysCache; |
| 56 | + } |
| 57 | + |
| 58 | + this.keysCache = await this.apiClient.getKeys(); |
| 59 | + this.keysTimestamp = Date.now(); |
| 60 | + return this.keysCache; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get details for a specific key (cached or fresh) |
| 65 | + */ |
| 66 | + async getKeyDetails(key: string, forceRefresh = false): Promise<ResourceKeyDetails> { |
| 67 | + if (!forceRefresh && this.keyDetailsCache.has(key)) { |
| 68 | + return this.keyDetailsCache.get(key)!; |
| 69 | + } |
| 70 | + |
| 71 | + const details = await this.apiClient.getKeyDetails(key); |
| 72 | + this.keyDetailsCache.set(key, details); |
| 73 | + return details; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get validation results (cached or fresh) |
| 78 | + */ |
| 79 | + async getValidation(forceRefresh = false): Promise<ValidationResult> { |
| 80 | + if (!forceRefresh && this.validationCache && !this.isExpired(this.validationTimestamp)) { |
| 81 | + return this.validationCache; |
| 82 | + } |
| 83 | + |
| 84 | + this.validationCache = await this.apiClient.validate(); |
| 85 | + this.validationTimestamp = Date.now(); |
| 86 | + return this.validationCache; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get references for a specific key (from cached scan results or API) |
| 91 | + */ |
| 92 | + async getKeyReferences(key: string, forceRefresh = false): Promise<KeyUsage> { |
| 93 | + // Try to get from cache first |
| 94 | + if (!forceRefresh && this.keyReferencesCache.has(key)) { |
| 95 | + return this.keyReferencesCache.get(key)!; |
| 96 | + } |
| 97 | + |
| 98 | + // If we have scan results, the key might just not have any references |
| 99 | + if (!forceRefresh && this.scanResultsCache && !this.isExpired(this.scanResultsTimestamp)) { |
| 100 | + const cached = this.keyReferencesCache.get(key); |
| 101 | + if (cached) { |
| 102 | + return cached; |
| 103 | + } |
| 104 | + // Key not in references means 0 references |
| 105 | + return { key, referenceCount: 0, references: [] }; |
| 106 | + } |
| 107 | + |
| 108 | + // Fetch fresh from API |
| 109 | + const usage = await this.apiClient.getKeyReferences(key); |
| 110 | + this.keyReferencesCache.set(key, usage); |
| 111 | + return usage; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Get reference count for a key (quick lookup from cache) |
| 116 | + */ |
| 117 | + getReferenceCountFromCache(key: string): number | null { |
| 118 | + const cached = this.keyReferencesCache.get(key); |
| 119 | + return cached ? cached.referenceCount : null; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Check if a key is in the unused list (from cached scan results) |
| 124 | + */ |
| 125 | + isKeyUnused(key: string): boolean | null { |
| 126 | + if (!this.scanResultsCache || !Array.isArray(this.scanResultsCache.unused)) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + return this.scanResultsCache.unused.includes(key); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Check if a key has duplicates (from cached validation) |
| 134 | + */ |
| 135 | + isKeyDuplicate(key: string): boolean | null { |
| 136 | + if (!this.validationCache || !Array.isArray(this.validationCache.duplicateKeys)) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + return this.validationCache.duplicateKeys.includes(key); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Get missing languages for a key (from cached key details) |
| 144 | + */ |
| 145 | + getMissingLanguages(key: string): string[] | null { |
| 146 | + const details = this.keyDetailsCache.get(key); |
| 147 | + if (!details) { |
| 148 | + return null; |
| 149 | + } |
| 150 | + |
| 151 | + // Find languages with empty values |
| 152 | + const missing: string[] = []; |
| 153 | + for (const [lang, data] of Object.entries(details.values)) { |
| 154 | + if (!data.value || data.value.trim() === '') { |
| 155 | + missing.push(lang); |
| 156 | + } |
| 157 | + } |
| 158 | + return missing; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Invalidate all caches (call when .resx files change) |
| 163 | + */ |
| 164 | + invalidate(): void { |
| 165 | + this.scanResultsCache = null; |
| 166 | + this.keyDetailsCache.clear(); |
| 167 | + this.keysCache = null; |
| 168 | + this.validationCache = null; |
| 169 | + this.keyReferencesCache.clear(); |
| 170 | + this.scanResultsTimestamp = 0; |
| 171 | + this.validationTimestamp = 0; |
| 172 | + this.keysTimestamp = 0; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Invalidate only key-specific caches (for when a single key is modified) |
| 177 | + */ |
| 178 | + invalidateKey(key: string): void { |
| 179 | + this.keyDetailsCache.delete(key); |
| 180 | + this.keyReferencesCache.delete(key); |
| 181 | + // Also invalidate keys list and validation since they might be affected |
| 182 | + this.keysCache = null; |
| 183 | + this.validationCache = null; |
| 184 | + this.keysTimestamp = 0; |
| 185 | + this.validationTimestamp = 0; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Check if cache has any data (for UI status) |
| 190 | + */ |
| 191 | + hasData(): boolean { |
| 192 | + return this.scanResultsCache !== null || this.keysCache !== null; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Get cached scan results if available (does NOT fetch from API) |
| 197 | + * Returns null if no cached results or if cache is expired |
| 198 | + */ |
| 199 | + getCachedScanResults(): ScanResult | null { |
| 200 | + if (this.scanResultsCache && !this.isExpired(this.scanResultsTimestamp)) { |
| 201 | + return this.scanResultsCache; |
| 202 | + } |
| 203 | + return null; |
| 204 | + } |
| 205 | +} |
0 commit comments