perf(cache): 优化 FileCache.Get() 锁粒度

移除 defer c.mu.Unlock(),在每个 early return 路径添加显式解锁,
在操作完成后立即释放锁,减少锁持有时间,提高并发性能。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-14 14:12:52 +08:00
parent 4bf7318816
commit 9f04b92a75

View File

@ -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
}