ailock
Version:
AI-Proof File Guard - Protect sensitive files from accidental AI modifications
104 lines • 2.78 kB
JavaScript
/**
* Git Repository Detection Cache
*
* Provides caching for expensive Git repository detection operations
* to improve performance when working with many files.
*/
export class GitRepoCache {
static instance;
cache = new Map();
TTL_MS = 5 * 60 * 1000; // 5 minutes TTL
MAX_ENTRIES = 1000; // Prevent unbounded growth
constructor() { }
static getInstance() {
if (!GitRepoCache.instance) {
GitRepoCache.instance = new GitRepoCache();
}
return GitRepoCache.instance;
}
/**
* Get cached repository root for a path
*/
get(path) {
const entry = this.cache.get(path);
if (!entry) {
return undefined; // Not in cache
}
// Check if entry is still valid
const now = Date.now();
if (now - entry.timestamp > this.TTL_MS) {
this.cache.delete(path);
return undefined; // Expired
}
return entry.repoRoot;
}
/**
* Set cached repository root for a path
*/
set(path, repoRoot) {
// Enforce max cache size
if (this.cache.size >= this.MAX_ENTRIES && !this.cache.has(path)) {
// Remove oldest entry (first in map)
const firstKey = this.cache.keys().next().value;
if (firstKey) {
this.cache.delete(firstKey);
}
}
this.cache.set(path, {
repoRoot,
timestamp: Date.now()
});
}
/**
* Clear the entire cache
*/
clear() {
this.cache.clear();
}
/**
* Clear expired entries
*/
clearExpired() {
const now = Date.now();
for (const [path, entry] of this.cache.entries()) {
if (now - entry.timestamp > this.TTL_MS) {
this.cache.delete(path);
}
}
}
/**
* Get cache statistics
*/
getStats() {
return {
size: this.cache.size,
maxSize: this.MAX_ENTRIES,
ttlMs: this.TTL_MS
};
}
/**
* Invalidate cache entries for a specific repository
*/
invalidateRepo(repoRoot) {
for (const [path, entry] of this.cache.entries()) {
if (entry.repoRoot === repoRoot) {
this.cache.delete(path);
}
}
}
/**
* Invalidate cache entries under a specific path
*/
invalidatePath(basePath) {
for (const [path] of this.cache.entries()) {
if (path.startsWith(basePath)) {
this.cache.delete(path);
}
}
}
}
/**
* Export singleton instance methods for convenience
*/
export const gitRepoCache = GitRepoCache.getInstance();
//# sourceMappingURL=git-cache.js.map