From 9f04b92a7526bdc8616f416058f2d439b7fc3249 Mon Sep 17 00:00:00 2001 From: xfy Date: Tue, 14 Apr 2026 14:12:52 +0800 Subject: [PATCH] =?UTF-8?q?perf(cache):=20=E4=BC=98=E5=8C=96=20FileCache.G?= =?UTF-8?q?et()=20=E9=94=81=E7=B2=92=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除 defer c.mu.Unlock(),在每个 early return 路径添加显式解锁, 在操作完成后立即释放锁,减少锁持有时间,提高并发性能。 Co-Authored-By: Claude Opus 4.6 --- internal/cache/file_cache.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/cache/file_cache.go b/internal/cache/file_cache.go index a33d6f6..68f7267 100644 --- a/internal/cache/file_cache.go +++ b/internal/cache/file_cache.go @@ -86,23 +86,24 @@ func NewFileCache(maxEntries, maxSize int64, inactive time.Duration) *FileCache // - bool: 是否找到有效缓存 func (c *FileCache) Get(path string) (*FileEntry, bool) { c.mu.Lock() - defer c.mu.Unlock() entry, ok := c.entries[path] if !ok { + c.mu.Unlock() return nil, false } // 检查是否过期 if time.Since(entry.LastAccess) > c.inactive { c.removeEntry(entry) + c.mu.Unlock() return nil, false } // 更新访问时间并移到 LRU 链表头部 entry.LastAccess = time.Now() c.lruList.MoveToFront(entry.element) - + c.mu.Unlock() return entry, true }