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:
parent
4bf7318816
commit
9f04b92a75
5
internal/cache/file_cache.go
vendored
5
internal/cache/file_cache.go
vendored
@ -86,23 +86,24 @@ func NewFileCache(maxEntries, maxSize int64, inactive time.Duration) *FileCache
|
|||||||
// - bool: 是否找到有效缓存
|
// - bool: 是否找到有效缓存
|
||||||
func (c *FileCache) Get(path string) (*FileEntry, bool) {
|
func (c *FileCache) Get(path string) (*FileEntry, bool) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
|
||||||
|
|
||||||
entry, ok := c.entries[path]
|
entry, ok := c.entries[path]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
c.mu.Unlock()
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否过期
|
// 检查是否过期
|
||||||
if time.Since(entry.LastAccess) > c.inactive {
|
if time.Since(entry.LastAccess) > c.inactive {
|
||||||
c.removeEntry(entry)
|
c.removeEntry(entry)
|
||||||
|
c.mu.Unlock()
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新访问时间并移到 LRU 链表头部
|
// 更新访问时间并移到 LRU 链表头部
|
||||||
entry.LastAccess = time.Now()
|
entry.LastAccess = time.Now()
|
||||||
c.lruList.MoveToFront(entry.element)
|
c.lruList.MoveToFront(entry.element)
|
||||||
|
c.mu.Unlock()
|
||||||
return entry, true
|
return entry, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user