|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import { createHash } from 'crypto'; |
| 3 | + |
| 4 | +// Cache entry interface |
| 5 | +interface CacheEntry { |
| 6 | + data: string; |
| 7 | + timestamp: number; |
| 8 | + extensionVersion: string; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * DocumentationCache class handles persistent caching of ZModel documentation |
| 13 | + * using VS Code's globalState for cross-session persistence |
| 14 | + */ |
| 15 | +export class DocumentationCache implements vscode.Disposable { |
| 16 | + private static readonly CACHE_DURATION_MS = 30 * 24 * 60 * 60 * 1000; // 30 days cache duration |
| 17 | + private static readonly CACHE_PREFIX = 'doc-cache.'; |
| 18 | + |
| 19 | + private extensionContext: vscode.ExtensionContext; |
| 20 | + private extensionVersion: string; |
| 21 | + |
| 22 | + constructor(context: vscode.ExtensionContext) { |
| 23 | + this.extensionContext = context; |
| 24 | + this.extensionVersion = context.extension.packageJSON.version as string; |
| 25 | + // clear expired cache entries on initialization |
| 26 | + this.clearExpiredCache(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Dispose of the cache resources (implements vscode.Disposable) |
| 31 | + */ |
| 32 | + dispose(): void {} |
| 33 | + |
| 34 | + /** |
| 35 | + * Get the cache prefix used for keys |
| 36 | + */ |
| 37 | + getCachePrefix(): string { |
| 38 | + return DocumentationCache.CACHE_PREFIX; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Enable cache synchronization across machines via VS Code Settings Sync |
| 43 | + */ |
| 44 | + private enableCacheSync(): void { |
| 45 | + const cacheKeys = this.extensionContext.globalState |
| 46 | + .keys() |
| 47 | + .filter((key) => key.startsWith(DocumentationCache.CACHE_PREFIX)); |
| 48 | + if (cacheKeys.length > 0) { |
| 49 | + this.extensionContext.globalState.setKeysForSync(cacheKeys); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Generate a cache key from request body with normalized content |
| 55 | + */ |
| 56 | + private generateCacheKey(models: string[]): string { |
| 57 | + // Remove ALL whitespace characters from each model string for cache key generation |
| 58 | + // This ensures identical content with different formatting uses the same cache |
| 59 | + const normalizedModels = models.map((model) => model.replace(/\s/g, '')).sort(); |
| 60 | + const hash = createHash('sha512') |
| 61 | + .update(JSON.stringify({ models: normalizedModels })) |
| 62 | + .digest('hex'); |
| 63 | + return `${DocumentationCache.CACHE_PREFIX}${hash}`; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Check if cache entry is still valid (not expired) |
| 68 | + */ |
| 69 | + private isCacheValid(entry: CacheEntry): boolean { |
| 70 | + return Date.now() - entry.timestamp < DocumentationCache.CACHE_DURATION_MS; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Get cached response if available and valid |
| 75 | + */ |
| 76 | + async getCachedResponse(models: string[]): Promise<string | null> { |
| 77 | + const cacheKey = this.generateCacheKey(models); |
| 78 | + const entry = this.extensionContext.globalState.get<CacheEntry>(cacheKey); |
| 79 | + |
| 80 | + if (entry && this.isCacheValid(entry)) { |
| 81 | + console.log('Using cached documentation response from persistent storage'); |
| 82 | + return entry.data; |
| 83 | + } |
| 84 | + |
| 85 | + // Clean up expired entry if it exists |
| 86 | + if (entry) { |
| 87 | + await this.extensionContext.globalState.update(cacheKey, undefined); |
| 88 | + } |
| 89 | + |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Cache a response for future use |
| 95 | + */ |
| 96 | + async setCachedResponse(models: string[], data: string): Promise<void> { |
| 97 | + const cacheKey = this.generateCacheKey(models); |
| 98 | + const cacheEntry: CacheEntry = { |
| 99 | + data, |
| 100 | + timestamp: Date.now(), |
| 101 | + extensionVersion: this.extensionVersion, |
| 102 | + }; |
| 103 | + |
| 104 | + await this.extensionContext.globalState.update(cacheKey, cacheEntry); |
| 105 | + |
| 106 | + // Update sync keys to include new cache entry |
| 107 | + this.enableCacheSync(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Clear expired cache entries from persistent storage |
| 112 | + */ |
| 113 | + async clearExpiredCache(): Promise<void> { |
| 114 | + const now = Date.now(); |
| 115 | + let clearedCount = 0; |
| 116 | + const allKeys = this.extensionContext.globalState.keys(); |
| 117 | + |
| 118 | + for (const key of allKeys) { |
| 119 | + if (key.startsWith(DocumentationCache.CACHE_PREFIX)) { |
| 120 | + const entry = this.extensionContext.globalState.get<CacheEntry>(key); |
| 121 | + if ( |
| 122 | + entry?.extensionVersion !== this.extensionVersion || |
| 123 | + now - entry.timestamp >= DocumentationCache.CACHE_DURATION_MS |
| 124 | + ) { |
| 125 | + await this.extensionContext.globalState.update(key, undefined); |
| 126 | + clearedCount++; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (clearedCount > 0) { |
| 132 | + console.log(`Cleared ${clearedCount} expired cache entries from persistent storage`); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Clear all cache entries from persistent storage |
| 138 | + */ |
| 139 | + async clearAllCache(): Promise<void> { |
| 140 | + const allKeys = this.extensionContext.globalState.keys(); |
| 141 | + let clearedCount = 0; |
| 142 | + |
| 143 | + for (const key of allKeys) { |
| 144 | + if (key.startsWith(DocumentationCache.CACHE_PREFIX)) { |
| 145 | + await this.extensionContext.globalState.update(key, undefined); |
| 146 | + clearedCount++; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + console.log(`Cleared all cache entries from persistent storage (${clearedCount} items)`); |
| 151 | + } |
| 152 | +} |
0 commit comments