Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | 5x 79x 79x 79x 79x | export interface CacheEntry<T> {
data: T;
timestamp: number;
ttl: number; // Time to live in milliseconds
}
export interface ProjectCacheData {
id: string;
name: string;
provider: string;
description?: string;
url?: string;
members?: UserRole[];
}
export interface UserRole {
userId: string;
username: string;
displayName: string;
email?: string;
role: string; // maintainer, developer, reporter, etc.
accessLevel?: number;
}
export interface ProviderUserCache {
provider: string;
users: Map<string, UserRole>;
lastUpdated: number;
}
/**
* Centralized cache manager for Project Nexus
* Handles caching of projects, users, and their roles with TTL support
*/
export class CacheManager {
private projectsCache: Map<string, CacheEntry<ProjectCacheData[]>> = new Map();
private usersCache: Map<string, CacheEntry<ProviderUserCache>> = new Map();
private refreshTimers: Map<string, NodeJS.Timeout> = new Map();
constructor(
private defaultTTL = 15 * 60 * 1000, // 15 minutes default TTL
) {}
/**
* Store projects data in cache for a specific provider
*/
setProjects(provider: string, projects: ProjectCacheData[], ttl?: number): void {
const entry: CacheEntry<ProjectCacheData[]> = {
data: projects,
timestamp: Date.now(),
ttl: ttl ?? this.defaultTTL,
};
this.projectsCache.set(provider, entry);
this.scheduleRefresh(`projects:${provider}`, entry.ttl);
process.stderr.write(`[cache] Cached ${projects.length} projects for ${provider}\n`);
}
/**
* Get cached projects for a provider
*/
getProjects(provider: string): ProjectCacheData[] | null {
const entry = this.projectsCache.get(provider);
if (!entry) {
return null;
}
// Check if cache is expired
if (Date.now() - entry.timestamp > entry.ttl) {
this.projectsCache.delete(provider);
return null;
}
return entry.data;
}
/**
* Get all cached projects across all providers
*/
getAllProjects(): ProjectCacheData[] {
const allProjects: ProjectCacheData[] = [];
for (const [provider, entry] of Array.from(this.projectsCache.entries())) {
// Check if cache is still valid
if (Date.now() - entry.timestamp <= entry.ttl) {
allProjects.push(...entry.data);
} else {
// Clean up expired cache
this.projectsCache.delete(provider);
}
}
return allProjects;
}
/**
* Search projects across all cached providers
*/
searchProjects(query?: string): ProjectCacheData[] {
const allProjects = this.getAllProjects();
if (!query) {
return allProjects;
}
const searchTerm = query.toLowerCase();
return allProjects.filter(
(project) =>
project.name.toLowerCase().includes(searchTerm) ||
project.id.toLowerCase().includes(searchTerm) ||
project.description?.toLowerCase().includes(searchTerm),
);
}
/**
* Store users data in cache for a specific provider
*/
setUsers(provider: string, users: UserRole[], ttl?: number): void {
const userMap = new Map<string, UserRole>();
users.forEach((user) => {
userMap.set(user.userId, user);
});
const providerUserCache: ProviderUserCache = {
provider,
users: userMap,
lastUpdated: Date.now(),
};
const entry: CacheEntry<ProviderUserCache> = {
data: providerUserCache,
timestamp: Date.now(),
ttl: ttl ?? this.defaultTTL,
};
this.usersCache.set(provider, entry);
this.scheduleRefresh(`users:${provider}`, entry.ttl);
process.stderr.write(`[cache] Cached ${users.length} users for ${provider}\n`);
}
/**
* Get cached users for a provider
*/
getUsers(provider: string): UserRole[] | null {
const entry = this.usersCache.get(provider);
if (!entry) {
return null;
}
// Check if cache is expired
if (Date.now() - entry.timestamp > entry.ttl) {
this.usersCache.delete(provider);
return null;
}
return Array.from(entry.data.users.values());
}
/**
* Get all cached users across all providers
*/
getAllUsers(): UserRole[] {
const allUsers: UserRole[] = [];
for (const [provider, entry] of Array.from(this.usersCache.entries())) {
// Check if cache is still valid
if (Date.now() - entry.timestamp <= entry.ttl) {
allUsers.push(...Array.from(entry.data.users.values()));
} else {
// Clean up expired cache
this.usersCache.delete(provider);
}
}
return allUsers;
}
/**
* Search users across all cached providers
*/
searchUsers(query?: string): UserRole[] {
const allUsers = this.getAllUsers();
if (!query) {
return allUsers;
}
const searchTerm = query.toLowerCase();
return allUsers.filter(
(user) =>
user.username.toLowerCase().includes(searchTerm) ||
user.displayName.toLowerCase().includes(searchTerm) ||
user.email?.toLowerCase().includes(searchTerm),
);
}
/**
* Get users for a specific project (from project members cache)
*/
getProjectUsers(projectId: string): UserRole[] {
const allProjects = this.getAllProjects();
const project = allProjects.find((p) => p.id === projectId);
return project?.members ?? [];
}
/**
* Schedule cache refresh
*/
private scheduleRefresh(key: string, ttl: number): void {
// Clear existing timer if any
const existingTimer = this.refreshTimers.get(key);
if (existingTimer) {
clearTimeout(existingTimer);
}
// Schedule new refresh timer
const timer = setTimeout(() => {
process.stderr.write(`[cache] Cache expired for ${key}, needs refresh\n`);
this.refreshTimers.delete(key);
// Emit refresh event (will be handled by the cache warming system)
this.onCacheExpired?.(key);
}, ttl);
this.refreshTimers.set(key, timer);
}
/**
* Callback for when cache expires - can be overridden to trigger refresh
*/
public onCacheExpired?: (cacheKey: string) => void;
/**
* Check if cache is available for a provider
*/
hasValidCache(provider: string, type: 'projects' | 'users'): boolean {
if (type === 'projects') {
const entry = this.projectsCache.get(provider);
return entry !== undefined && Date.now() - entry.timestamp <= entry.ttl;
} else {
const entry = this.usersCache.get(provider);
return entry !== undefined && Date.now() - entry.timestamp <= entry.ttl;
}
}
/**
* Get cache statistics
*/
getStats(): {
projects: { provider: string; count: number; age: number; ttl: number }[];
users: { provider: string; count: number; age: number; ttl: number }[];
} {
const now = Date.now();
const projects = Array.from(this.projectsCache.entries()).map(([provider, entry]) => ({
provider,
count: entry.data.length,
age: now - entry.timestamp,
ttl: entry.ttl,
}));
const users = Array.from(this.usersCache.entries()).map(([provider, entry]) => ({
provider,
count: entry.data.users.size,
age: now - entry.timestamp,
ttl: entry.ttl,
}));
return { projects, users };
}
/**
* Clear all caches
*/
clear(): void {
this.projectsCache.clear();
this.usersCache.clear();
// Clear all timers
for (const timer of Array.from(this.refreshTimers.values())) {
clearTimeout(timer);
}
this.refreshTimers.clear();
process.stderr.write('[cache] All caches cleared\n');
}
/**
* Graceful shutdown - clear all timers
*/
shutdown(): void {
for (const timer of Array.from(this.refreshTimers.values())) {
clearTimeout(timer);
}
this.refreshTimers.clear();
process.stderr.write('[cache] Cache manager shut down\n');
}
}
|