-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathcache-manager.ts
More file actions
181 lines (154 loc) · 4.39 KB
/
cache-manager.ts
File metadata and controls
181 lines (154 loc) · 4.39 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
import fs from 'fs';
import path from 'path';
import { getCacheConfig } from '../../../config';
export interface CacheStats {
totalRepositories: number;
totalSizeMB: number;
repositories: Array<{
name: string;
sizeMB: number;
lastAccessed: Date;
}>;
}
export class CacheManager {
private cacheDir: string;
private maxSizeGB: number;
private maxRepositories: number;
constructor(
cacheDir: string = './.remote/cache',
maxSizeGB: number = 2,
maxRepositories: number = 50,
) {
this.cacheDir = cacheDir;
this.maxSizeGB = maxSizeGB;
this.maxRepositories = maxRepositories;
}
/**
* Update access time for repository (for LRU purposes)
*/
touchRepository(repoName: string): void {
const repoPath = path.join(this.cacheDir, repoName);
if (fs.existsSync(repoPath)) {
const now = new Date();
fs.utimesSync(repoPath, now, now);
}
}
/**
* Get cache statistics
*/
getCacheStats(): CacheStats {
if (!fs.existsSync(this.cacheDir)) {
return {
totalRepositories: 0,
totalSizeMB: 0,
repositories: [],
};
}
const repositories: Array<{ name: string; sizeMB: number; lastAccessed: Date }> = [];
let totalSizeMB = 0;
const entries = fs.readdirSync(this.cacheDir, { withFileTypes: true });
for (const entry of entries) {
if (entry.isDirectory()) {
const repoPath = path.join(this.cacheDir, entry.name);
const sizeMB = this.getDirectorySize(repoPath);
const stats = fs.statSync(repoPath);
repositories.push({
name: entry.name,
sizeMB,
lastAccessed: stats.atime,
});
totalSizeMB += sizeMB;
}
}
// Sort by last accessed (newest first)
repositories.sort((a, b) => b.lastAccessed.getTime() - a.lastAccessed.getTime());
return {
totalRepositories: repositories.length,
totalSizeMB,
repositories,
};
}
/**
* Enforce cache limits using LRU eviction
*/
enforceLimits(): { removedRepos: string[]; freedMB: number } {
const stats = this.getCacheStats();
const removedRepos: string[] = [];
let freedMB = 0;
// Sort repositories by last accessed (oldest first for removal)
const reposToEvaluate = [...stats.repositories].sort(
(a, b) => a.lastAccessed.getTime() - b.lastAccessed.getTime(),
);
// Check size limit
let currentSizeMB = stats.totalSizeMB;
const maxSizeMB = this.maxSizeGB * 1024;
for (const repo of reposToEvaluate) {
const shouldRemove =
currentSizeMB > maxSizeMB || // Over size limit
stats.totalRepositories - removedRepos.length > this.maxRepositories; // Over count limit
if (shouldRemove) {
this.removeRepository(repo.name);
removedRepos.push(repo.name);
freedMB += repo.sizeMB;
currentSizeMB -= repo.sizeMB;
} else {
break; // We've cleaned enough
}
}
return { removedRepos, freedMB };
}
/**
* Remove specific repository from cache
*/
private removeRepository(repoName: string): void {
const repoPath = path.join(this.cacheDir, repoName);
if (fs.existsSync(repoPath)) {
fs.rmSync(repoPath, { recursive: true, force: true });
}
}
/**
* Calculate directory size in MB
*/
private getDirectorySize(dirPath: string): number {
let totalBytes = 0;
const calculateSize = (currentPath: string) => {
const items = fs.readdirSync(currentPath, { withFileTypes: true });
for (const item of items) {
const itemPath = path.join(currentPath, item.name);
if (item.isDirectory()) {
calculateSize(itemPath);
} else {
try {
const stats = fs.statSync(itemPath);
totalBytes += stats.size;
} catch (error) {
// Skip files that can't be read
}
}
}
};
try {
calculateSize(dirPath);
} catch (error) {
return 0;
}
return Math.round(totalBytes / (1024 * 1024)); // Convert to MB
}
/**
* Get cache configuration
*/
getConfig() {
return {
maxSizeGB: this.maxSizeGB,
maxRepositories: this.maxRepositories,
cacheDir: this.cacheDir,
};
}
}
// Global instance initialized with config
const config = getCacheConfig();
export const cacheManager = new CacheManager(
config.cacheDir,
config.maxSizeGB,
config.maxRepositories,
);