-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathcontextCache.ts
More file actions
211 lines (183 loc) · 6.05 KB
/
Copy pathcontextCache.ts
File metadata and controls
211 lines (183 loc) · 6.05 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as crypto from 'crypto';
import { INodeImportClass } from './copilotHelper';
/**
* Cache entry interface for storing import data with timestamp
*/
interface CacheEntry {
value: INodeImportClass[];
timestamp: number;
}
/**
* Configuration options for the context cache
*/
interface ContextCacheOptions {
/** Cache expiry time in milliseconds. Default: 5 minutes */
expiryTime?: number;
/** Enable automatic cleanup interval. Default: true */
enableAutoCleanup?: boolean;
/** Enable file watching for cache invalidation. Default: true */
enableFileWatching?: boolean;
}
/**
* Context cache manager for storing and managing Java import contexts
*/
export class ContextCache {
private readonly cache = new Map<string, CacheEntry>();
private readonly expiryTime: number;
private readonly enableAutoCleanup: boolean;
private readonly enableFileWatching: boolean;
private cleanupInterval?: NodeJS.Timeout;
private fileWatcher?: vscode.FileSystemWatcher;
constructor(options: ContextCacheOptions = {}) {
this.expiryTime = options.expiryTime ?? 5 * 60 * 1000; // 5 minutes default
this.enableAutoCleanup = options.enableAutoCleanup ?? true;
this.enableFileWatching = options.enableFileWatching ?? true;
}
/**
* Initialize the cache with VS Code extension context
* @param context VS Code extension context for managing disposables
*/
public initialize(context: vscode.ExtensionContext): void {
if (this.enableAutoCleanup) {
this.startPeriodicCleanup();
}
if (this.enableFileWatching) {
this.setupFileWatcher();
}
// Register cleanup on extension disposal
context.subscriptions.push(
new vscode.Disposable(() => {
this.dispose();
})
);
if (this.fileWatcher) {
context.subscriptions.push(this.fileWatcher);
}
}
/**
* Generate a hash for the document URI to use as cache key
* @param uri Document URI
* @returns Hashed URI string
*/
private generateCacheKey(uri: vscode.Uri): string {
return crypto.createHash('md5').update(uri.toString()).digest('hex');
}
/**
* Get cached imports for a document URI
* @param uri Document URI
* @returns Cached imports or null if not found/expired
*/
public get(uri: vscode.Uri): INodeImportClass[] | null {
const key = this.generateCacheKey(uri);
const cached = this.cache.get(key);
if (!cached) {
return null;
}
// Check if cache is expired
if (this.isExpired(cached)) {
this.cache.delete(key);
return null;
}
return cached.value;
}
/**
* Set cached imports for a document URI
* @param uri Document URI
* @param imports Import class array to cache
*/
public set(uri: vscode.Uri, imports: INodeImportClass[]): void {
const key = this.generateCacheKey(uri);
this.cache.set(key, {
value: imports,
timestamp: Date.now()
});
}
/**
* Check if a cache entry is expired
* @param entry Cache entry to check
* @returns True if expired, false otherwise
*/
private isExpired(entry: CacheEntry): boolean {
return Date.now() - entry.timestamp > this.expiryTime;
}
/**
* Clear expired cache entries
*/
public clearExpired(): void {
const now = Date.now();
for (const [key, entry] of this.cache.entries()) {
if (now - entry.timestamp > this.expiryTime) {
this.cache.delete(key);
}
}
}
/**
* Clear all cache entries
*/
public clear(): void {
this.cache.clear();
}
/**
* Invalidate cache for specific URI
* @param uri URI to invalidate
*/
public invalidate(uri: vscode.Uri): void {
const key = this.generateCacheKey(uri);
if (this.cache.has(key)) {
this.cache.delete(key);
console.log('Cache invalidated for:', uri.toString());
}
}
/**
* Get cache statistics
* @returns Object containing cache size and other statistics
*/
public getStats(): { size: number; expiryTime: number } {
return {
size: this.cache.size,
expiryTime: this.expiryTime
};
}
/**
* Start periodic cleanup of expired cache entries
*/
private startPeriodicCleanup(): void {
this.cleanupInterval = setInterval(() => {
this.clearExpired();
}, this.expiryTime);
}
/**
* Setup file system watcher for Java files to invalidate cache on changes
*/
private setupFileWatcher(): void {
this.fileWatcher = vscode.workspace.createFileSystemWatcher('**/*.java');
const invalidateHandler = (uri: vscode.Uri) => {
this.invalidate(uri);
};
this.fileWatcher.onDidChange(invalidateHandler);
this.fileWatcher.onDidDelete(invalidateHandler);
}
/**
* Dispose of all resources (intervals, watchers, etc.)
*/
public dispose(): void {
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
this.cleanupInterval = undefined;
}
if (this.fileWatcher) {
this.fileWatcher.dispose();
this.fileWatcher = undefined;
}
this.clear();
}
}
/**
* Default context cache instance
*/
export const contextCache = new ContextCache();