Skip to content

Commit 65cb5be

Browse files
author
catlog22
committed
feat: 优化缓存管理,增加从缓存读取 CLI 状态和配置的功能
1 parent fd9c551 commit 65cb5be

3 files changed

Lines changed: 107 additions & 29 deletions

File tree

ccw/src/templates/dashboard-js/components/cli-status.js

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,31 @@ async function loadAllStatusesFallback() {
7878

7979
/**
8080
* Legacy: Load CLI tool status individually
81+
* 优先从缓存读取,如果缓存有效则直接使用
8182
*/
8283
async function loadCliToolStatus() {
84+
// 尝试从缓存获取
85+
if (window.cacheManager) {
86+
const cached = window.cacheManager.get('cli-status');
87+
if (cached) {
88+
cliToolStatus = cached;
89+
updateCliBadge();
90+
console.log('[CLI Status] Loaded from cache');
91+
return cached;
92+
}
93+
}
94+
8395
try {
8496
const response = await fetch('/api/cli/status');
8597
if (!response.ok) throw new Error('Failed to load CLI status');
8698
const data = await response.json();
8799
cliToolStatus = data;
88100

101+
// 存入缓存
102+
if (window.cacheManager) {
103+
window.cacheManager.set('cli-status', data, 300000); // 5分钟
104+
}
105+
89106
// Update badge
90107
updateCliBadge();
91108

@@ -135,36 +152,31 @@ async function loadCodexLensStatus() {
135152
/**
136153
* Load CodexLens dashboard data using aggregated endpoint (single API call)
137154
* This is optimized for the CodexLens Manager page initialization
155+
* 优先从缓存读取,如果缓存有效则直接使用
138156
* @returns {Promise<object|null>} Dashboard init data or null on error
139157
*/
140158
async function loadCodexLensDashboardInit() {
159+
// 尝试从缓存获取
160+
if (window.cacheManager) {
161+
const cached = window.cacheManager.get('dashboard-init');
162+
if (cached) {
163+
applyDashboardInitData(cached);
164+
console.log('[CLI Status] CodexLens dashboard init loaded from cache');
165+
return cached;
166+
}
167+
}
168+
141169
try {
142170
const response = await fetch('/api/codexlens/dashboard-init');
143171
if (!response.ok) throw new Error('Failed to load CodexLens dashboard init');
144172
const data = await response.json();
145173

146-
// Update status variables from aggregated response
147-
codexLensStatus = data.status || { ready: false };
148-
semanticStatus = data.semantic || { available: false };
174+
applyDashboardInitData(data);
149175

150-
// Expose to window for other modules
151-
if (!window.cliToolsStatus) {
152-
window.cliToolsStatus = {};
176+
// 存入缓存
177+
if (window.cacheManager) {
178+
window.cacheManager.set('dashboard-init', data, 300000); // 5分钟
153179
}
154-
window.cliToolsStatus.codexlens = {
155-
installed: data.installed || false,
156-
version: data.status?.version || null,
157-
installedModels: [],
158-
config: data.config || {},
159-
semantic: data.semantic || {}
160-
};
161-
162-
// Store config globally for easy access
163-
window.codexLensConfig = data.config || {};
164-
window.codexLensStatusData = data.statusData || {};
165-
166-
// Update badges
167-
updateCodexLensBadge();
168180

169181
console.log('[CLI Status] CodexLens dashboard init loaded:', {
170182
installed: data.installed,
@@ -180,6 +192,35 @@ async function loadCodexLensDashboardInit() {
180192
}
181193
}
182194

195+
/**
196+
* 应用 dashboard-init 数据到状态变量
197+
* @param {object} data - dashboard init 响应数据
198+
*/
199+
function applyDashboardInitData(data) {
200+
// Update status variables from aggregated response
201+
codexLensStatus = data.status || { ready: false };
202+
semanticStatus = data.semantic || { available: false };
203+
204+
// Expose to window for other modules
205+
if (!window.cliToolsStatus) {
206+
window.cliToolsStatus = {};
207+
}
208+
window.cliToolsStatus.codexlens = {
209+
installed: data.installed || false,
210+
version: data.status?.version || null,
211+
installedModels: [],
212+
config: data.config || {},
213+
semantic: data.semantic || {}
214+
};
215+
216+
// Store config globally for easy access
217+
window.codexLensConfig = data.config || {};
218+
window.codexLensStatusData = data.statusData || {};
219+
220+
// Update badges
221+
updateCodexLensBadge();
222+
}
223+
183224
/**
184225
* Legacy: Load semantic status individually
185226
*/
@@ -229,8 +270,23 @@ async function loadInstalledModels() {
229270

230271
/**
231272
* Load CLI tools config from .claude/cli-tools.json (project or global fallback)
273+
* 优先从缓存读取,如果缓存有效则直接使用
232274
*/
233275
async function loadCliToolsConfig() {
276+
// 尝试从缓存获取
277+
if (window.cacheManager) {
278+
const cached = window.cacheManager.get('cli-tools-config');
279+
if (cached) {
280+
cliToolsConfig = cached.tools?.tools || {};
281+
window.claudeCliToolsConfig = cached;
282+
if (cached.defaultTool) {
283+
defaultCliTool = cached.defaultTool;
284+
}
285+
console.log('[CLI Tools Config] Loaded from cache');
286+
return cached;
287+
}
288+
}
289+
234290
try {
235291
const response = await fetch('/api/cli/tools-config');
236292
if (!response.ok) return null;
@@ -244,6 +300,11 @@ async function loadCliToolsConfig() {
244300
defaultCliTool = data.defaultTool;
245301
}
246302

303+
// 存入缓存
304+
if (window.cacheManager) {
305+
window.cacheManager.set('cli-tools-config', data, 300000); // 5分钟
306+
}
307+
247308
console.log('[CLI Config] Loaded from:', data._configInfo?.source || 'unknown', '| Default:', data.defaultTool);
248309
return data;
249310
} catch (err) {

ccw/src/templates/dashboard-js/main.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,30 @@ function initPreloadServices() {
118118
{ isHighPriority: true, ttl: 120000 } // 2分钟
119119
);
120120

121+
// CLI 状态 - 高优先级
121122
window.preloadService.register('cli-status',
122123
() => fetch('/api/cli/status').then(r => r.ok ? r.json() : Promise.reject(r)),
123124
{ isHighPriority: true, ttl: 300000 } // 5分钟
124125
);
125126

127+
// CLI 工具配置 - /api/cli/config(cli-manager.js 使用)
128+
window.preloadService.register('cli-config',
129+
() => fetch('/api/cli/config').then(r => r.ok ? r.json() : Promise.reject(r)),
130+
{ isHighPriority: true, ttl: 300000 } // 5分钟
131+
);
132+
133+
// CLI 工具列表配置 - /api/cli/tools-config(cli-status.js 使用)
134+
window.preloadService.register('cli-tools-config',
135+
() => fetch('/api/cli/tools-config').then(r => r.ok ? r.json() : Promise.reject(r)),
136+
{ isHighPriority: true, ttl: 300000 } // 5分钟
137+
);
138+
126139
// 注册中优先级数据源
127140
window.preloadService.register('codexlens-models',
128141
() => fetch('/api/codexlens/models').then(r => r.ok ? r.json() : Promise.reject(r)),
129142
{ isHighPriority: false, ttl: 600000 } // 10分钟
130143
);
131144

132-
window.preloadService.register('cli-config',
133-
() => fetch('/api/cli/config').then(r => r.ok ? r.json() : Promise.reject(r)),
134-
{ isHighPriority: false, ttl: 300000 } // 5分钟
135-
);
136-
137145
// 立即触发高优先级预加载(静默后台执行)
138146
window.preloadService.runInitialPreload();
139147

ccw/src/templates/dashboard-js/views/codexlens-manager.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// CodexLens Manager - Configuration, Model Management, and Semantic Dependencies
22
// Extracted from cli-manager.js for better maintainability
33

4+
// ============================================================
5+
// EVENT HANDLERS - 用于防止内存泄漏的处理器引用
6+
// ============================================================
7+
var _workspaceStatusHandler = null;
8+
49
// ============================================================
510
// CACHE BRIDGE - 使用全局 PreloadService
611
// ============================================================
@@ -156,13 +161,17 @@ async function refreshWorkspaceIndexStatus(forceRefresh) {
156161
if (window.lucide) lucide.createIcons();
157162
}
158163

159-
// 2. Listen for data update events
164+
// 2. Listen for data update events (防止内存泄漏:先移除旧监听器)
160165
if (window.eventManager) {
161-
var handleUpdate = function(data) {
166+
// 移除之前的监听器(如果存在)
167+
if (_workspaceStatusHandler) {
168+
window.eventManager.off('data:updated:workspace-status', _workspaceStatusHandler);
169+
}
170+
// 创建新的监听器并保存引用
171+
_workspaceStatusHandler = function(data) {
162172
render(data);
163173
};
164-
// Use one-time listener or remove at appropriate time
165-
window.eventManager.on('data:updated:workspace-status', handleUpdate);
174+
window.eventManager.on('data:updated:workspace-status', _workspaceStatusHandler);
166175
}
167176

168177
// 3. Trigger background loading

0 commit comments

Comments
 (0)